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

Info icon This 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)
Image of page 1

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

View Full Document Right 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);
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
Image of page 3

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

View Full Document Right 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...
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
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern