Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Arrays.

Similar presentations


Presentation on theme: "Chapter 9 Arrays."— Presentation transcript:

1 Chapter 9 Arrays

2 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.

3 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

4 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};

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 };

6 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]

7 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…

8 Table: Three ways to initialize
Hard code the values in each spot String [] stuff = new String [4]; stuff[0] = "Buick"; stuff[1] = "Nissan"; stuff[2] = "Toyota"; stuff[3] = "Ford"; Type out a list in curly brackets separated by commas. String [] siblings = {"Ernest", "Mary", "Therman"}; The above two methods are inefficient for large arrays. The third option is most widely used. Also, there are more advanced ways such as filling with data from a text file. Iterate through the elements in an array. This also allows you to access each index value. for(int i = 0; i < stuff.length; i++ { }

9 Accessing elements in Arrays
Several ways… Iterating through each element is one of them. See examples 9-5, 9-6, and 9-7. (remix)

10 Create a Bar Chart As promised, here is the bar chart shown during class Look at bar chart example at: 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);

11 Time for Fun Programming again
#54 A bit of a remix with names and descriptions of computer company founders

12 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 )

13 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.

14 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.

15 Let’s do a little sharing…I’ll explain
//inspired by adult coloring books int xloc= 0; int yloc= 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(int xloc=0 ; xloc<=width ; xloc += spacer) { for(int yloc = 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.

16 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, int tempDiameter) { 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( int i = 0; i< bugs.length; i++) { bugs[i] = new Jitterbug(random(width), random(width), i*3); } void draw() { strokeWeight(.25); for(int i = 0; i<bugs.length; i++){ bugs[i].display(); bugs[i].move();


Download ppt "Chapter 9 Arrays."

Similar presentations


Ads by Google