0% found this document useful (0 votes)
21 views

Arrays Revisited & ArrayList

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Arrays Revisited & ArrayList

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Chapter 10 : Array Revisited

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.

Ex: int[ ] a = new int[10];


int[ ] a = new int[ ]; // C.E
It is legal to have an array with size 0
int[ ] a = new int[0];
If the number of elements in a Java array is zero, the array is said to be empty. In
this case you will not be able to store any element in the array; therefore the
array will be empty.

If we are specifying array size with some –ve integer


int[ ] a = new int[-10];
we will get R.E saying NegativeArraySizeException.

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

int[ ] a = new int[10]; // ✔


int[ ] a1 = new int[100]; // ✔
int[ ] a = new int[100]; // ✘

int[ ] a2 = new int[10.5]; // ✘

int[ ] a3 = new int[true]; // ✘

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];

int[ ][ ] a = new int[4][ ];


a[0] = new int[1];
a[1] = new int[2];
a[2] = new int[4];
a[3] = new int[3];
4
Arrays: Revisited
 How to declare an array for the following
diagrams?

int a[ ][ ][ ] = new int[2][ ][ ];


a[0] = new int[3][ ];
a[0][0] = new int[1];
a[0][1] = new int[2];
a[0][2] = new int[3];
a[1] = new int[2][2];
5
Arrays: Revisited
 How to declare an array for the following
diagrams?

int [ ][ ][ ][ ] a = new int[1][1][1][1];

Note*: This array will store only 1 element.

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]; // ✘

int [ ][ ] a = new int[][]; // ✘

int [ ][ ][ ] a = new int[3][4][]; // ✔


int [ ][ ][ ] a = new int[3][][5]; // ✘

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

int[ ][ ] a = new int[3][2];


System.out.println(a); //I@10b62c9
System.out.println(a[0]); //I@82ba41
System.out.println(a[0][0]); // 0

8
Initialization of Arrays

Ex:

int[ ][ ] a = new int[3][ ];


System.out.println(a); // I@10b62c9
System.out.println(a[0]); // null
System.out.println(a[0][0]); // NullPointerException

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.

The toString method for class Object returns a string


consisting of the name of the class of which the object is
an instance, the at-sign character `@', and the unsigned
hexadecimal representation of the hash code of the
object. In other words, this method returns a string
equal to the value of:

getClass().getName()+ ‘@’ + Integer.toHexString(hashCode))

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

 An array is a basic functionality provided by java.


ArrayList is a part of the collection framework in
Java. Therefore array members are accessed using
[ ], while ArrayList has a set of methods to access
elements and modify them.

 The array is a fixed-size data structure while


ArrayList is not. One need not mention the size of
the ArrayList while creating its object. Even if we
specify some initial capacity, we can add more
elements.
12
Array vs ArrayList

 An array can contain both primitive data types as


well as objects of a class depending on the
definition of the array. However, ArrayList only
supports object entries, not the primitive data
types.
• When we do arraylist.add(1) than it converts the primitive
int data type into an Integer object

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:

• remove(Object): This method is used to simply remove an


object from the ArrayList. If there are multiple such objects,
then the first occurrence of the object is removed.

• remove(int index): Since an ArrayList is indexed, this


method takes an integer value which simply removes the
element present at that specific index in the ArrayList. After
removing the element, all the elements are moved to the left
to fill the space and the indices of the objects are updated.
16
Methods supported by
ArrayList
 Iterating the ArrayList: There are multiple ways
to iterate through the ArrayList. The most famous
ways are by using the basic for loop in combination
with a get() method to get the element at a
specific index.

17
ArrayList Implementation

18
Important Features

 ArrayList inherits AbstractList class and implements


List interface.
 ArrayList is initialized by the size. However, the size
is increased automatically if the collection grows or
shrinks if the objects are removed from the
collection.
 Java ArrayList allows us to randomly access the list.
 ArrayList can not be used for primitive types, like int,
char, etc. We need a wrapper class for such cases.
 ArrayList in Java can be seen as a vector in C++.
 ArrayList is not Synchronized. Its equivalent
synchronized class in Java is Vector.
19
ArrayList in Depth

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:

• AbstractList: This class is used to implement an unmodifiable list, for


which one needs to only extend this AbstractList Class and implement
only the get() and the size() methods.
• CopyOnWriteArrayList: This class implements the list interface. It is an
enhanced version of ArrayList in which all the modifications(add, set,
remove, etc.) are implemented by making a fresh copy of the list.

• AbstractSequentialList: This class implements the Collection


interface and the AbstractCollection class. This class is used to
implement an unmodifiable list, for which one needs to only extend this
AbstractList Class and implement only the get() and the size() methods.
21
How ArrayList works
Internally
Since ArrayList is a dynamic array and we do not have
to specify the size while creating it, the size of the array
automatically increases when we dynamically add and
remove items. Though the actual library implementation
may be more complex, the following is a very basic idea
explaining the working of the array when the array
becomes full and if we try to add an item:

• Creates a bigger sized memory on heap memory (for


example memory of double size).
• Copies the current memory elements to the new memory.
• New item is added now as there is bigger memory available
now.
• Delete the old memory.
22
Constructors in the ArrayList

In order to create an ArrayList, we need to create an


object of the ArrayList class. The ArrayList class
consists various constructors which allows the
possible creation of the array list. The following are
the constructors available in this class:

• ArrayList(): This constructor is used to build an empty


array list. If we wish to create an empty ArrayList with the
name arr, then, it can be created as:
ArrayList arr = new ArrayList();

23
Constructors in the ArrayList

• ArrayList(Collection c): This constructor is used to build an


array list initialized with the elements from the collection s.c.
Suppose, we wish to create an arraylist arr which contains the
elements present in the collection c, then, it can be created as:
ArrayList arr = new ArrayList(c);

• ArrayList(int capacity): This constructor is used to build an


array list with initial capacity being specified. Suppose we wish
to create an ArrayList with the initial size being N, then, it can
be created as:
ArrayList arr = new ArrayList(N);

The generic ArrayList is created in the same way as shown in the


previous slides.n
ArrayList<String> arr = new ArrayList<String>();
24

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy