Arrays and Collections Intro to the Java Collections Framework Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick
Modern programming languages support the concept of collections That is, mechanisms to sort and manipulate many instances of the same type of data In Java, an array is an indexed collection of data values of the same type Arrays of both primitives or objects are possible Integer arrays String arrays WinPlotter arrays Winter 2004 CS-1010 Dr. Mark L. Hornick
Using constants to declare the array sizes does not always lead to efficient use of space rainfall = new double [12]; Declaration of arrays with constants is called fixed-size array declaration. Fixed-size array declaration may pose two problems; either: Not enough capacity for the task at hand. Wasted space (too much capacity) Winter 2004 CS-1010 Dr. Mark L. Hornick
Java is not limited to fixed-size array declaration int size; int[] number; size= Integer.parseInt( JOptionPane.showInputDialog(null, “Enter the size of an array:”)); number = new int[size]; // size determined from user input Winter 2004 CS-1010 Dr. Mark L. Hornick
Any valid integer arithmetic expression is allowed for specifying the size of an array size = Integer.parseInt( JOptionPane.showInputDialog(null,””)); number = new int[size*size + 2* size + 5]; Winter 2004 CS-1010 Dr. Mark L. Hornick
Object Deletion A Before is executed After is executed 1 2 3 person 1 CS-1020 12/29/2018 Object Deletion int delIdx = 1; person[delIdx] = null; Delete Person B by setting the reference in position 1 to null. A 1 2 3 person A B C D 1 2 3 person A C D Before is executed After is executed In this approach, we simply leave the position of a deleted object to a null. With this approach, an array index positions will be a mixture of null and real pointers. Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
Java Collections Framework CS-1020 12/29/2018 Java Collections Framework The java.util standard package contains other types of classes for maintaining a collection of objects. We can add to, remove from, and retrieve objects in a collection. A collection does not have a set limit to the number of objects we can add to it. JCF includes classes that maintain collections of objects as sets, lists, or maps. CS2851 is an entire course based on JCF Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
Advantages to using JCF collection classes Internal structure is hidden from view Code is optimized for performance Code is tested for quality More flexible Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick
CS-1020 12/29/2018 ArrayList is a collection class that implements the following methods (and others) boolean add( Object o ) Adds an object o to the list void clear( ) Clears this list, i.e., make the list empty Object get( int idx ) Returns the element at position idx boolean remove( int idx ) Removes the element at position idx int size() Returns the number of elements in the list There are total of 25 methods defined in the List interface. The five listed here are the very frequently used subset of those 25 methods. Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
Generic ArrayList<E> ArrayList<Double> salaryList = new ArrayList<Double>(); E specifies the specific type of data that the ArrayList can manage You must substitute a class name for E; you cannot create an ArrayList of primitives This creates an instance, salaryList, of the ArrayList collection class. The elements in salaryList must be (references to) Doubles. Double is the wrapper class for the primitive type double. Generics allow you to create a class template that can use different types of objects when instantiated CS-2851 Dr. Mark L. Hornick
Type conversion is done automatically through boxing and unboxing Example: What if you want to insert a double value into an ArrayList<Double> at index 30? salaryList.add(30, 40000.00); This is called boxing: The automatic conversion of a primitive (double) value to the appropriate (Double) wrapper object To retrieve the value, no explicit cast is needed: double pay = /*(double)*/ salaryList.get(30); This is because although the get() method returns a Double, a Double can be converted to a double primitive type. This is called unboxing. CS-2851 Dr. Mark L. Hornick
ArrayList<E> usage features What if salaryList needs to be expanded? Done automatically! What if you need to know the number of elements currently in salaryList? salaryList.size() What if you want to insert the element at index 30? salaryList.add (30, 40000.00); // note: old element 30 becomes element 31, 31->32, etc To retrieve the value, no cast is needed: double sum = 0; sum = sum + salaryList.get(30); …provided that you use the generic version of ArrayList Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick
Built-in automatic sorting Suppose you randomly add elements to an ArrayList You can use the static sort() method of the Collections class to sort them Collections.sort(list); // list is an ArrayList Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick