The ArrayList class in Java is part of the java.util
package and provides a resizable-array implementation of the List
interface. It is widely used due to its dynamic size, ease of use, and ability to store duplicate elements.
To create an ArrayList
, you need to instantiate it using the ArrayList
class. Here is a simple example:
import java.util.ArrayList;
class Example1 {
public static void main(String[] args) {
ArrayList names = new ArrayList();
names.add("Alice");
names.add("Bob");
System.out.println(names);
}
}
Console Output:
[Alice, Bob]
Elements can be added to an ArrayList
using the add()
method. This method appends the specified element to the end of the list.
import java.util.ArrayList;
class Example2 {
public static void main(String[] args) {
ArrayList numbers = new ArrayList();
numbers.add(10);
numbers.add(20);
numbers.add(30);
System.out.println(numbers);
}
}
Console Output:
[10, 20, 30]
You can access elements in an ArrayList
using the get()
method. This method takes an index as an argument and returns the element at that position.
import java.util.ArrayList;
class Example3 {
public static void main(String[] args) {
ArrayList fruits = new ArrayList();
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits.get(1));
}
}
Console Output:
Banana
The set()
method allows you to modify an element at a specific index. It takes two arguments: the index of the element to replace and the element to be stored.
import java.util.ArrayList;
class Example4 {
public static void main(String[] args) {
ArrayList cars = new ArrayList();
cars.add("Toyota");
cars.add("Honda");
cars.set(1, "Ford");
System.out.println(cars);
}
}
Console Output:
[Toyota, Ford]
Elements can be removed from an ArrayList
using the remove()
method. This method can take either the index of the element to be removed or the element itself.
import java.util.ArrayList;
class Example5 {
public static void main(String[] args) {
ArrayList animals = new ArrayList();
animals.add("Dog");
animals.add("Cat");
animals.remove("Dog");
System.out.println(animals);
}
}
Console Output:
[Cat]
You can iterate over an ArrayList
using a for-each loop. This loop simplifies the iteration process and makes the code more readable.
import java.util.ArrayList;
class Example6 {
public static void main(String[] args) {
ArrayList colors = new ArrayList();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
for (String color : colors) {
System.out.println(color);
}
}
}
Console Output:
Red
Green
Blue
The contains()
method checks if an ArrayList
contains a specific element. It returns true
if the element is found, otherwise false
.
import java.util.ArrayList;
class Example7 {
public static void main(String[] args) {
ArrayList cities = new ArrayList();
cities.add("New York");
cities.add("Los Angeles");
System.out.println(cities.contains("New York"));
}
}
Console Output:
true
To determine the number of elements in an ArrayList
, use the size()
method. This method returns an integer representing the number of elements.
import java.util.ArrayList;
class Example8 {
public static void main(String[] args) {
ArrayList books = new ArrayList();
books.add("1984");
books.add("Animal Farm");
System.out.println(books.size());
}
}
Console Output:
2
The clear()
method removes all elements from an ArrayList
, leaving it empty.
import java.util.ArrayList;
class Example9 {
public static void main(String[] args) {
ArrayList languages = new ArrayList();
languages.add("Java");
languages.add("Python");
languages.clear();
System.out.println(languages);
}
}
Console Output:
[]
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies