Arrays Revisited & ArrayList
Arrays Revisited & ArrayList
and ArrayList
Arrays: Revisited
Construction of Arrays
• Single Dimension: Arrays are internally implemented as object hence
by using new operator we can construct an array.
The only allowed Data type to allow the size are byte, short, char, int. if we are
using any other datatype, then a C.E will be generated.
2
Arrays: Revisited
Illegal Use of Arrays
3
Arrays: Revisited
Construction of Arrays
• Multi Dimension: In java multidimensional arrays are
implemented as single dimension arrays. This approach
improves performance with respect to memory.
• Ex:
int[ ][ ] a = new int[3][2];
6
Arrays: Revisited
Which of the following Array declarations are
valid?
int [ ][ ] a = new int[3][4]; // ✔
int [ ][ ] a = new int[3][]; // ✔
int [ ][ ] a = new int[][4]; // ✘
7
Initialization of Arrays
Once we created an array, all it’s elements
initialized with default values.
Ex:
int[ ] a = new int[3];
System.out.println(a[0]); // 0
System.out.println(a); //I@10b62c9
8
Initialization of Arrays
Ex:
9
What happens when printing
an object in Java
Every reference type in java is a subtype
of java.lang.Object and therefore inherits
its toString() method.
10
ArrayList (Way of Resizing an
Array)
An ArrayList class is a resizable array, which is
present in the java.util package.
While built-in arrays have a fixed size,
ArrayLists can change their size dynamically.
Elements can be added and removed from an
ArrayList whenever there is a need, helping the
user with memory management.
11
Array vs ArrayList
13
Array vs ArrayList
Since ArrayList can’t be created for primitive data
types, members of ArrayList are always references
to objects at different memory locations. Therefore
in ArrayList, the actual objects are never stored at
contiguous locations. References of the actual
objects are stored at contiguous locations.
On the other hand, in the array, it depends
whether the array is of primitive type or object
type. In the case of primitive types, actual values
are contiguous locations, but in the case of objects,
allocation is similar to ArrayList.
14
Methods supported by
ArrayList
Adding Elements: In order to add an element to an
ArrayList, we can use the add() method. This method is
overloaded to perform multiple operations based on
different parameters. They are:
• add(Object): This method is used to add an element at the end
of the ArrayList.
• add(int index, Object): This method is used to add an
element at a specific index in the ArrayList.
Changing Elements: After adding the elements, if we
wish to change the element, it can be done using the
set() method. Since an ArrayList is indexed, the element
which we wish to change is referenced by the index of
the element. Therefore, this method takes an index and
the updated element which needs to be inserted at that
index.
15
Methods supported by
ArrayList
Removing Elements: In order to remove an
element from an ArrayList, we can use the
remove() method. This method is overloaded to
perform multiple operations based on different
parameters. They are:
17
ArrayList Implementation
18
Important Features
20
Important Features
In the above
illustration, AbstractList, CopyOnWriteArrayList and
the AbstractSequentialList are the classes which implement
the list interface. A separate functionality is implemented in
each of the mentioned classes. They are:
23
Constructors in the ArrayList