ArrayList

As a part of the java.util package, we can resize the ArrayList by adding or removing elements. This is the basic difference between java array and ArrayList.

Declaraing an ArrayList

We need to import java.util to use ArrayList.
import java.util.ArrayList;
Create an ArrayList
ArrayList languages = new ArrayList();
Here is the full code.
package my_proj;

import java.util.Locale;
import java.util.ArrayList;
public class my_first {

public static void main(String[] args) {
	ArrayList languages = new ArrayList();
}
}
ArrayList functions
add()Adding elements to ArrayList
addAll()Adding elements from an array to ArrayList
clear()Removing all elements of an ArrayList
clone()Create shallow clone of the ArrayList.
contains()Search for presence of elements in ArrayList
ensureCapacity()Ensure minimum capacity for the ArrayList
get()Returns element at the input position of the ArrayList
indexOf()Returns the position of first occurrence of input element
isEmpty()Checks if any element is present or not inside the ArrayList
lastIndexOf()Returns the position of last occurrence of input element
listIterator()Traversing forward and reverse through elements of ArrayList
remove()remove elements from ArrayList
removeAll()remove matching elements from another ArrayList
removeRange()remove a range of elements based on position
retainAll()Retain all matching elements of the list
set()Replaces the element at given position of the ArrayList
size()Returns number of elements present inside the ArrayList
subList()Returns view of a range of elements of the ArrayList
toArray()Returns an Array from an ArrayList
trimToSize()Minimize the storage of an ArrayList

Displaying elements of an ArrayList

Using for loop and size()
ArrayList<Integer> marks = new ArrayList<Integer>(3);
marks.add(55);
marks.add(54);
marks.add(60);
	
// Using for loop and size() 
for(int i=0; i < marks.size(); i++)
System.out.println(marks.get(i)); 
By using Iterator. For using Iterator we must include iterator utility.
import java.util.Iterator;
Looping by using iterator ( marks is our ArrayList as declared above.
Iterator j = marks.iterator();
    
while (j.hasNext()) {
   System.out.println(j.next());
}


Array

Learn more about creating and passing data to an Array through class and methods
Java Tutorials


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer