Cse 2123 Arraylists: Jeremy Morris
Cse 2123 Arraylists: Jeremy Morris
Jeremy Morris
A programming problem
The median dollar amount of the purchases The number of purchases where more money was spent than median We cant do this as a running total we need to store the individual purchases to do the computation How can we store a sequence of data values?
2
A programming problem
A sequence of variables of the same data type (e.g. int, double, String, boolean, etc.) Each variable in the array is called an element Array elements are accessed through their index
9
3
A programming snippet
Scanner keyboard = new Scanner(System.in); System.out.print("Enter a filename: "); String fname = keyboard.nextLine(); Scanner inFile = new Scanner(new File(fname)); int numPurchases = inFile.nextInt(); double[] purchases = new double[numPurchases]; int count = 0; while (count<numPurchases) { purchases[count]=inFile.nextDouble(); count=count+1; }
4
A programming problem
Maybe from an online ordering system Except you dont know the number of elements in the file before we read it We just need to keep reading elements from the file until we reach the end How are we going to do that?
A programming snippet
Scanner keyboard = new Scanner(System.in); System.out.print("Enter a filename: "); String fname = keyboard.nextLine(); Scanner inFile = new Scanner(new File(fname)); int numPurchases = inFile.nextInt(); double[] purchases = new double[numPurchases]; int count = 0; while (count<purchases.length) { purchases[count]=inFile.nextDouble(); count=count+1; }
6
A programming problem
In Java we can use an ArrayList to hold this information No need to pre-determine the number of elements up front, just add to the array as we need it
0
7
A programming problem
In Java we can use an ArrayList to hold this information No need to pre-determine the number of elements up front, just add to the array as we need it
1
8
A programming problem
In Java we can use an ArrayList to hold this information No need to pre-determine the number of elements up front, just add to the array as we need it
2
9
Well talk (a lot) more about classes in the coming weeks The thing to understand now is that a class is a combination of data and behavior (methods)
Thats what that magic word new does It constructs a new instance of the class
More about that later too For now, just keep the syntax in mind
11
Primitive types (int, char, boolean, double) only contain data Methods are all implemented separately
Youve already seen this behavior in action: String input = keyboard.nextLine(); nextLine() is a method, one defined by the Scanner class
12
ArrayLists
How do we declare it? How can we put things into an ArrayList? How can we read things from an ArrayList?
13
public static void main(String [] args) { ArrayList<String> stringList = new ArrayList<String>(); Scanner keyboard = new Scanner(System.in); String input = ; while (input.equals(stop) == false) { input = keyboard.nextLine(); stringList.add(input); } int i = 0; while (i<stringList.size()) { System.out.println(stringList.get(i)); i = i + 1; } }
14
public static void main(String [] args) { ArrayList<String> stringList = new ArrayList<String>(); Scanner keyboard = new Scanner(System.in); String input = ; while (input.equals(stop) == false) { input = keyboard.nextLine(); <String>? stringList.add(input); } int i = 0; while (i<stringList.size()) { System.out.println(stringList.get(i)); i = i + 1; } }
15
ArrayList - Declaration
This syntax indicates that the class you are using is a generic class
ArrayList<E> - <E> is the placeholder for the class Can be used with any class For now, you can think of it as replacing the array type declaration:
16
public static void main(String [] args) { ArrayList<String> stringList = new ArrayList<String>(); Scanner keyboard = new Scanner(System.in); String input = ; while (input.equals(stop) == false) { input = keyboard.nextLine(); stringList.add(input); add()? } int i = 0; while (i<stringList.size()) { System.out.println(stringList.get(i)); i = i + 1; } }
17
ArrayList - Methods
Returns true if it is able to add it, false if not The type E is a generic type must match the type used to declare the ArrayList No real equivalent in arrays, since arrays arent dynamic
18
public static void main(String [] args) { ArrayList<String> stringList = new ArrayList<String>(); Scanner keyboard = new Scanner(System.in); String input = ; while (input.equals(stop) == false) { input = keyboard.nextLine(); stringList.add(input); } int i = 0; while (i<stringList.size()) { System.out.println(stringList.get(i)); i = i + 1; } get()? }
19
ArrayList - Methods
The type E is a generic type must match the type used to declare the ArrayList Equivalent to accessing an array via a subscript:
System.out.println(stringArr[index]); System.out.println(stringL.get(index));
20
public static void main(String [] args) { ArrayList<String> stringList = new ArrayList<String>(); Scanner keyboard = new Scanner(System.in); String input = ; while (input.equals(stop) == false) { input = keyboard.nextLine(); stringList.add(input); } int i = 0; size()? while (i<stringList.size()) { System.out.println(stringList.get(i)); i = i + 1; } }
21
ArrayList - Methods
We also need to have a way to check how long our ArrayList is:
int size()
22
ArrayList - Methods
Since ArrayLists are dynamic, theres a second method for adding elements:
void add(int index, E obj)
Moves everything currently in the ArrayList from index on to the right one position to insert the new element into place The type E is a generic type must match the type used to declare the ArrayList No real equivalent in arrays, since arrays arent dynamic
23
ArrayList - Methods
The type E is a generic type must match the type used to declare the ArrayList Returns back what was previously stored at that index Equivalent to using the array subscript:
ArrayList - Methods
Everything to the right of index is shifted one position left to fill the gap Returns back what weve removed No real equivalent in arrays because arrays are not dynamic
25
ArrayList Example
Scanner keyboard = new Scanner(System.in); System.out.print("Enter a filename: "); String fname = keyboard.nextLine(); Scanner inFile = new Scanner(new File(fname)); ArrayList<Double> purchases = new ArrayList<Double>(); while (inFile.hasNext()) { double purch =inFile.nextDouble(); purchases.add(purch); }
26
Collections - ArrayList
27
Collections - ArrayList
However, each primitive type has a wrapper class that we can use
Useful for generic classes (like collections) that require a class and cant use a primitive
30
Taking a primitive data element and wrapping it up in a wrapper class is known as boxing
Integer myInt = new Integer(6);
Java will (usually) automatically do the right thing on its own (autoboxing)
int myPrimitive = myInt; Integer myInt = 6; int sum = myInt + 3;
31
ArrayLists
32
ArrayLists
If we need an ArrayList of primitives, we use the corresponding class instead But we can use autoboxing to make our life easier adding and examining elements
public static void main(String [] args) { ArrayList<Integer> intList = new ArrayList<Integer>(); intList.add(12); int myInt = intList.get(0); }
33
In-class Example
public class ArrayExample { public static int countOccurrences(char [] array, char ch) { int chCount=0; for (int loopCount=0; loopCount<array.length; loopCount++) { if( array[loopCount] == ch ) { chCount=chCount+1; } } return chCount; } public static void main(String [] args) { char [] myChars = new char[4]; myChars[0]=c; myChars[1]=b; myChars[2]=b; myChars[3]=a; char myCh = b; int count = countOccurrences(myChars,myCh); System.out.println(The number of occurrences is: +count); } }
34