Tutorials (back to the list of tutorials)

## Arrays

### Simple Array

Array is a container to have series of data. You can make an array of any type and class by putting a pair of square brackets [ ]. See the example below.

```int[] intArray = new int[5]; //array of five integers

double[] values = new double[100]; //array of hundred double

IVec[] vectors = new IVec[10]; //array of ten vectors

IPoint[] points = new IPoint[10]; //array ten points
```

In the code above, although arrays as containers are created, their contents are not assigned yet. Assignment of contents would look like the below.
You'd need to know the length of array you've already created. You can take length as integer number out of an array by adding ".length".

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

int[] intArray = new int[5]; //array of five integers
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
intArray[3] = 4;
intArray[4] = 5; //Note that the last member is [4], not [5].

double[] values = new double[100]; //array of hundred double
for(int i=0; i < values.length; i++){
values[i] = i*2.5;
}

IVec[] vectors = new IVec[10]; //array of ten vectors
for(int i=0; i < vectors.length; i++){
vectors[i] = new IVec( -i*10, i*10, 0 );
}

IPoint[] points = new IPoint[vectors.length]; //array ten points
for(int i=0; i < points.length; i++){
points[i] = new IPoint(vectors[i]).clr(i*0.1, 1-i*0.1, 1);
}
```

Please be aware of the way of the usage of .length in for loops and the usage of [ i ] to have some value assigned to the content of the array and to take the value out of the array to assign other variables.

One of the arrays in the code can be diagrammed like the figure on the right. Check the relationship between index numbers and the length.

With the print debugging method described here, you can see the content of the arrays.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

int[] intArray = new int[5]; //array of five integers
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
intArray[3] = 4;
intArray[4] = 5; //Note that the last member is [4], not [5].

//just checking the contents
for(int i=0; i < intArray.length; i++){
IG.p("intArray[" + i + "] = " + intArray[i] );
}

double[] values = new double[100]; //array of hundred double
for(int i=0; i < values.length; i++){
values[i] = i*2.5;
//checking the contents
IG.p("values[" + i + "] = " + values[i] );
}

IVec[] vectors = new IVec[10]; //array of ten vectors
for(int i=0; i < vectors.length; i++){
vectors[i] = new IVec( -i*10, i*10, 0 );
//checking the contents
IG.p("vectors[" + i + "] = " + vectors[i] );
}

IPoint[] points = new IPoint[vectors.length]; //array ten points
for(int i=0; i < points.length; i++){
points[i] = new IPoint(vectors[i]).clr(i*0.1, 1-i*0.1, 1);
//checking the contents
IG.p("points[" + i + "] = " + points[i] );
}
```

When you run the code above, you will see the message like below in the text area at the bottom of PDE window, showing the content of the arrays.

```
sketch_tutorial.setup: intArray[0] = 1
sketch_tutorial.setup: intArray[1] = 2
sketch_tutorial.setup: intArray[2] = 3
sketch_tutorial.setup: intArray[3] = 4
sketch_tutorial.setup: intArray[4] = 5
sketch_tutorial.setup: values[0] = 0.0
sketch_tutorial.setup: values[1] = 2.5
sketch_tutorial.setup: values[2] = 5.0
sketch_tutorial.setup: values[3] = 7.5
sketch_tutorial.setup: values[4] = 10.0
sketch_tutorial.setup: values[5] = 12.5
.
.
.
sketch_tutorial.setup: values[99] = 247.5
sketch_tutorial.setup: vectors[0] = (0.0,0.0,0.0)
sketch_tutorial.setup: vectors[1] = (-10.0,10.0,0.0)
sketch_tutorial.setup: vectors[2] = (-20.0,20.0,0.0)
sketch_tutorial.setup: vectors[3] = (-30.0,30.0,0.0)
sketch_tutorial.setup: vectors[4] = (-40.0,40.0,0.0)
sketch_tutorial.setup: vectors[5] = (-50.0,50.0,0.0)
.
.
.
```
When you see those contents of arrays, please note that the index number inside [ ] ends at "the length of the array - 1", not "the length of the array" itself. This is because the index number always starts with zero.

Please be aware of the use of vectors.length in this line in the code. This is to match the length of the array of IPoint with the length of the array of IVec.

```IPoint[] points = new IPoint[vectors.length];
```

Let's see what happens if it doesn't match.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

IVec[] vectors = new IVec[10]; //array of ten vectors
for(int i=0; i < vectors.length; i++){
vectors[i] = new IVec( -i*10, i*10, 0 );
}

IPoint[] points = new IPoint[11];
for(int i=0; i < points.length; i++){
points[i] = new IPoint(vectors[i]).clr(i*0.1, 1-i*0.1, 1);
}
```

When you run the code, it crashes the execution and shows the error message like above.
It is an error because when the counting variable i become 10 to assign a value to the final member of the array, point[10], the code tries to access vector[10] but the final member of the array of IVec is vector[9] and the code fails accessing a non-existing variable, showing the error message of "ArrayIndexOutOfBoundsException: 10".

### Null Values

When an array of a class is created with a specific length but the contents are not initialized, the actual content of each member of the array is a null. A null value is an entity which represents nothingness. If you try to access the data or method of the class through a variable whose content is null, the execution of the code will crash.

In case of an array of primitive data types like int, double and float, the contents are automatically initialized with the initial value of each type as listed on this page.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

IPoint[] points = new IPoint[3];

// printing the content of the variable.
IG.p("points[0]="+points[0]);

// this will crash the execution of the code.
points[0].clr(1.,0,0);
```

When you access the null, the error message tells you "NullPointerException", as shown above.

Actually null doesn't show up only in arrays. Even when you declare a variable of a class and you don't assign anything, the content of the variable is null. The example below also cause an error.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

IPoint pt;

// this will cause an error.
pt.clr(1.,0,0);
```

On this code, the error message is different from the previous example and it is "The local variable pt may not have been initialized".

### 2 Dimensional Array

A 2 dimensional array is like a matrix and can be used like a matrix although strictly speaking it is an array of arrays.
For 2 dimensional array, you put two pairs of square brackets [ ][ ].

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

int[][] matrix = new int[3][2];
// initialize contents with int
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 3;
matrix[1][1] = 4;
matrix[2][0] = 5;
matrix[2][1] = 6;

double[][] zValues = new double[10][10];
// initialize contents with double
for(int i=0; i < zValues.length; i++){
for(int j=0; j < zValues[i].length; j++){
zValues[i][j] = i*2 - j*3;
}
}

IVec[][] controlPoints =
new IVec[zValues.length][zValues[0].length];
// initialize contents with IVec
for(int i=0; i < controlPoints.length; i++){
for(int j=0; j < controlPoints[i].length; j++){
controlPoints[i][j] = new IVec(i*10,j*10,zValues[i][j]);
}
}

IPoint[][] points =
new IPoint[controlPoints.length][controlPoints[0].length];
// initialize contents with IPoint
for(int i=0; i < points.length; i++){
for(int j=0; j < points[i].length; j++){
points[i][j] =
new IPoint(controlPoints[i][j]).clr(0.5,i*0.1,j*0.1);
}
}
```

The 2 dimensional array of integer in the code can be diagrammed like the figure on the right. To get the length of the first dimension it is the same form with the 1 dimensional array; matrix.length
To get the length of the second dimension, you need to add [ ] before .length , like matrix[0].length
This is because 2 dimensional array is essentially an array of arrays. Although matrix[0].length , matrix[1].length and matrix[2].length exist independently, they are all same in this code.

### 3 Dimensional Array

In the very same way to expand 1 dimensional array to 2 dimensional array, you can have 3 dimensional array with three pairs of square bracket [ ][ ][ ].

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

int[][][] matrix3 = new int[3][2][2];
// initialize contents with int
matrix3[0][0][0] = 1;
matrix3[0][0][1] = 2;
matrix3[0][1][0] = 3;
matrix3[0][1][1] = 4;
matrix3[1][0][0] = 5;
matrix3[1][0][1] = 6;
matrix3[1][1][0] = 7;
matrix3[1][1][1] = 8;
matrix3[2][0][0] = 9;
matrix3[2][0][1] = 10;
matrix3[2][1][0] = 11;
matrix3[2][1][1] = 12;

double[][][] zShift = new double[10][10][10];
// initialize contents with double
for(int i=0; i < zShift.length; i++){
for(int j=0; j < zShift[i].length; j++){
for(int k=0; k < zShift[i][j].length; k++){
zShift[i][j][k] = (i + j) * k;
}
}
}

IVec[][][] controlPoints =
new IVec[zShift.length][zShift[0].length][zShift[0][0].length];
// initialize contents with IVec
for(int i=0; i < controlPoints.length; i++){
for(int j=0; j < controlPoints[i].length; j++){
for(int k=0; k < controlPoints[i][j].length; k++){
controlPoints[i][j][k] =
new IVec(i*10,j*10,k*10 + zShift[i][j][k]);
}
}
}

IPoint[][][] points =
new IPoint[controlPoints.length][controlPoints[0].length][controlPoints[0][0].length];
// initialize contents with IPoint
for(int i=0; i < points.length; i++){
for(int j=0; j < points[i].length; j++){
for(int k=0; k < points[i][j].length; k++){
points[i][j][k] =
new IPoint(controlPoints[i][j][k])
.clr(i*0.1,j*0.1,k*0.1);
}
}
}
```

The 3 dimensional array of integer in the code can be diagrammed like the figure on the right. A 3 dimensional array is an array of arrays of array. The length of the first dimension is matrix3.length
The length of the second dimension is matrix[0].length
The length of the third dimension is matrix[0][0].length .
Like 2 dimensional arrays, this 3 dimensional array also has matrix[0][0].length , matrix[0][1].length , matrix[1][0].length , matrix[1][1].length , matrix[2][0].length and matrix[2][1].length independently, but again they are all same value in this code.

(back to the list of tutorials)