Chapter 9 Arrays What is an Array An



![Declaring and Initializing Arrays • Declaring: int [] my. Array; String [] my. Word Declaring and Initializing Arrays • Declaring: int [] my. Array; String [] my. Word](https://slidetodoc.com/presentation_image_h2/dd3d2b531591978bdd97ee4b5d953b45/image-4.jpg)
![…from processing. org/tutorials/arrays, an easier way to remember array declarations // Declare int[] data; …from processing. org/tutorials/arrays, an easier way to remember array declarations // Declare int[] data;](https://slidetodoc.com/presentation_image_h2/dd3d2b531591978bdd97ee4b5d953b45/image-5.jpg)
![Examples of specifying size color [] swatches = new color[4]; int [] store. Loc Examples of specifying size color [] swatches = new color[4]; int [] store. Loc](https://slidetodoc.com/presentation_image_h2/dd3d2b531591978bdd97ee4b5d953b45/image-6.jpg)

![Table: Three ways to initialize Hard code the values in each spot String [] Table: Three ways to initialize Hard code the values in each spot String []](https://slidetodoc.com/presentation_image_h2/dd3d2b531591978bdd97ee4b5d953b45/image-8.jpg)









- Slides: 17
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 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 [] my. Array; String [] my. Word = new String[6]; • Declaring and initializing with values : float[] my. Nums = { 1. 0, 3. 2, 6, 9. 5};
…from processing. org/tutorials/arrays, an easier way to remember array declarations // Declare int[] data; // Declare & create int[] data = new int[5]; // Create data = new int[5]; // Assign data[0] = 19; data[1] = 40; data[2] = 75; data[3] = 76; data[4] = 90; // Assign data[0] = data[1] = data[2] = data[3] = data[4] = // Declare, create, assign int[] data = { 19, 40, 75, 76, 90 }; 19; 40; 75; 76; 90;
Examples of specifying size color [] swatches = new color[4]; int [] store. Loc = new int [12]; int cities = 5; String [] mycities = new String [cities]; int num=60; Car[] cars = new Car [num];
In summary, 3 ways to fill with value (initialize) 1. Hard code the values in each spot. 2. Type out a list in curly brackets separated by commas. 3. Iterate through the elements in an array. See next slide…
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++ { }
Accessing elements in Arrays Iterating through each element is a popular method. See examples 9 -5, 9 -6, and 9 -7. (You should do remix) Let’s try my remix
Create a Bar Chart Look at bar chart example at: https: //processing. org/tutorials/arrays? Then remix your own way. PImage tiger ; int [] clem = {52, 24, 41, 52, 21, 45, 59}; void setup() { size(300, 200); tiger = load. Image("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); //for aesthetics, move remainder of items over 20 px //then added text translate(20, 0); fill(#9900 CC); text. Size(22); text("Clemson's 2019 Scores", 0, 25); //Loop for the bars for (int j = 0; j < clem. length; j++) { fill(#E 57417); //orange rect(j*20, 170 -clem[j], 15, clem[j] ); fill(255); text. Size(11); text(clem[j] , j*20, 185); println(j); } }
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 )
Interactive Objects Rollover effects: - Example 9 -10; lots of possibilities. - Popular in software interfaces of all kinds. - Can be used for appearance or functionality. For the sake of time, let’s copy and discuss Example 9 -10. Remix by making stripe red when mouse hovers over it.
Array with Objects • Example 9 -9, lacks visual appeal, but perfect example of how to use object as array. • CHALLENGE: o How much of that old Car class can you create without looking in 5 minutes? o I’ll give you some particulars.
Revisit a design I shared earlier //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 = {#0 ff 0 ff, #00 ff 22, #ff 6609, #ccff 00, #660033}; void setup() { size(660, 390); background(255); frame. Rate(10); } void draw() { //no. Loop() ; stroke(#ff 6633); 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); stroke(#993366); ellipse(xloc, yloc, size*. 5, size*. 9); //smaller ellipse is oval fill(c[4]); ellipse(xloc, yloc, size/10); } //end of nested for } } This is my remix.
Image Array How do you think it’s done? Partial answer is OK. Quick! -search iconarchive. com or flaticon. com for star. -declare & initialize image in main program. -create a class with x & y variables. -create a display and move functions.
Another Example If time permits, look at another example class Jitterbug { float x; float y; int diameter; float speed = 5; Jitterbug[] bugs = new Jitterbug[10]; void setup() { size(240, 120); Jitterbug(float temp. X, float temp. Y, int temp. Diameter) { x = temp. X; y = temp. Y; diameter = temp. Diameter; } for( int i = 0; i< bugs. length; i++) { bugs[i] = new Jitterbug(random(width), i*3); } void move() { x += random(-speed, speed); y += random(-speed, speed); x = constrain(x, 0, width); y = constrain(y, 0, height); } void draw() { stroke. Weight(. 25); for(int i = 0; i<bugs. length; i++){ bugs[i]. display(); bugs[i]. move(); } } void display() { ellipse(x, y, diameter) ; } } }