Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Different ways of creating arrays & accessing members

Notes:

1. Creating an array in single step : [Starts at: 00min:35sec]
2. Creating an array in two steps : [Starts at: 03min:26sec]
3. Assigning values to array members: [Starts at: 06min:53sec]
4. Accessing values from array members: [Starts at: 12min:27sec]
5. Initializing array members: [Starts at: 14min:43sec]
6. Important notes (Summary): [Starts at: 22min:38sec]

1. Creating an array in single step : [Starts at: 00min:35sec]

Example Code:

// declaring and initializing array in single step
int[] numbers = new int[5];

2. Creating an array in two steps : [Starts at: 03min:26sec]

Example Code:

// declaring and initializing array in two steps
int[] numbers; // declaring array variable
numbers = new int[5]; // initializing array

3. Assigning values to array members: [Starts at: 06min:53sec]

Example Code:

numbers [0] = 10;
numbers [1] = 20;
numbers [2] = 30;
numbers [3] = 40;
numbers [4] = 50;

numbers2 [0] = 10;
numbers2 [1] = 20;
numbers2 [2] = 30;
numbers2 [3] = 40;
numbers2 [4] = 50;

4. Accessing values from array members: [Starts at: 12min:27sec]

Example Code:

Debug.Log (numbers [0]);
Debug.Log (numbers [1]);
Debug.Log (numbers [2]);
Debug.Log (numbers [3]);
Debug.Log (numbers [4]);

Debug.Log (numbers2 [0]);
Debug.Log (numbers2 [1]);
Debug.Log (numbers2 [2]);
Debug.Log (numbers2 [3]);
Debug.Log (numbers2 [4]);

Complete Code:

Example Code:

using UnityEngine;
public class ArraysDemo : MonoBehaviour {
void Start () {

// creating an integer array single step
int[] numbers = new int[5];

// creating an integer array 2 steps
int[] numbers2;
numbers2 = new int[5];

// assigning values to array elements
numbers [0] = 10;
numbers [1] = 20;
numbers [2] = 30;
numbers [3] = 40;
numbers [4] = 50;

numbers2 [0] = 10;
numbers2 [1] = 20;
numbers2 [2] = 30;
numbers2 [3] = 40;
numbers2 [4] = 50;

// Accessing values from array elements
Debug.Log (numbers [0]);
Debug.Log (numbers [1]);
Debug.Log (numbers [2]);
Debug.Log (numbers [3]);
Debug.Log (numbers [4]);

Debug.Log (numbers2 [0]);
Debug.Log (numbers2 [1]);
Debug.Log (numbers2 [2]);
Debug.Log (numbers2 [3]);
Debug.Log (numbers2 [4]);

}
}

5. Initializing array members: [Starts at: 14min:43sec]

// You can add exactly 5 members
int[] numbers = new int[5] {10,20,30,40,50};

// You can add any number of data members
int[] numbers = {10,20,30,40,50,60};

// In two step process, you must and should use new int[size]
int[] numbers2;
numbers2 = new int[5]{10,20,30,40,50};

Complete Code:

using UnityEngine;
public class ArraysDemo : MonoBehaviour {
void Start () {

// initializing array members
int[] numbers = new int[5] {10,20,30,40,50};

int[] numbers2;
numbers2 = new int[5]{10,20,30,40,50};

// getting values from array elements
Debug.Log (numbers [0]);
Debug.Log (numbers [1]);
Debug.Log (numbers [2]);
Debug.Log (numbers [3]);
Debug.Log (numbers [4]);
Debug.Log (numbers2 [0]);
Debug.Log (numbers2 [1]);
Debug.Log (numbers2 [2]);
Debug.Log (numbers2 [3]);
Debug.Log (numbers2 [4]);
}
}

6. Important notes (Summary): [Starts at: 22min:38sec]

// declaring and initializing array in single step
int[] numbers = new int[5];

// declaring and initializing array in two steps
int[] numbers; // declaring array variable
numbers = new int[5]; // initializing array

// You can add exactly 5 members
int[] numbers = new int[5] {10,20,30,40,50};
v // You can add any number of data members
int[] numbers = new int[]{10,20,30,40,50,60};

or int[] numbers = {10,20,30,40,50,60};

// In two step process, you must and should use new int[]
int[] numbers2;
numbers2 = new int[5]{10,20,30,40,50}; // only five

int[] numbers2;
numbers2 = new int[]{10,20,30,40,50}; // any number of

int[] numbers2;
numbers2 = {10,20,30,40,50}; // error