Constructors in Java ArrayList
ArrayList(): This constructor is used to build an empty array list
ArrayList(Collection c): This constructor is used to build an array list initialized with the elements from collection c
ArrayList(int capacity): This constructor is used to build an array list with initial capacity being specified
Methods in Java ArrayList
void clear() :This method is used to remove all the elements from any list.
void add(int index, Object element): This method is used to insert a specific element at a specific position index in a list.
void trimToSize(): This method is used to trim the capacity of the instance of the ArrayLis to the list’s current size.
int indexOf(Object O): The index the first occurrence of a specific element is either returned, or -1 in case the element is not in the list.
int lastIndexOf(Object O): The index the last occurrence of a specific element is either returned, or -1 in case the element is not in the list.
Object clone(): This method is used to return a shallow copy of an ArrayList.
Object[] toArray(): This method is used to return an array containing all of the elements in the list in correct order.
Object[] toArray(Object[] O): It is also used to return an array containing all of the elements in this list in the correct order same as the previous method.
boolean addAll(Collection C): This method is used to append all the elements from a specific collection to the end of the mentioned list, in such a order that the values are returned by the specified collection’s iterator.
boolean add(Object o): This method is used to append a specificd element to the end of a list.
boolean addAll(int index, Collection C): Used to insert all of the elements starting at the specified position from a specific collection into the mentioned list.
CODING:ArrayList(): This constructor is used to build an empty array list
ArrayList(Collection c): This constructor is used to build an array list initialized with the elements from collection c
ArrayList(int capacity): This constructor is used to build an array list with initial capacity being specified
Methods in Java ArrayList
void clear() :This method is used to remove all the elements from any list.
void add(int index, Object element): This method is used to insert a specific element at a specific position index in a list.
void trimToSize(): This method is used to trim the capacity of the instance of the ArrayLis to the list’s current size.
int indexOf(Object O): The index the first occurrence of a specific element is either returned, or -1 in case the element is not in the list.
int lastIndexOf(Object O): The index the last occurrence of a specific element is either returned, or -1 in case the element is not in the list.
Object clone(): This method is used to return a shallow copy of an ArrayList.
Object[] toArray(): This method is used to return an array containing all of the elements in the list in correct order.
Object[] toArray(Object[] O): It is also used to return an array containing all of the elements in this list in the correct order same as the previous method.
boolean addAll(Collection C): This method is used to append all the elements from a specific collection to the end of the mentioned list, in such a order that the values are returned by the specified collection’s iterator.
boolean add(Object o): This method is used to append a specificd element to the end of a list.
boolean addAll(int index, Collection C): Used to insert all of the elements starting at the specified position from a specific collection into the mentioned list.
import java.util.ArrayList;
import java.util.Iterator;
public class arraylist {
public static void main(String[] args){
ArrayList<String> o=new ArrayList<>();
o.add("Earth");
o.add("Pluto");
o.add("Sun");
o.set(1,"Moon");
System.out.println("Index "+o.indexOf("Sun"));
System.out.println("Get "+ o.get(2));
Iterator<String> ans=o.iterator();
while(ans.hasNext()){
System.out.println(ans.next());
}
}
}
import java.util.Iterator;
public class arraylist {
public static void main(String[] args){
ArrayList<String> o=new ArrayList<>();
o.add("Earth");
o.add("Pluto");
o.add("Sun");
o.set(1,"Moon");
System.out.println("Index "+o.indexOf("Sun"));
System.out.println("Get "+ o.get(2));
Iterator<String> ans=o.iterator();
while(ans.hasNext()){
System.out.println(ans.next());
}
}
}
Comments
Post a Comment