
Whats funny is that I almost called this “Playing with Schiffman’s Snake.”
Starting School has kicked my P5ing down a bit. Getting back to business now. Here should be everything I need to know about arrays. Its still not clear enough but I’m working on it.
Arrays: anytime a program requires multiple instances of similar data its useful to use an array(if not necessary).
While variables allow programs to track single points of information over a period time, arrays can point to multiple pieces of information. An array is a list of variables that keeps track of its own order.
In an array each element within a list has a unique index value. An index value is an integer value that designates its position within a list. *Starting with 0 an array with 10 elements has array index values ranging from 0 – 9
Using an Array:
There are a couple things to remember when we declare, create and assign an array.
1) Arrays can be any datatype including objects.
2) They are of fixed size.
3) You always have the steps 1)declare 2) create and 3) assign.
An array declaration is written as follows:
type declaration + ["empty brackets"] + the array name.
example
int[]myArray; //declaration
You would then create the array by following the declaration with new operator + type+ ["the size of the array"] ;
example
int[] myArray = new int [100];//creation
The keyword new is what allocates space in the computers memory to store the array’s data.
After the array is created then values can be assgned to it:
int[] myArray = new int[3]; // declare and create
myArray [0] = 3; //assign value
myArray[1] = 2; //assign value
myArray[2] = 1;//assign value
Each element of the array is individually referred to by specifying its index, starting with [0].
______________________________________________________
Using Loops to Fill Arrays
A for loop can be used to store data inside of an array and to access array elements. A for loop or even a while loop can be used very effectively,I’m going to stick with the for loop for brevity’s sake.
Example from Processing, a Programmers Handbook
int[] data = {19,40,75,76,77};
for (int i = 0; i<data.length; i++){
line(data[i], 0, data[i],100);
}
______________________________________________________
Shaking Schiffman’s Snake(hahahaha)
Program a trail that will follow the mouse by using 2 arrays, one to store the horizontal and one to store the verticle mouse locations.
Step 1
Declare 2 arrays that will hold the last 120 mouseX and mouseY positions.
int [] xpos = new int[120];
int [] ypos = new int[120];
Step 2
In the setup() initialize the arrays using a for loop.
for (int i = 0; i < xpos.length; i++){
xpos [i] = 0;
ypos [i] = 0;
}
}
Step 3
Shifting the Array Values:
for (int i = 0; i < xpos.length - 1; i++){
xpos[i] = xpos [i+1];
ypos[i] = ypos [i+1];
}
Step 4
Drawing the new location:
xpos[xpos.length -1] = mouseX;
ypos[ypos.length -1] = mouseY;
Step 5
Draw Everything:
for (int i = 0; i < xpos.length; i++){
noStroke();
fill(255 - i*5);
ellipse(xpos[i], ypos[i], i-120,i-120);
}
}
View the Sketch at my open processing page. http://openprocessing.org/visuals/?visualID=11994