KEMBAR78
Collection Arraylist | PDF
0% found this document useful (0 votes)
8 views7 pages

Collection Arraylist

The document explains the ArrayList class in Java, highlighting its resizable nature compared to built-in arrays. It provides syntax for creating an ArrayList, methods for adding, accessing, modifying, and removing elements, as well as how to determine the size of the ArrayList and loop through its elements. Examples of code snippets are included to illustrate these functionalities.
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)
8 views7 pages

Collection Arraylist

The document explains the ArrayList class in Java, highlighting its resizable nature compared to built-in arrays. It provides syntax for creating an ArrayList, methods for adding, accessing, modifying, and removing elements, as well as how to determine the size of the ArrayList and loop through its elements. Examples of code snippets are included to illustrate these functionalities.
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/ 7

ARRAY LIST

Collection in java
JAVA ARRAYLIST
The ArrayList class is a resizable array which can be found in the java.util package.
The difference between a built-in array and an ArrayList in Java, is that the size of an array
cannot be modified (if you want to add or remove elements to/from an array, you have to create a
new one). While elements can be added and removed from an ArrayList whenever you want.

Syntax
import java.util.ArrayList; // import the ArrayList class
ArrayList<String> cars = new ArrayList<String>(); // Create
an ArrayList object
Add Items
The ArrayList class has many useful methods. For example, to add elements to the
list, use the add() method:

import java.util.ArrayList;
public class Main {
public static void main(String[] args) { A
ArrayList<String> person = new ArrayList<String>();
person.add(“Sita");
person.add(“Gita");
Person.add(“Rita");
person.add(“Meeta");
System.out.println(person);
}
}
You can also add an item at a specified position by referring to the index number

import java.util.ArrayList;
public class Main {
public static void main(String[] args) { A
ArrayList<String> person = new ArrayList<String>();
person.add(“Sita"); //0
person.add(“Gita"); // 1
Person.add(“Rita"); //2
person.add(“Meeta"); //3
System.out.println(person);
person.add( 2,“Radha");
}
}
Access an Item
To access an element in the ArrayList, use the get() method and refer
to the index number:

person.get(0);

Change an Item
To modify an element, use the set() method and refer to the
index number:

person. Set(0, “laxmi");


Remove an Item
To remove an element, use the remove() method and refer to the index number:

person.remove(0);

person.remove(“Sita”);

ArrayList Size
To find out how many elements an ArrayList have, use the size method:

person.size();
Loop Through an ArrayList
Loop through the elements of an ArrayList with a for loop, and use the size() method to specify
how many times the loop should run:
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> person = new ArrayList<String>();
person.add(“Sita");
person.add(“Gita");
Person.add(“Rita");
person.add(“Meeta");
for (int i = 0; i < person.size(); i++) {
System.out.println(person.get(i));
}
}
}

You might also like