1.2 Array List
1.2 Array List
TOPIC 1: LIST
Content
List Concepts
List in Collection Frameworks
1
List Concept
2
List in Collection Framework
Method Description
boolean add(E e) Appends the specified element to the end of this list
void add(int index, E e) Inserts the specified element at the specified position in this list (optional
operation)
E get() Returns the element at the specified position in this list
boolean isEmpty() Returns true if this list contains no elements
E remove(int index) Removes the element at the specified position in this list
Int size() Returns the number of elements in this list
E set(int index, E e) Replaces the element at the specified position in this list with the
specified element
https://docs.oracle.com/javase/8/docs/api/java/util/List.html
3
Differences with Array
Array ArrayList
A reference type that are Class in Java that implemented Primitive data type Wrapper Class
used to store same the list concept byte Byte
elements short Short
Part of Core Java Part of Collection Framework int Integer
Can contain primitive data Can only contain reference double Double
type and reference data data type (object) boolean Boolean
type char Character
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
4
ArrayList in Collection Framework
Method Description
boolean add(E e) Appends the specified element to the end of this list
void add(int index, E e) Inserts the specified element at the specified position in this list (optional
operation)
E get() Returns the element at the specified position in this list
boolean isEmpty() Returns true if this list contains no elements
E remove(int index) Removes the element at the specified position in this list
Int size() Returns the number of elements in this list
E set(int index, E e) Replaces the element at the specified position in this list with the
specified element
Examples:
ArrayList<String> listName=new ArrayList<>();
//create empty list without initial capacity
10
5
Application using ArrayList
listName.add(”100”);
add(E Element)
//add object to list
String s=listName.get(0);
get(int index)
//returns the element in specified index
listName.set(0,”50”);
set(int,object)
//replace element at specified position with specified element
11
for(int i=0;i<5;i++){
System.out.println("Insert a number");
listA.add(sc.nextInt());
}
print(listA);
12
6
Implementing User Defined Sequential List
• Defining your own sequential list means that you need to understand the
behavior of list and its properties.
• The idea is to write a class that will have, at the very least, the following
operations:
14
public ArrayList(){
list = new Object[INITIAL_CAPACITY];
size = 0;
}
15
7
Implementing User Defined Sequential List
16
18
8
Summary
List Concepts
List in Collection Frameworks
19