Lec08_Arrays_2per - 1 EECS EECS 285 285 EECS285 Lecture 08...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 EECS EECS 285 285 EECS285 Lecture 08 Arrays Van der Linden Ch. 9 2 Andrew M. Morgan EECS EECS 285 285 Arrays An array is a list of values, all having the same type Example: A list of quiz scores all having type "float" could be stored in a single array rather than 5 uniquely named variables Declare just one variable name to store multiple values Java's arrays are 0-based An array of 10 values are indexed with 0 through 9 inclusive float quizScore1; float quizScore2; float quizScore3; float quizScore4; float quizScore5; ... System.out.print(" 1: " + quizScore1 + " 2: " + quizScore2 + " 3: " + quizScore3 + " 4: " + quizScore4); float quizScores; quizScores = new float[5]; ... System.out.print(" " + (i + 1) + ": " + quizScores[i]); Without Array With Array 2 3 Andrew M. Morgan EECS EECS 285 285 Arrays Overview In C and C++, there are two types of arrays Statically allocated arrays Size specified at declaration-time, memory from stack used Dynamically allocated arrays Pointer declared, size specified when using "new", memory from heap used Java treats its arrays like objects When you declare an array, you declare a reference to an array No size can be specified at declaration-time You must use "new" to allocate the actual array There are no statically allocated arrays in Java 4 Andrew M. Morgan EECS EECS 285 285 Using Arrays Array objects have a public attribute named "length" that indicates how many elements are stored in the array Special for loop syntax for iterating through arrays Two ways to iterate through an array for (<type> <identifier> : <arrayObj>) statement for (String strElem : myStrAry) { System.out.println("Elem: " + strElem); } for (i = 0; i < myStrAry.length; i++) { System.out.println("Elem: " + myStrAry[i]); } 3 5 Andrew M. Morgan EECS EECS 285 285 Simple Array Demo public class ArrayDemo { public static void main(String args) { int squareVals; int sumOfSquares; int i; squareVals = new int[5]; for (i = 0; i < squareVals.length; i++) { squareVals[i] = i * i; } sumOfSquares = 0; for (int sq : squareVals) { out.println("SquareVal: " + sq); sumOfSquares += sq; } out.println("Total Sum: " + sumOfSquares); } Declares a reference to an array only (Don't specify array size here) Allocates space for an array big enough for 5 ints, and sets squareVals to refer to that space length is a member of all array objects "for each" version of the for loop SquareVal: 0 SquareVal: 1 SquareVal: 4 SquareVal: 9 SquareVal: 16...
View Full Document

This note was uploaded on 01/19/2012 for the course EECS 285 taught by Professor Idk during the Fall '08 term at University of Michigan.

Page1 / 8

Lec08_Arrays_2per - 1 EECS EECS 285 285 EECS285 Lecture 08...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online