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

1.2 Array List

Uploaded by

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

1.2 Array List

Uploaded by

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

ARRAY LIST

TOPIC 1: LIST

Prepared by: Nik Marsyahariani Nik Daud

Content

List Concepts
List in Collection Frameworks

Sequential list Concept


Differences with Array
ArrayList in Collection Frameworks
Application using ArrayList

Implementing User Defined Sequential List


Application using User Defined Sequential List

1
List Concept

• List: A collection of elements of the same type.


• List operations:
• Add new element into the list
• Remove element from list
• Search for element within a list
• Moving from one element to another within the list

List in Collection Framework

• Collection Framework offers an interface class List in java.util package.


• It is defined as:
An ordered collection where the user of this interface has precise control over
where in the list each element is inserted.
The user can access elements by their integer index (position in the list), and search
for elements in the list.

2
List in Collection Framework

• Some of the methods declared in this interface are:

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

Sequential List Concept

• Also known as ArrayList.


• ArrayList is a class offered in Collection Framework
• User can use this class without knowing how the list is implemented.
• It is called array list as this list class implements array for storage.
• ArrayList, in contrast to array, is dynamic size.

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

Element is stored by using Element is stored by using add long Long


assignment operator methods float Float

Can contain primitive data Can only contain reference double Double
type and reference data data type (object) boolean Boolean
type char Character

Fixed size Dynamic size

ArrayList in Collection Framework

• Java provides ArrayList class that uses array to as the underlying


structure of a list.
• ArrayList implements List interface.
• It can be considered as an improvement to array as:
• it can select random element from the list using subscript value.
• The size is automatically increase or decrease when new element is
added or removed.
• ArrayList is part of Java package (java.util.ArrayList)

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

Application using ArrayList

Import ArrayList class.


Create ArrayList object.
Create object(s) and store it into the ArrayList Object.

Examples:
ArrayList<String> listName=new ArrayList<>();
//create empty list without initial capacity

ArrayList <String> listName=new ArrayList<>(50);


//create empty list with initial capacity 50

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

Application using ArrayList


import java.util.*; }

public static void print(ArrayList<Integer> a){


class simpleAL{ if(a.isEmpty())
System.out.println("List empty");
public static void main(String[] a){ else
for(int i=0;i<a.size();i++){
ArrayList<Integer> listA=new ArrayList<>(); System.out.print(""+a.get(i));
}
Scanner sc=new Scanner(System.in); }
}

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:

• Add new element into list.


• Remove element from list.
• Check if list is empty.
• Check if element is in the list
• Check the capacity of the list.
• Get an element from the list based on its index.

14

Implementing User Defined Sequential List

public class ArrayList {


public Object[] list;
public int size;
private static final int INITIAL_CAPACITY = 10;

public ArrayList(){
list = new Object[INITIAL_CAPACITY];
size = 0;
}

public ArrayList(int c){


list = new Object[c];
size = 0;
}

15

7
Implementing User Defined Sequential List

public boolean isEmpty(){


if(size == 0) return true;
else return false;
}

public void add(Object e){


if(size == 0){
list[0] = e;
}else{
list[size] = e;
}
size++;
}

16

Application using User Defined ArrayList


import java.util.*; }

class simpleAL{ public static void print(ArrayList a){


if(a.isEmpty())
System.out.println("List empty");
public static void main(String[] a){ else
ArrayList listA=new ArrayList(); for(int i=0;i<a.size();i++){
listA.add(12); System.out.print(""+a.get(i));
}
listA.add(15);
}
listA.add(1); }
listA.add(7);

int num1 = (Integer)listA.get(1);


int num2 = (Integer)listA.get(2);
sum = num1+ num2;
System.out.println(sum);
print(listA);

18

8
Summary

List Concepts
List in Collection Frameworks

Sequential list Concept


Differences with Array
ArrayList in Collection Frameworks
Application using ArrayList

Implementing User Defined Sequential List


Application using User Defined Sequential List

19

You might also like