ArrayList Class Operations Summary

Creating ArrayList

There are three constructors for creating an ArrayList:

  1. When you create an ArrayList using the constructor ArrayList(), the internal array for storage is created with a size 10.

  2. The constructor ArrayList(int initialCapacity) allows us to set the initial capacity.

  3. The constructor ArrayList( Collection) constructs a list containing the elements of the specified collection.

ArrayList Constructor Example:

ArrayList<String> list = new ArrayList<String>();

ArrayList<String> list = new ArrayList<String>(20);

ArrayList<String> list = new ArrayList<String>(myCollection);

 

Adding elements

We can add elements to an array list using the add or addAll methods that appends one or more elements to the end of the list, or an overloaded add or addAll with an index argument that inserts one or more elements at a position within the list.

ArrayList<String> names = new ArrayList<String>();

names.add("Heartin");

names.add("Sneha");

names.add(1, "Jacob");

System.out.println(names);

ArrayList<String> namesNew = new ArrayList<String>();

namesNew.addAll(names);

System.out.println(namesNew);

namesNew.addAll(2,names);

System.out.println(namesNew);

 

This will print:

[Heartin, Jacob, Sneha]

[Heartin, Jacob, Sneha]

[Heartin, Jacob, Heartin, Jacob, Sneha, Sneha]

 

Modifying elements

We can modify an element of a list using the set method.

names.set(1, "June");

 

Retrieving elements and index

To retrieve an element at a given position, use the get method.

System.out.println(names.get(2));

 

The index of the first occurrence of an element can be obtained using the indexOf method.

System.out.println(names.indexOf("Jacob"));

-1 is returned if the element is not present. The index of the last occurrence of an element can be obtained using lastIndexOf method.

 

Traversing

We can traverse an ArrayList using:

  • Simple for statement

  • for-each statement

  • Iterator

  • ListIterator

 

Example  - for each loop

for(String str : names)

{

  System.out.println(str);

}

 

Example – for loop

for(int i=0; i<names.size();i++)

{

  System.out.println(names.get(i));

}

 

Example – Iterator

Iterator<String> iterator = names.iterator();

while (iterator.hasNext()) {

  System.out.println(iterator.next());

}

 

Example – ListIterator

ListIterator can be used to traverse the list in both directions.

ListIterator<String> listIterator = names.listIterator();

while (listIterator.hasNext()) {

  System.out.println(listIterator.next());

}

while(listIterator.hasPrevious()) {

  System.out.println(listIterator.previous());

}

This  will print:

Heartin

June

Sneha

Sneha

June

Heartin

First while loop will traverse in the forward direction from first reaching the last and then the second while loop will traverse in the reverse direction.

Note that if we put the hasPrevious while loop before the next while loop, output will be only:

Heartin

June

Sneha

You can read more about Iterator and at ListIterator at www.javajee.com/enumeration-iterator-and-listiterator-in-java.  

 

Sorting

Collections.sort method can be used to sort an ArrayList:

Collections.sort(names);

 

Removing elements

The clear method will remove all elements. The remove method removes a single element and the removeAll method removes all values in a given collection from the list. The retainAll method retains all values in a given collection from the list.  

namesNew.remove(0);

namesNew.remove("Jacob");

namesNew.retainAll(names);

namesNew.removeAll(names);

namesNew.clear();

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quizzes (17) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (1) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)