home processing download documents tutorial python tutorial gallery source about
 Python 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.

intArray = [1, 2, 5, 10, 0, -1] #array of integers

values = [0.1, 0.2, 0.01, 101.2, 3.4, -10.1] #array of double

vectors = [IVec(0,0,0), IVec(10,0,0), IVec(-10,5,5)] #array of vectors

points = [IPoint(0,0,0), IPoint(10,0,0), IPoint(-10,0,10)] #array 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".

add_library('igeo')

size( 480, 360, IG.GL )

intArray = [] # empty array to be filled
intArray.append(1)
intArray.append(2)
intArray.append(5)
intArray.append(10)
intArray.append(-1)

values = [] 
for i in range(100) : #100 double numbers are appended
    values.append(i*2.5) 

vectors = [] #10 vectors are appended
for i in range(10) :
    vectors.append(IVec( -i*10, i*10, 0 ))

points = [] 
for i in range(len(vectors)) : #same length with vectors
    points.append(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.

add_library('igeo')

size( 480, 360, IG.GL )

intArray = [] # empty array to be filled
intArray.append(1)
intArray.append(2)
intArray.append(5)
intArray.append(10)
intArray.append(-1)

# just checking the contents
for i in range(len(intArray)) : #same length with intArray
    print("intArray[" + str(i) + "]=" + str(intArray[i])) #i-th member in intArray

values = [] 
for i in range(100) : #100 double numbers are appended
    values.append(i*2.5) 
    # just checking the contents
    print("values[" + str(i) + "]=" + str(values[i]))

vectors = [] #10 vectors are appended
for i in range(10) :
    vectors.append(IVec( -i*10, i*10, 0 ))
    # just checking the contents
    print("vectors[" + str(i) + "]=" + str(vectors[i]))

points = [] 
for i in range(len(vectors)) : #same length with vectors
    points.append(IPoint(vectors[i]).clr(i*0.1, 1-i*0.1, 1))
    # just checking the contents
    print("points[" + str(i) + "]=" + str(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.

for i in range(len(vectors)) :

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

add_library('igeo')

size( 480, 360, IG.GL )

vectors = []
for i in range(10) :
  vectors.append(IVec( -i*10, i*10, 0 ))

points = []
for i in range(11) : # shoud be 10
  points.append(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.

add_library('igeo')

size( 480, 360, IG.GL )

points = []

# printing the content of the variable.
print("points[0]="+str(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.

add_library('igeo')

size( 480, 360, IG.GL )

# 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 [ ][ ].

add_library('igeo')

size( 480, 360, IG.GL )

# 2D array of int (nested list)
matrix = [[ 1, 2 ], [ 3, 4 ], [ 5, 6 ]]

zValues = []
# append 2D array through for-loop
for i in range(10) :
    zValues.append([])
    for j in range(10) :
        zValues[i].append(i*2 - j*3)

controlPoints = []
# set 2D array of vector by zValues through for-loop
for i in range(len(zValues)) : 
    controlPoints.append([])
    for j in range(len(zValues[i])) :
        controlPoints[i].append(IVec(i*10,j*10,zValues[i][j]))

points = []
# set 2D array of vector by controlPoints through for-loop
for i in range(len(controlPoints)) :
    points.append([])
    for j in range(len(controlPoints[i])) :
        points[i].append(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 [ ][ ][ ].

add_library('igeo')

size( 480, 360, IG.GL )

# 3D array of integer
matrix3 = [[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]]

zShift = []
# append 3D array through for-loop
for i in range(10) : 
    zShift.append([])
    for j in range(10) : 
        zShift[i].append([])
        for k in range(10) : 
            zShift[i][j].append((i + j) * k)

controlPoints = []
# set 3D array of vector by zShift through for-loop
for i in range(len(zShift)) : 
    controlPoints.append([])
    for j in range(len(zShift[i])) :
        controlPoints[i].append([])
        for k in range(len(zShift[i][j])) :
            controlPoints[i][j].append(IVec(i*10,j*10,k*10 + zShift[i][j][k]))

points = []
# set 3D array of point by controlPoints through for-loop
for i in range(len(controlPoints)) : 
    points.append([])
    for j in range(len(controlPoints[i])) : 
        points[i].append([])
        for k in range(len(controlPoints[i][j])) : 
            points[i][j].append(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)

HOME
FOR PROCESSING
DOWNLOAD
DOCUMENTS
TUTORIALS (Java / Python)
GALLERY
SOURCE CODE(GitHub)
PRIVACY POLICY
ABOUT/CONTACT