170 likes | 412 Views
Chapter 9 Arrays. What is an Array. An array is a special variable, which can hold a collection of elements of the same type . Examples: A list of colors A list of objects, such as rain drops or cars A list of players in a game
E N D
What is an Array • An array is a special variable, which can hold a collection of elements of the same type. • Examples: • A list of colors • A list of objects, such as rain drops or cars • A list of players in a game • A sequential list of numbers to associate with other variables such as x, y, w, h, color, etc.
Characteristics of Arrays • Arrays start with 0. • Arrays have a fixed size. • Each element in an array has a unique position, in which the order is crucial. • Each index/position of an array is matched with an element. E.g. array of your siblings you might be #2. • The size can be: - A number - A variable of int type - An expression that calculates an integer
Declaring and Initializing Arrays • Declaring: int [] myArray;String [] myWord = new String[6]; • Declaring and initializing with values : float[] myNums = { 1.0, 3.2, 6, 9.5};
…from processing.org/tutorials/arrays, an easier way to remember array declarations // Declare int[] data; // Create data = new int[5]; // Assign data[0] = 19; data[1] = 40; data[2] = 75; data[3] = 76; data[4] = 90; // Declare & create int[] data = new int[5]; // Assign data[0] = 19; data[1] = 40; data[2] = 75; data[3] = 76; data[4] = 90; // Declare, create, assign int[] data = { 19, 40, 75, 76, 90 };
Examples of specifying size color [] swatches = new color[4]; int [] storeLoc = new int [12]; int cities = 5; string [] mycities = new string [cities]; int [] family = new int [siblings+parents]
In summary, 3 ways to fill with value (initialize) • Hard code the values in each spot. • Type out a list in curly brackets separated by commas. • Iterate through the elements in an array. See next slide…
Accessing elements in Arrays • Several ways… Iterating through each element is one of them. See examples 9-5, 9-6, and 9-7. (remix)
Create a Bar Chart As promised, here is the bar chart shown during class Look at bar chart example at: https://processing.org/tutorials/arrays? Then remix your own way. PImage tiger ; int [] clem = {48,28,38,49,27,63,41,59,77, 27}; void setup() { size(300,200); tiger = loadImage("tiger.jpg"); } void draw() { //Tinted and added image tint(120); image(tiger, 0, 0 ); //black rectangle at top fill(0); rect(0,0,width, 40); //move remainder of items over 10px //then added text translate(10, 0); fill(#750FD1); textSize(22); text("Clemson's 2018 Scores", 0, 25); //Loop for the bars for (int j = 0; j < clem.length; j++) { fill(#E57417); rect(j*20, 170-clem[j], 16, clem[j] ); fill(255); textSize(11); text(clem[j] , j*20, 185); } }
Time for Fun Programming again #54 https://www.funprogramming.org A bit of a remix with names and descriptions of computer company founders
Do It Yourself: • Create a String array with about 5 names of family members. • Iterate through each. • If time permits, use a for() to place a rectangle around each name. • println() or text() to see results (See solution at http://moorec.people.cofc.edu/fam.txt )
Array with Objects • Example 9-9 on pg. 177; not most visual appeal, but perfect example of how to use object as array. • For time sake, copy the class. I’ll explain. • Then let’s type the main program from scratch.
Interactive Stripes • Example 9-10; lots of possibilities with this one. • Once again, copy the class and let me explain. • Then type the main program from scratch.
Let’s do a little sharing…I’ll explain //inspired by adult coloring books intxloc= 0; intyloc= 0; int size= 160; int spacer = size/2; //Program will randomly pick from these colors. //swatches better than totally random because you can better target your choices. color [] c = {#0ff0ff, #00ff22, #ff6609, #ccff00, #660033}; void setup() { size(660,390); background(255); frameRate(10); } void draw() { //noLoop() ; stroke(#ff6633); for(intxloc=0 ; xloc<=width ; xloc += spacer) { for(intyloc = 25; yloc <= height; yloc += spacer) { fill(c[int(random(0,4) )], 30); //fill with array "c" ellipse(xloc, yloc, size, size); stroke(#993366); ellipse(xloc, yloc, size*.5, size*.9); //smaller ellipse is oval fill(c[4]); ellipse(xloc, yloc, size/10, size/10); } //end of nested for } } This is my fork. I probably don’t remember how I did it, ha ha.
Another Example If time permits, look at another example class Jitterbug { float x; float y; int diameter; float speed = 5; Jitterbug(float tempX, float tempY, inttempDiameter) { x = tempX; y = tempY; diameter = tempDiameter; } void move() { x += random(-speed, speed); y += random(-speed, speed); x = constrain(x, 0, width); y = constrain(y, 0, height); } void display() { ellipse(x, y, diameter, diameter) ; } } Jitterbug[] bugs = new Jitterbug[10]; void setup() { size(240, 120); for( inti = 0; i< bugs.length; i++) { bugs[i] = new Jitterbug(random(width), random(width), i*3); } } void draw() { strokeWeight(.25); for(inti = 0; i<bugs.length; i++){ bugs[i].display(); bugs[i].move(); } }