Download presentation
Presentation is loading. Please wait.
Published byOpal Morris Modified over 9 years ago
1
Arrays
2
An array is a collection “The Dinner offers an array of choices.” In computer programming, an array is a collection of variables of the same data type Arrays can be collections of ints, chars, floats, booleans,…, any data type. Example: String---a collection of characters
3
What good is a collection? Can you think of times when it would be useful to store a collection of values? –How about the display on the monitor---isn’t that a collection of pixel values? –Test grades---isn’t that a collection –College courses… –The path of a missile… –The names of all students… –Just about any list… We use collections all of the time!
4
How to Create an array An array of integers: int[ ] numbers = {1,2,3,4,5,6,7,8,9,10}; –This is an array with 10 elements An array with no values specified: int[ ] numbers = new int[10]; –This is an array large enough to store 10 elements but the elements have not been specified
5
Creating arrays An array of 10 doubles: double [ ] tomsGrades = {0,1.1,2.2,3,4.4,5.5,6,7,8,9.9}; double [ ] fredsGrades = new double[10]; An array of 10 characters char [ ] sally = {‘a’,’b’,’c’,’b’,…,’a’}; String sue = “abcbacdfaaaa”; this is actually more than just an array char [ ] jane = new char[26];
6
Accessing the elements Each element in an array has a name---it’s the array plus an offset also called an index int [ ] evens = {2, 4, 6, 8, 10}; Name of array element: 246810 Start of array named evens 0 offset 12 34 evens[0]evens[1] evens[2]evens[3]evens[4]
7
for-loops and arrays They were designed to be used together int[ ] numbers = new int[100]; for (int i = 0; i<100; i++) { numbers[i] = i; } OR int[ ] numbers = new int[100]; for (int i = 0; i<numbers.length; i++) { numbers[i] = i; }
8
Let it Snow int num = 11; int spacing = 40; int diameter = 20; float[] x = new float[num]; float[] y = new float[num]; void setup() { size(400, 400); noStroke(); smooth(); fill(255, 180); for (int i = 0; i < num; i++) { x[i]=i*spacing; y[i]=random(0,spacing); } } //continued on next page
9
Snow (continued) void draw() { background(0); for (int i = 0; i < num; i++) { ellipse(x[i], y[i], diameter, diameter); } // move down the canvas for (int i = 0; i < num; i++) { (y[i])++; }
10
Another example int numLines = 12; float[] x = new float[numLines]; float[] speed = new float[numLines]; float offset = 8; // Set space between lines void setup() { size(100, 100); smooth(); strokeWeight(10); for (int i = 0; i < numLines; i++) { x[i] = i; // Set initial position speed[i] = 0.1 + (i / offset); // Set initial speed } } //continued on next page
11
Continued… void draw() { background(204); for (int i = 0; i < x.length; i++) { x[i] += speed[i]; // Update line position if (x[i] > (width + offset)) { // If off the right, x[i] = -offset * 2; // return to the left } float y = i * offset; // Set y-coordinate for line line(x[i], y, x[i] + offset, y + offset); // Draw line }
12
Your Turn Modify the Snow example presented earlier to make the snow flakes reappear at the top of the canvas once they have moved off the bottom of the canvas.
13
Review and More Fun With Arrays int num = 50; int[] x = new int[num]; int[] y = new int[num]; void setup() { size(100, 100); noStroke(); smooth(); fill(255, 102); } //continued on next page
14
More Fun With Arrays (cont) void draw() { background(0); // Shift the values to the right for (int i = num - 1; i > 0; i--) { x[i] = x[i-1]; y[i] = y[i-1]; } // Add the new values to the beginning of the array x[0] = mouseX; y[0] = mouseY; // Draw the circles for (int i = 0; i < num; i++) { ellipse(x[i], y[i], i / 2.0, i / 2.0); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.