Python Tutorials | (back to the list of tutorials) |
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".
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".
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.
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
The length of the third dimension is
Like 2 dimensional arrays, this 3 dimensional array also has