04 Java Arrays

You might also like

You are on page 1of 16

CS 335 Lecture 04 Java Programming Arrays

Fall 2003

Arrays in Java
Indexes start at 0 All data types (primitive and user-defined objects) can be put into arrays Any expression which evaluates to an integer can be used as the array subscript Arrays can be re-allocated to change the size; they are static once created

Declaring Arrays
Arrays must be both declared and allocated (using the new command):

int myarray[] = new int [100] int myarray[] = new int [100] int myarray[]; int myarray[]; myarray = new int[100]; myarray = new int[100];

Example Declarations
byte bytearray[] = new byte [255] char chararray[] = new char [255] MyObj ObjArray[]; ObjArray = new MyObj[10] byte [] array1, array2; array1 = new byte[100]; array2 = new byte[200];

Allocating Arrays
Must allocate with the new command

null pointer exception!!

Fixed size after allocation The allocated size can be dynamically determined with an expression:

char chararray[]; char chararray[]; ... ... chararray = new char [size*6]; chararray = new char [size*6];

Dynamic Arrays
Re-allocation possible: char myarray[] = new char [255]; char tmparray[] = new char [255]; ... for (int i=0; i<myarray.length; i++) tmparray[i]= myarray[i]; myarray = new char[255*2]; for (int i=0; i<255; i++) myarray[i]= tmparray[i];

// Fig. 7.4: InitArray.java // Fig. 7.4: InitArray.java // initializing an array with a declaration // initializing an array with a declaration import javax.swing.*; import javax.swing.*; public class InitArray { public class InitArray { public static void main( String args[] ) { public static void main( String args[] ) { String output = ""; String output = ""; // Initializer list specifies number of elements and // Initializer list specifies number of elements and // value for each element. // value for each element. int n[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; int n[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; output += "Subscript\tValue\n"; output += "Subscript\tValue\n"; for ( int i = 0; i < n.length; i++ ) for ( int i = 0; i < n.length; i++ ) output += i + "\t" + n[ i ] + "\n"; output += i + "\t" + n[ i ] + "\n"; JTextArea outputArea = new JTextArea( 11, 10 ); JTextArea outputArea = new JTextArea( 11, 10 ); outputArea.setText( output ); outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, JOptionPane.showMessageDialog( null, outputArea, "Initializing an Array with a Declaration", "Initializing an Array with a Declaration", JOptionPane.INFORMATION_MESSAGE ); JOptionPane.INFORMATION_MESSAGE ); } } System.exit( 0 ); } System.exit( 0 ); }

// Fig. 7.5: InitArray.java // Fig. 7.5: InitArray.java // initialize array n to the even integers from 2 to 20 // initialize array n to the even integers from 2 to 20 import javax.swing.*; import javax.swing.*; public class InitArray { public class InitArray { public static void main( String args[] ) { public static void main( String args[] ) { final int ARRAY_SIZE = 10; final int ARRAY_SIZE = 10; int n[]; // reference to int array int n[]; // reference to int array String output = ""; String output = ""; n = new int[ ARRAY_SIZE ]; // allocate array n = new int[ ARRAY_SIZE ]; // allocate array // Set the values in the array // Set the values in the array for ( int i = 0; i < n.length; i++ ) for ( int i = 0; i < n.length; i++ ) n[ i ] = 2 + 2 * i; n[ i ] = 2 + 2 * i; output += "Subscript\tValue\n"; output += "Subscript\tValue\n"; for ( int i = 0; i < n.length; i++ ) for ( int i = 0; i < n.length; i++ ) output += i + "\t" + n[ i ] + "\n"; output += i + "\t" + n[ i ] + "\n"; JTextArea outputArea = new JTextArea( 11, 10 ); JTextArea outputArea = new JTextArea( 11, 10 ); outputArea.setText( output ); outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, JOptionPane.showMessageDialog( null, outputArea, "Initializing to Even Numbers from 2 to 20", "Initializing to Even Numbers from 2 to 20", JOptionPane.INFORMATION_MESSAGE ); JOptionPane.INFORMATION_MESSAGE ); } } System.exit( 0 ); } System.exit( 0 ); }

// Fig. 7.8: Histogram.java // Fig. 7.8: Histogram.java // Histogram printing program // Histogram printing program import javax.swing.*; import javax.swing.*; public class Histogram { public class Histogram { public static void main( String args[] ) public static void main( String args[] ) { { int n[] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; int n[] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; String output = ""; String output = ""; output += "Element\tValue\tHistogram"; output += "Element\tValue\tHistogram"; for ( int i = 0; i < n.length; i++ ) { for ( int i = 0; i < n.length; i++ ) { output += "\n" + i + "\t" + n[ i ] + "\t"; output += "\n" + i + "\t" + n[ i ] + "\t"; for ( int j = 1; j <= n[ i ]; j++ ) // print a bar for ( int j = 1; j <= n[ i ]; j++ ) // print a bar output += "*"; output += "*";

} }

JTextArea outputArea = new JTextArea( 11, 30 ); JTextArea outputArea = new JTextArea( 11, 30 ); outputArea.setText( output ); outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, JOptionPane.showMessageDialog( null, outputArea, "Histogram Printing Program", "Histogram Printing Program", JOptionPane.INFORMATION_MESSAGE ); JOptionPane.INFORMATION_MESSAGE ); } } System.exit( 0 ); System.exit( 0 );

} }

Arrays as Parameters
Java treats arrays as objects. When the actual parameter is an array, it is passed to the method by reference:
int a[] = { 1, 2, 3, 4, 5}; ... modifyArray ( a ); ... public void modifyArray (int b[]) { ... }

Arrays as Parameters
When elements in an array are primitive data types, the elements can be passed by value:
int a[] = { 1, 2, 3, 4, 5}; int a[] = { 1, 2, 3, 4, 5}; ... ... modifyElement ( a[4] ); modifyElement ( a[4] ); ... ... public void modifyElement (int elem) { public void modifyElement (int elem) { ... ... } }

Example: Binary Search


// Binary search // Binary search public int binarySearch( int array[], int key ) public int binarySearch( int array[], int key ) { { int low = 0; // low subscript int low = 0; // low subscript int high = array.length - 1; // high subscript int high = array.length - 1; // high subscript int middle; // middle subscript int middle; // middle subscript while ( low <= high ) { while ( low <= high ) { middle = ( low + high ) / 2; middle = ( low + high ) / 2; if ( key == array[ middle ] ) // match if ( key == array[ middle ] ) // match return middle; return middle; else if ( key < array[ middle ] ) else if ( key < array[ middle ] ) high = middle - 1; // search low end of array high = middle - 1; // search low end of array else else low = middle + 1; low = middle + 1; // search high end of array // search high end of array // searchKey not found // searchKey not found

} } } }

return -1; return -1;

Multi-Dimensional Arrays
Java supports single arrays whose elements are also single arrays, thus achieving the effect of double-subscripted arrays:
int my2darray[][] = { {1, 2}, {3, 4}}; int my2darray[][] = { {1, 2}, {3, 4}}; exprResult = my2darray[0][0]*my2darray[1][0]; exprResult = my2darray[0][0]*my2darray[1][0];

Multi-Dimensional Arrays
Arrays of objects must have each slot allocated with the new command:
int b[][]; int b[][]; b = new int [4][6]; b = new int [4][6]; JButton board[][]; JButton board[][]; board = new Jbutton[4][6]; board = new Jbutton[4][6]; for (int i = 0; i < board.length; i++) for (int i = 0; i < board.length; i++) for (int j = 0; j < board[i].length; j++) for (int j = 0; j < board[i].length; j++) board[i][j] = new JButton(); board[i][j] = new JButton();

Example Game: Memory


Basic constructs: arrays, buttons, objects Simple GUI http://www.lizardpoint.com/fun/java/conc/Memory.html

You might also like