1 of 9

Arrays That Are Not Full &�Parallel Arrays

2 of 9

Putting Values into the Array Using the Intializer List

  • Arrays can be declared, instantiated and initialized in one step. The list of number between the braces is called an initializer list.

int [ ] array1 = {1,2,3,4,5};

3 of 9

Declaring Multiple Arrays

  • Just like variables, multiple arrays can be declared or instantiated simultaneously.

int [ ] array1 = new int [100], array2 = new int [100];

Be sure to include the “new” term when declaring arrays. Failure to do this will result in a �null pointer exception.

4 of 9

Declaring Multiple Arrays

  • Because arrays are objects, two variables can refer to the same array.

int [ ] array1 = new int [100];

array2 = array1;

Since array1 and array2 refer to the same set of array elements, changing one will result in a �change to the other.

5 of 9

Copying From One Array �to Another

  • If you want array1 and array2 to refer to two separate arrays that contain the same initial values, you can copy all elements from array1 to array2.

int [ ] array1 = new int [100], array2 = new int [100];

int i;

for (i = 0; i < 100; i++) {

array1[i] = array2[i];

}

6 of 9

Working With Arrays That Are Not Full

  • When an array is created, the computer automatically fills its elements with default values of 0.

  • What if the user only adds 5 numbers to an array of 20 elements? What happens to the other 15 elements?

7 of 9

Working With Arrays That Are Not Full

  • The array has a Physical Size and a Logical Size.

  • Physical Size is the actual size of the array that was declared.
  • Logical Size is the amount of elements currently used.

8 of 9

Working With Arrays That Are Not Full

  • One way to avoid problems with unused parts of the array is the track the Logical Size of the array.�
  • This can be done using a separate variable called size that increments as elements are added to the array.�
  • Just remember that the size variable cannot exceed the Physical Size of the array.

9 of 9

Parallel Arrays

  • Parallel Arrays are multiple arrays that correspond by the number of the index.

Ex:

String [ ] name = {“Bill”, “Sue”, “Jan”, “Joe”};

int [ ] age = {20 , 21 , 19 , 14 };