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

Collections New

collection

Uploaded by

vani shree
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Collections New

collection

Uploaded by

vani shree
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Java ArrayList is a part of the Java collections

framework and it is a class of java.util package. It provides


us with dynamic arrays in Java. Though, it may be slower
than standard arrays but can be helpful in programs where
lots of manipulation in the array is needed. This class is
found in java.util package. The main advantage of ArrayList
in Java is, that if we declare an array then we need to
mention the size, but in ArrayList, it is not needed to
mention the size of ArrayList. If you want to mention the
size then you can do it.

Add and Add with index:

import java.io.*;
import java.util.ArrayList;

public class ArrayListDemo {


public static void main(String[] args)
{

// create an empty array list


with an initial capacity
ArrayList<Integer> arrlist = new
ArrayList<Integer>(5);

// use add() method to add


elements in the list
arrlist.add(10);
arrlist.add(22);
arrlist.add(30);
arrlist.add(40);

// adding element 35 at fourth


position
arrlist.add(3, 35);

// let us print all the elements


available in list
for (Integer number : arrlist) {
System.out.println("Number =
" + number);
}
}
}

Output:
Number = 10
Number = 22
Number = 30
Number = 35
Number = 40

Addall():
import java.io.*;
import java.util.ArrayList;

public class ArrayListDemo {


public static void main(String
args[])
{

// create an empty array list1


with initial
// capacity as 5
ArrayList<Integer> arrlist1 =
new
ArrayList<Integer>(5);
// use add() method to add
elements in the list
arrlist1.add(12);
arrlist1.add(20);
arrlist1.add(45);

// prints all the elements


available in list1
System.out.println("Printing
list1:");
for (Integer number : arrlist1)
System.out.println("Number =
" + number);

// create an empty array list2


with an initial
// capacity
ArrayList<Integer> arrlist2 =
new
ArrayList<Integer>(5);

// use add() method to add


elements in list2
arrlist2.add(25);
arrlist2.add(30);
arrlist2.add(31);
arrlist2.add(35);

// let us print all the elements


available in
// list2
System.out.println("Printing
list2:");
for (Integer number : arrlist2)
System.out.println("Number =
" + number);
// inserting all elements, list2
will get printed
// after list1
arrlist1.addAll(arrlist2);

System.out.println("Printing all
the elements");
// let us print all the elements
available in
// list1
for (Integer number : arrlist1)
System.out.println("Number =
" + number);
}
}

Output:Printing list1:
Number = 12
Number = 20
Number = 45
Printing list2:
Number = 25
Number = 30
Number = 31
Number = 35
Printing all the elements
Number = 12
Number = 20
Number = 45
Number = 25
Number = 30
Number = 31
Number = 35

Clear():
import java.util.ArrayList;

// Main class
public class GFG {

// Main driver method


public static void main(String[]
args)
{
// Creating an empty Integer
ArrayList
ArrayList<Integer> arr = new
ArrayList<Integer>(4);

// Adding elements to above


ArrayList
// using add() method
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);

// Printing the elements


inside current ArrayList
System.out.println("The list
initially: " + arr);

// Clearing off elements


// using clear() method
arr.clear();

// Displaying ArrayList
elements
// after using clear() method
System.out.println(
"The list after using
clear() method: " + arr);
}
}

Output
The list initially: [1, 2, 3, 4]
The list after using clear() method: []
Contains:

import java.util.ArrayList;

class GFG {
public static void main(String[]
args)
{

// creating an Empty Integer


ArrayList
ArrayList<Integer> arr = new
ArrayList<Integer>(4);

// using add() to initialize


values
// [1, 2, 3, 4]
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);

// use contains() to check if the


element
// 2 exits or not
boolean ans = arr.contains(2);

if (ans)
System.out.println("The list
contains 2");
else
System.out.println("The list
does not contains 2");

// use contains() to check if the


element
// 5 exits or not
ans = arr.contains(5);

if (ans)
System.out.println("The list
contains 5");
else
System.out.println("The list
does not contains 5");
}
}

Output:
The list contains 2
The list does not contains 5
Get():

import java.util.ArrayList;

// Main class
public class GFG {

// Main driver method


public static void main(String[]
args)
{
// Creating an Empty Integer
ArrayList
ArrayList<Integer> arr = new
ArrayList<Integer>(4);

// Using add() to initialize


values
// [10, 20, 30, 40]
arr.add(10);
arr.add(20);
arr.add(30);
arr.add(40);

// Printing elements of list


System.out.println("List: " +
arr);

// Getting element at index 2


int element = arr.get(2);

// Displaying element at
specified index
// on console inside list
System.out.println("the
element at index 2 is "
+ element);
}
}

Output
List: [10, 20, 30, 40]
the element at index 2 is 30
Remove():

import java.util.*;

public class GFG


{
public static void main(String[]
args)
{
// Demonstrating remove on
ArrayList
List<String> myAlist = new
ArrayList<String>();
myAlist.add("Geeks");
myAlist.add("Practice");
myAlist.add("Quiz");
System.out.println("Original
ArrayList : " + myAlist);
myAlist.remove("Quiz");
System.out.println("Modified
ArrayList : " + myAlist);

// Demonstrating remove on
LinkedList
List<String> myLlist = new
LinkedList<String>();
myLlist.add("Geeks");
myLlist.add("Practice");
myLlist.add("Quiz");
System.out.println("Original
LinkedList : " + myLlist);
myLlist.remove("Quiz");
System.out.println("Modified
LinkedList : " + myLlist);
}
}
Output
Original ArrayList : [Geeks, Practice, Quiz]
Modified ArrayList : [Geeks, Practice]
Original LinkedList : [Geeks, Practice, Quiz]
Modified LinkedList : [Geeks, Practice]
object remove(int index) :
It removes the element at given index. It returns the object
that is removed.

After removing, it shifts subsequent elements(if any) to


left and decreases their indexes by 1.
If the list contains int types, then this method is called
when an int is passed (Please refer this for details)
It throws IndexOutOfBoundsException if index is out of
bound,
Java
// A Java program to demonstrate working
of list remove
// when index is passed.
import java.util.*;

public class GFG


{
public static void main(String[]
args)
{
// Demonstrating remove on
ArrayList
List<String> myAlist = new
ArrayList<String>();
myAlist.add("Geeks");
myAlist.add("Practice");
myAlist.add("Quiz");
System.out.println("Original
ArrayList : " + myAlist);
myAlist.remove("Quiz");
System.out.println("Modified
ArrayList : " + myAlist);

// Demonstrating remove on
LinkedList
List<String> myLlist = new
LinkedList<String>();
myLlist.add("Geeks");
myLlist.add("Practice");
myLlist.add("Quiz");
System.out.println("Original
LinkedList : " + myLlist);
myLlist.remove(2);
System.out.println("Modified
LinkedList : " + myLlist);
}
}

Output
Original ArrayList : [Geeks, Practice, Quiz]
Modified ArrayList : [Geeks, Practice]
Original LinkedList : [Geeks, Practice, Quiz]
Modified LinkedList : [Geeks, Practice]
size:

import java.util.*;

public class GFG1 {


public static void
main(String[] argv)
throws Exception
{
try {

// Creating object of
ArrayList<Integer>
ArrayList<Integer>
arrlist = new
ArrayList<Integer>();

// Populating arrlist1
arrlist.add(1);
arrlist.add(2);
arrlist.add(3);
arrlist.add(4);
arrlist.add(5);

// print arrlist
System.out.println("Bef
ore operation: "
+
arrlist);

// getting total size


of arrlist
// using size() method
int size =
arrlist.size();
// print the size of
arrlist
System.out.println("Siz
e of list = "
+
size);
}

catch
(IndexOutOfBoundsException e) {

System.out.println("Exc
eption thrown: "
+
e);
}
}
}

Output:
Before operation: [1, 2, 3, 4, 5]
Size of list = 5

You might also like