Java – Part II Lecture Notes 4
Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates) l The number corresponding to each position is called an index or subscript Index = 0 Value = 12 Index = 2 Value = 16 Index = 1 Value = 10
Declaring and using Arrays Arrays are objects Int [] height = new int[11] New operator allocates memory space to save values The type of the array is int [] Eg: Random x = new Random(); final int LIMIT = 15; int [] list = new int[LIMIT] for (int I=0; I<LIMIT;I++) { list[I] = x.nextInt(LIMIT);} for (int I=0; I<LIMIT;I++) { System.out.print(list[I]+”\t”);}
Declaring and using Arrays Two ways to declare arrays: int [] grades; int grades[]; No difference as far as compiler is concerned First one is more consistent with type declarations int [] A, B, C; int A[], B, C[];
Interactively Read a set of numbers into an Array import javax.swing.JOptionPane; boolean done = false; final int LIMIT = 15; int [] list = new int[LIMIT]; int index = 0; Int num; While (true) { num =Integer.parseInt( JOptionPane.showInputDialog("Enter an integer")); if (I==LIMIT-1 || num==-999) break; else list[I++] = num; }
An Array Example final int NUMCHARS = 26; String line = JOptionPane.showInputDialog("Enter a sentence"); char current; int other= 0; int [] upper = new int[NUMCHARS]; For (int I=0; I<line.length(); I++) { current = line.charAt(I); if (current >= ‘A’ && current <= ‘Z’) upper[current-’A’]++; } What does above program do?
More on Arrays Array Intializer int [] list = {1,2,3,4}; Array as Parameter An entire array can be passed as a parameter to a method A copy of the reference to the array is passed A method can change array elements permanently A method cannot change the reference itself
More on Arrays Array of String Objects String [] words = new String[10]; Creates an array that holds references to String objects Reversing an Array for (int I=0; I<SIZE/2;I++) { int temp = list[I]; list[I] = list[SIZE-I-1]; list[SIZE-I-1]=temp; }
Two Dimensional Arrays A Table (Grid) of Rows and Columns Uses Two Indexes to refer to an element Two Dimensional Array(or Matrix) is an array of Arrays Eg: int [][] Table = new int[5][10]; for (int I=0; I<Rows; I++) for (int j=0;j<Cols;j++) Table[I][j]=I+j; for (int I=0;I<Table.length; I++) for (int j=0;j<Table[I].length;j++) System.out.print(Table[I][j]);
Two Dimensional Arrays ctd.. Int [][] Scores = {{2,3,3},{2,2,2}}; Defines a 2 by 3 matrix (I.e 2 rows and 3 columns) Finding Row Sum of Row 0 (first row) for (int j=0; j<Scores[0].length;j++) sum += Scores[0][j]; Finding Column Sum of Column 2 (third column) for (int i=0; i<Scores.length;i++) sum += Scores[i][2];
The Vector Class import java.util.* Vector is like an array Stores multiple values by an index Array size is fixed after declaration Vector object can grow dynamically as needed Vector objects maintain list of references to an object class Some Vector Methods Vector () - default constructor (size =0) void addElement(Object obj); void insertElementAt(Object obj, int index) void setElementAt(Object obj, int index) Object remove(int index)
Some Vector Methods ctd.. boolean removeElement(Object obj) void removeElementAt(int index) void clear() boolean contains(Object obj) int indexOf(Object obj) Object elementAt(int index) boolean isEmpty() int size();
A Vector Example Vector Band = new Vector(); Band.addElement(“Guna”); Band.addElement(“Mary”); Band.addElement(“Mo”); // print the entire band as a vector [ …..] System.out.println(Band); Band.removeElement(“Mo”); // insert (by pushing others to right, at index 2) Band.insertElementAt(“Ringo”, 2);
Conclusion One Dimensional Arrays(vectors) Have many Applications. Read a Set of Data Sort, Search, Modify, insert, Delete and many more Two Dimensional Arrays have many applications Computer Graphics Table implementation and many more