Title: SE1020
1Arrays and Collections
- Intro to the Java Collections Framework
2Modern 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
3Square brackets are used to declare an array
- double rainfall
- or
- double rainfall
- An array named rainfall of type double
4The new operator is used to allocate the memory
to store the values in an array
- double rainfall
- rainfall new double 12
- // declares and then creates an array big enough
to hold 12 values
5We use a single identifier to refer to the whole
collection in the array
- rainfall0 24.5 // rainfall for 1st day
- We use an indexed expression to refer to the
individual elements of the collection. - Arrays use zero-based indexing
-
- An individual value in an array is called an
array element.
6In Java, an array is a reference data type (like
an object)
7- An array of 12 double values after all 12 are
assigned values.
8Determining array length after creation
- An array has a public constant length attribute
that indicates the size of the array. - for (i0 iltrainfall.length i)
- ...
-
- This allows you to check the size of an array
without knowing how it was initially declared - The size of the array remains the same whether
you assigned values to each element or not
9Dont confuse the length attribute of an array
and the length() method of a String
- length() is a method for a String object, so we
use the syntax for calling a method - String str This is a string
- int size str.length()
10Shortcut An array can be initialized at the time
it is declared
- double rainfall 1.3, 2.5, 4.2, 6.5, 0.8,
1.7, 1.5, 3.2, 6.7, 1.9, 0.3, 1.5 - When initialized in this way, it is unnecessary
to use the new operator or to state the size of
the array. - The length is determined by the number of values
in the list
11Using 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)
12Java 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 intsize // size determined from
user input
13Any valid integer arithmetic expression is
allowed for specifying the size of an array
- size Integer.parseInt(
- JOptionPane.showInputDialog(null,))
- number new intsizesize 2 size 5
14Arrays are not limited to primitive data types
- String monthName January, February,
March, April, May, June, July,
August, September, October, November,
December - An array of objects (e.g. Person) is declared and
created just as an array of primitive data types
is. - Person person
- person new Person20
15- An array of Person objects after the array is
created.
16Arrays of Objects must be initialized
- Array elements are initially null. Person20
person // each personk is null - Since each array element is an object, each must
also be created. - person0 new Person( Me )
- person1 new Person( You )
-
17Since each array element is an object, each must
also be created.
Code
One Person object is created and the reference to
this object is placed in position 0.
State of Memory
18Object Deletion
Delete Person B by setting the reference in
position 1 to null.
19Java 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
20Advantages to using JCF collection classes
- Internal structure is hidden from view
- Code is optimized for performance
- Code is tested for quality
- More flexible
21Whats the difference between ArrayList and
LinkedList?
- The ArrayList class internally uses an array to
manage data - Faster access, slower add/remove
- The LinkedList class uses a technique called
linked-node representation - Slower access, faster add/remove
But performance isnt noticeably different for
small collections (say, less than 10000 elements)
22ArrayList and LinkedList are two collection
classes that share similar methods
- Both classes implement the List interface, which
defines the following methods (partial)
23Generic ArrayListltEgt(or LinkedListltEgt)
- ArrayListltDoublegt salaryList
- new ArrayListltDoublegt()
- E specifies the specific type of data that the
ArrayList can manage - You must substitute a class name for E
- 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
24Automatic type conversion
- What if you want to insert a double value into an
ArrayListltDoublegt 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.
Note This example assumes JRE gt 1.5
25ArrayListltEgt and LinkedListltEgt 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-gt32, 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
Note This example code assumes JRE 1.5
26ArrayList vs. ArrayListltEgt(LinkedList vs.
LinkedListltEgt)
- JDK 1.4 (deprecated) To declare an ArrayList
that can contain any datatypeArrayList
someList - JDK 1.5 and later To declare an ArrayList that
can contain only Doubles ArrayListltDoublegt
salaryList
27Built-in automatic sorting
- Suppose you randomly add elements to an
ArrayList/LinkedList - You can use the static sort() method of the
Collections class to sort them - Collections.sort(list) // list is an ArrayList
or LinkedList