lec12

lec12 - CSE 8A Lecture 12 Declaring and creating arrays...

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

View Full Document Right Arrow Icon
Page 1 of 31 CSE 8A, UCSD LEC 12 CSE 8A: Lecture 12 Declaring and creating arrays Indexing Array initialization Arrays as arguments (Reading: Savitch, Ch. 6)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Page 2 of 31 CSE 8A, UCSD LEC 12 Problem solutions with few variables Some problems involve many data values, but since the individual values need not be stored separately, only a few variables are required in the solution Example: the user will enter 1000 numbers at the terminal; print out their average 1000 pieces of data. .. but a straightforward solution uses only 3 variables: double num, sum=0; for(int i=0; i<1000; i++) { System.out.print("Enter a number: "); num = scn.nextDouble(); sum += num; } System.out.println("Their average is " + sum/1000);
Background image of page 2
Page 3 of 31 CSE 8A, UCSD LEC 12 Problem solutions with many variables Some problems involve many data values, and all the individual values need to be stored separately during the computation; therefore, many variables are required in the solution Example: the user will enter 1000 numbers at the terminal; print out all and only the numbers that are larger than the average need >= 1000 variables, because you won’t know the average until you read all the numbers, and you won’t know which are larger than average until you know the average Example: the user will enter 1000 numbers at the terminal; print the numbers in sorted increasing order need >= 1000 variables, because you don’t know what position any number should be in, until you have read all the numbers It would be possible but very tedious to declare and manipulate 1000 separate variables in a Java solution to those problems (try it!). .. But you don’t have to. .. arrays permit a more elegant solution
Background image of page 3

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

View Full DocumentRight Arrow Icon
Page 4 of 31 CSE 8A, UCSD LEC 12 Simple and aggregate data types A variable of a simple data type contains only one data value In Java, the primitive data types are simple data types A variable of an aggregate data type can contain several data values In Java, a class will usually be an aggregate data type, because an object of a class can contain several instance variables Arrays are a particular kind of aggregate data type, a very useful kind. ..
Background image of page 4
Page 5 of 31 CSE 8A, UCSD LEC 12 Arrays: elements, length, and indexing An array is a sequence of variables, all of the same type in Java, these variables can be reference type pointers or primitive values these pointers or primitive values are the elements of the array these elements can be of any type (primitive types, classes. .. even arrays!), but all must be of the same type according to the Java type system An array contains a certain number of elements this number is the size or length of the array the size of an array is specified when the array is created, and cannot be changed The elements of an array are indexed by integers from 0 up to the size of the array an element is referred to using the name of a pointer to the array, together with the index of the element
Background image of page 5

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

View Full DocumentRight Arrow Icon
Page 6 of 31 CSE 8A, UCSD LEC 12 Declaring arrays in Java
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/12/2008 for the course CSE 8 taught by Professor Marx during the Fall '08 term at UCSD.

Page1 / 31

lec12 - CSE 8A Lecture 12 Declaring and creating arrays...

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

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