Unformatted text preview:

Arrays Background Programmer often need the ability to represent a group of values as a list List may be one dimensional or multidimensional Java provides arrays and the collection classes Consider arrays first Basic terminology List is composed of elements Elements in a list have a common name The list as a whole is referenced through the common name List elements are of the same type the base type Elements of a list are referenced by subscripting indexing the common name Java array features Subscripts are denoted as expressions within brackets Base element type can be any type Size of array can be specified at run time Index type is integer and the index range must be 0 n 1 Where n is the number of elements Automatic bounds checking Ensures any reference to an array element is valid Data field length specifies the number of elements in the list Array is an object Has features common to all other objects Array variable definition styles Without initialization ElementType id Type of values in list Brackets indicate array variable being defined Name of list Array variable definition styles With initialization Nonnegative integer expression specifying the number of elements in the array ElementType id new ElementType n Reference to a newarray of n elements Example Definitions char c int value new int 10 Causes Array object variable c is un initialized Array object variable v references a new ten element list of integers Each of the integers is default initialized to 0 c value 0 0 0 0 0 0 Consider int v new int 10 int i 7 int j 2 int k 4 v 0 1 v i 5 v j v i 3 v j 1 v i v 0 v v j 12 System out println v 2 v k Integer parseInt stdin readLine v 0 0 0 0 0 0 0 0 0 0 v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Consider int v new int 10 int i 7 int j 2 int k 4 v 0 1 v i 5 v j v i 3 v j 1 v i v 0 v v j 12 System out println v 2 v k Integer parseInt stdin readLine v 1 0 0 0 0 0 0 0 0 0 v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Consider int v new int 10 int i 7 int j 2 int k 4 v 0 1 v i 5 v j v i 3 v j 1 v i v 0 v v j 12 System out println v 2 v k Integer parseInt stdin readLine v 1 0 0 0 0 0 0 5 0 0 v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Consider int v new int 10 int i 7 int j 2 int k 4 v 0 1 v i 5 v j v i 3 v j 1 v i v 0 v v j 12 System out println v 2 v k Integer parseInt stdin readLine v 1 0 8 0 0 0 0 5 0 0 v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Consider int v new int 10 int i 7 int j 2 int k 4 v 0 1 v i 5 v j v i 3 v j 1 v i v 0 v v j 12 System out println v 2 v k Integer parseInt stdin readLine v 1 0 8 6 0 0 0 5 0 0 v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Consider int v new int 10 int i 7 int j 2 int k 4 v 0 1 v i 5 v j v i 3 v j 1 v i v 0 v v j 12 System out println v 2 v k Integer parseInt stdin readLine v 1 0 8 6 0 0 0 5 12 0 v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Consider int v new int 10 int i 7 int j 2 int k 4 v 0 1 v i 5 v j v i 3 8 is displayed v j 1 v i v 0 v v j 12 System out println v 2 v k Integer parseInt stdin readLine v 1 0 8 6 0 0 0 5 12 0 v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Consider int v new int 10 int i 7 int j 2 int k 4 v 0 1 v i 5 v j v i 3 Suppose 3 is extracted v j 1 v i v 0 v v j 12 System out println v 2 v k Integer parseInt stdin readLine v 1 0 8 6 3 0 0 5 12 0 v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Consider Segment int b new int 100 b 1 0 b 100 0 Causes Array variable to reference a new list of 100 integers Each element is initialized to 0 Two exceptions to be thrown 1 is not a valid index too small 100 is not a valid index too large IndexOutOfBoundsException Consider Point p new Point 3 p 0 new Point 0 0 p 1 new Point 1 1 p 2 new Point 2 2 p 0 setX 1 p 1 setY p 2 getY Point vertex new Point 4 4 p 1 p 0 p 2 vertex Consider Point p new Point 3 p 0 new Point 0 0 p 1 new Point 1 1 p 2 new Point 2 2 p 0 setX 1 p 1 setY p 2 getY Point vertex new Point 4 4 p 1 p 0 p 2 vertex p 0 p 1 p 2 p Point 0 0 Point 1 1 Point 2 2 Consider Point p new Point 3 p 0 new Point 0 0 p 1 new Point 1 1 p 2 new Point 2 2 p 0 setX 1 p 1 setY p 2 getY Point vertex new Point 4 4 p 1 p 0 p 2 vertex p 0 p 1 p 2 p Point 1 0 Point 1 1 Point 2 2 Consider Point p new Point 3 p 0 new Point 0 0 p 1 new Point 1 1 p 2 new Point 2 2 p 0 setX 1 p 1 setY p 2 getY Point vertex new Point 4 4 p 1 p 0 p 2 vertex p 0 p 1 p 2 p Point 1 0 Point 1 2 Point 2 2 Consider Point p new Point 3 p 0 new Point 0 0 p 1 new …


View Full Document

UVA CS 101 - Arrays

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Iteration

Iteration

92 pages

Classes

Classes

186 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Load more
Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?