Upload
selima
View
20
Download
1
Embed Size (px)
DESCRIPTION
Arrays. Spring 2010. 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 int , char , double , boolean ,…, any data type. What good is a collection?. - PowerPoint PPT Presentation
Citation preview
Arrays
Spring 2010
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 int, char, double, boolean,…, any data type.
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!
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 (by default all will be 0)
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 characterschar [ ] sally = {’a’,’b’,’c’,’b’,…,’a’};char [ ] jane = new char[10];
Accessing the elements
• Each element in an array has a name---it’s the array plus an offset also called an indexint [ ] evens = {2, 4, 6, 8, 10};
Name of array element:
2 4 6 8 10
Start of array named evens
0offset
1 2 3 4
evens[0] evens[1] evens[2] evens[3] evens[4]
for-loops and arrays• They were designed to each other
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;
}
An exampleint 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…
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 }}
Strings: Like an Array
• Strings are similar to arrays of characters, but these are two very distinct data typesString word = ”dictionary”;Char word[] = {’d’,’i’,’c’,’t’,’i’,’o’,’n’,’a’,’r’,’y’};
• Strings have special String-only functions that differ from those of arrays. Some of those String-only functions are substring(), toLowerCase() and charAt().
Arrays of StringsString[] trees = { "lychee", "coconut", "fig"};trees = shorten(trees); // Remove the last element from the arrayprintln(trees); // Prints "lychee coconut"
trees = shorten(trees); // Remove the last element from the arrayprintln(trees); // Prints "lychee"
Arrays of Strings
String[] trees = { "lychee", "coconut", "fig"};trees = append(trees, "guava");
// What does this do?
trees = shorten(trees); println(trees); // What will be printed now?
Arrays of Strings: length() vs length
String[] trees = { "lychee", "coconut", "fig"};trees = append(trees,"guava");
println(trees.length);//What is printed and why?
println(trees[2].length());//What is printed and why?
Assignment 6• Image Processing is the signal processing of an image. Begin with a png or
jpg image of your choice. Using a mouse or keyboard event, trigger an image processing function which reads from and modifies the image pixel array. You must use a for loop to index into the array of pixels. The example code below has an example of reading and writing into that array. Use the image of your choice to be the background.
• 10% Aesthetics• 10% zipped .pde file type• 10% image included in zip file• 20% for loop• 30% reading the pixel array• 20% modifying the pixel array
Assignment 6
//Author: Susan Reiser//Oct 8, 2009float radius = 1.0;int h = 0, dimension;float s = 1;PImage img;
void setup() { size(500,500); background(0); colorMode(HSB,360,100,100); noStroke(); smooth(); img = loadImage("space.jpg"); //load background dimension = img.height * img.width; image(img,0,0); frameRate(10000);}
Assignment 6void draw() {}
void keyPressed() { if (key == 'h') { img.loadPixels(); //increments hue 60 degrees // % makes sure hue never grows beyond 360 for (int i=0; i < dimension; i++) { img.pixels[i] = color((hue(img.pixels[i])+60)%360,
saturation(img.pixels[i]), brightness(img.pixels[i])); } img.updatePixels(); image(img, 0, 0); }}
Assignment 6
void mousePressed() { //loads the pixels from the display img.loadPixels();}void mouseDragged(){ int h, s, b, j, i, currentIndex = mouseX-1 + (mouseY- 1)*img.width; color currentPixelColor; img.loadPixels(); //sums the nine pixels h,s,b centered at the mouse for (h=0, s=0,b=0,j = 0; j<3; j++) { for (i = 0; i<3; i++) { h += hue(img.pixels[currentIndex+i]); s += saturation(img.pixels[currentIndex+i]); b += brightness(img.pixels[currentIndex+i]); } currentIndex += img.width; }
Assignment 6
//sets fill to the mean of the 9 pixels fill(color(h/9, s/9,constrain(b/9+20,0,100), 50)); //draws a circle wth the averaged fill at the location ellipse(mouseX, mouseY, 20,20);}void mouseReleased() { /* refreshes the pixels and redraws the image to the display */ img.updatePixels(); image(img, 0, 0);}