Java Arrays: Fixed-Sized Collections of Variables - Prof. Brian F. Hanks, Study notes of Javascript programming

An introduction to arrays in java, a fixed-sized collection of variables of the same type with a single identifier. Topics covered include array declaration, initialization, accessing elements, assigning values, and using arrays with loops. Examples are given for numeric, boolean, and reference types.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-hn4-1
koofers-user-hn4-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 15
Announcements
Read Section 4.12 again!
Next Program on course web site!
Quiz Review
Arrays
A few days ago I said that Java provided two ways of keeping a set of values in a
collection:
1) Collection classes in the Java library.
2) Arrays.
We're going to look at arrays now.
Arrays
An array is a fixed-sized set of variables of the same type that have a single identifier
associated with them. Each of the variables is called an element of the array.
An array is similar to an ArrayList, but:
- included as part of language, not as a library class
ospecial syntax
omore efficient
- fixed size
- Elements of ArrayList must be objects. Arrays can have elements of any type,
including int, double, char, boolean
Declaration of an array variable:
<ElementType>[] <identifier>;
Examples:
int[] quizScores;
double[] temperature;
String[] monthNames;
int[] daysPerMonth;
Student[] classList;
These declare arrays whose elements are the given type. The type of these variables is
โ€œarray of <type>โ€. Arrays are objects, so these declarations only declare/allocate the
reference. We need to use new to create the array elements themselves.
pf3
pf4
pf5

Partial preview of the text

Download Java Arrays: Fixed-Sized Collections of Variables - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 15

Announcements Read Section 4.12 again! Next Program on course web site! Quiz Review Arrays A few days ago I said that Java provided two ways of keeping a set of values in a collection:

  1. Collection classes in the Java library.
  2. Arrays. We're going to look at arrays now. Arrays An array is a fixed-sized set of variables of the same type that have a single identifier associated with them. Each of the variables is called an element of the array. An array is similar to an ArrayList, but:
  • included as part of language, not as a library class o special syntax o more efficient
  • fixed size
  • Elements of ArrayList must be objects. Arrays can have elements of any type, including int, double, char, boolean Declaration of an array variable: [] ; Examples: int[] quizScores; double[] temperature; String[] monthNames; int[] daysPerMonth; Student[] classList; These declare arrays whose elements are the given type. The type of these variables is โ€œarray of โ€. Arrays are objects, so these declarations only declare/allocate the reference. We need to use new to create the array elements themselves.

quizScores = new int[ 18 ]; temperature = new double[ 365 ]; monthNames = new String[ 12 ]; daysPerMonth = new String[ 12 ]; classList = new Student[ numStudents ]; Draw picture of one or two of these. Show what happens to the array elements. numeric types initialized to 0 Booleans initialized to false References initialized to null โ€“ means that they donโ€™t reference anything yet. Can also do both parts in a single declaration Emphasize that declaring the array only creates the reference. You have to allocate the array elements also! Accessing array elements Ok, this is all great. I've created these arrays that are sets of variables with the same name. How do I actually use the array elements in a program? We access the array elements using subscripting or indexing. Each array element has a subscript (or index ) that we can use to refer to that element. The subscripts are integer values, starting at 0. Thus, the first element of the array has subscript 0, the second has subscript 1, and the last has a subscript of the array-size - 1. [Note similarity to indexing in ArrayList]. int[] daysPerMonth = new int[12]; Draw picture, showing the array subscripts, and the names of each element (daysPerMonth[0], daysPerMonth[1], ...) The elements of an array are referenced using the array name and a subscript. So, we can say daysPerMonth[6] or quizScores[4] or temperature[300]. The value of the index or subscript MUST BE an integer expression. That means we don't have to use constants. temperature[ index ] daysPerMonth[ currentMonth ] Assigning values to array elements Use an assignment statement, but the variable on the left-hand side is indexed. monthName[0] = "January"; daysPerMonth[0] = 31; // January (1 - 1)

size method:

  • Uses the .length instance variable associated with array. Note that it does not use (). This tells us how many elements the array has. (Not necessarily the same thing as the number of elements that you are using) printNumbers
  • uses foreach loop
  • prints out the values of the array elements
  • Discuss use of System.out.print getSum
  • uses while loop
  • calculates sum of the array elements
  • uses .length Write printLargerValues and findLargest methods with class.

Lab Exercise Complete the ArrayExample class. It has two methods that you need to finish:

  • printBackward โ€“ print the elements of the array in reverse order.
  • printEvenValues โ€“ print the elements of the array that are even numbers. Email your ArrayExample.java file to me โ€“ use the subject "Lab 12". Lab Exercise Complete the ArrayExample class. It has two methods that you need to finish:
  • printBackward โ€“ print the elements of the array in reverse order.
  • printEvenValues โ€“ print the elements of the array that are even numbers. Email your ArrayExample.java file to me โ€“ use the subject "Lab 12". Lab Exercise Complete the ArrayExample class. It has two methods that you need to finish:
  • printBackward โ€“ print the elements of the array in reverse order.
  • printEvenValues โ€“ print the elements of the array that are even numbers. Email your ArrayExample.java file to me โ€“ use the subject "Lab 12". Lab Exercise Complete the ArrayExample class. It has two methods that you need to finish:
  • printBackward โ€“ print the elements of the array in reverse order.
  • printEvenValues โ€“ print the elements of the array that are even numbers. Email your ArrayExample.java file to me โ€“ use the subject "Lab 12". Lab Exercise Complete the ArrayExample class. It has two methods that you need to finish:
  • printBackward โ€“ print the elements of the array in reverse order.
  • printEvenValues โ€“ print the elements of the array that are even numbers. Email your ArrayExample.java file to me โ€“ use the subject "Lab 12".