Introduction
To Java - MFC 158
Week
7 Lecture notes - Fall 2000
Chapter 7 - Arrays - MFC 158 (1 of 3)
Arrays
A.
Data structures consisting of related data elements of the
same type.
B.
A group of contiguous memory locations
C.
We specify the Name of the array and position number of an
element in the array
D.
The first element is the 0th element
E.
Every array in Java knows its own length, using
(array.length)
Declaring and
allocating Arrays
int c[ ] ; //
declares the array c = new int
[ 12 ]; // allocates space for
12 elements int c[ ] =
new int[ 12 ]; // declares and
allocates in one step c.length =
returns the size of the array (for while loops, etc.) c[0] = 2; //
initializes the first element of c to 2; c[4] = 2; //
initializes the 5th element of c to 2; a = 5; b = 6; c[a + b] +=
2 //
adds 2 to the 12th element sum = c[0] +
c[1] + c[2] // calculates the sum of
the 1st three elements, into sum x = c[6] /
2 // divides the value of the 7th
element in array c by 2, puts in x String b[ ]
= new String[100], x[ ] = new String[27];
// 2 string arrays declared |
Manipulation
of arrays
int n[ ]; // declare reference to an array n
= new int[ 10 ]; // dynamically
allocate array output
+= "Subscript\tValue\n"; for
( int i = 0; i < n.length; i++ )
// concatenates each value into 'output' output += i +
"\t" + n[ i ] + "\n"; |
int a[ ] = { 1, 2, 3, 4, 5, 6, 7, 8,
9, 10 }; // declare an array and set
the values int total = 0; for ( int i = 0; i < a.length; i++ ) // add the elements of the array
into variable total total = total + a[ i ]; |
References and
Reference parameters - two ways to pass arguments to methods:
-
call-by-value:
a copy of the argument is made and is passed to the called method
-
call-by-reference: a 'reference' to the original parameter is
passed. When the reference is modified
in called method, it directly modifies the original value.
-
Saves
time by avoiding copying data to the called method
-
Can
weaken security because the called method can access the caller's data
directly.
-
Arrays
are always passed by reference in Java
-
Passing
large arrays by reference is very efficient (can waste time and memory
otherwise)
Example of
passing an array to a method
int a[ ] = { 1, 2, 3, 4, 5 }; // declare array a, allocate and set the
value of each element for ( int i = 0; i < a.length; i++
) output += " " + a[ i ]; modifyArray( a ); // array a passed call-by-reference for ( int i = 0; i < a.length; i++
) output += " " + a[ i ]; public void
modifyArray( int b[ ] ) { for ( int j = 0; j < b.length; j++
) b[ j ] *= 2; } |
Sorting arrays - in this
course, we won't cover the mechanics of sorting, but we need to be able to know
how to call sort routines to do our work.
Chapter 24 "Collections" covers methods for sorting large
amounts of data.
int a[ ] = {
2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
// declare an array and initialize with values bubbleSort(
a ); // sending the array (by
reference) to sort the array public void
bubbleSort( int b[] ) {
for ( int pass = 1; pass <
b.length; pass++ ) // passes for ( int i = 0; i < b.length -
1; i++ ) // one pass if ( b[ i ] > b[ i + 1 ]
) // one comparison swap( b, i, i + 1 ); // one swap } |
Searching
arrays - we
need to know how to call methods to perform searches. We will not cover the mechanics in detail in this course.
-
linear
search - OK for use with small arrays (and can be unsorted data).
-
binary
search - useful for large arrays (must be in sorted order)
Calling a
Binary Search method
int a[ ]; // create
array and fill with even integers 0 to 28 a = new int[
15 ]; for ( int i
= 0; i < a.length; i++ ) a[ i ] = 2 * i; int element
= binarySearch( a, Integer.parseInt( searchKey ) ); // perform the binary
search public int
binarySearch( int array[ ], int key ) { …etc… return found_element or (-1) if not
found …etc… } |
Calling a
linear search method
int a[ ]; // create
array and populate with even integers 0 to 198 a = new int[
100 ]; for ( int i
= 0; i < a.length; i++ ) a[ i ] = 2 * i; int element
= linearSearch( a, Integer.parseInt( searchKey ) ); if ( element
!= -1 ) result.setText( "Found value
in element " + element ); else result.setText( "Value not
found" ); // Search
"array" for the specified "key" value public int
linearSearch( int array[ ], int key ) {
for ( int n = 0; n < a.length; n++
) if ( array[ n ] == key ) return n; return -1; } |
Multiple-subscripted
Arrays
- often used to represent tables of values consisting of data organized in rows
and columns. To identify an element,
specify two subscripts.
int a[ ] [ ];
Column 0 Column1 Column2
Row 0 a[0]
[0] a[0] [1] a[0] [2]
Row 1 a[1]
[0] a[1] [1] a[1] [2]
Row 2 a[2]
[0] a[2] [1] a[2] [2]
int a[ ] [
]; a = new
int[3] [3]; int count =
1; for (int i =
0; i < a.length; i++)
// populates elements with ascending numbers for (int j = 0; j < a.length;
j++) a[i][j] = count++; for (int i =
0; i < a.length; i++) { //
prints out coordinates 00,01,02,10,11,12,20,21,22 for (int j = 0; j < a.length;
j++) // as shown below.
System.out.print(" " + a[i][j] + " "); System.out.println(""); } |
The code above
prints out a matrix
1
2 3
4
5 6
7
8 9