Different between array and ArrayList-
Before we learn about Java Array to access array data structure using Java. Array have fix size (length) and fix structure they cannot grow or shrink. Before we use array, it wants to be declared. Array declaration is required array size. In this reason we must know the array size before we declare the array. In this reason array data structure make a consume sometimes when we are programming. Sometimes we want to use an array, but we cannot identify its size, Java ArrayList is implements for escape this consume.
Introduction ArrayList-
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
ArrayList is a class and it extends AbstractList and implements the list interface. ArrayList like a drain, Because when we add value, automatically ArrayList grows longer. In this reason array list is very useful for our programmings. Using ArrayList, we can shuffle values, select position and add value, change values or delete values in your choices easily.ArrayList class Constructors-
ArrayList()
ArrayList(collection a)
ArrayList(int length)
Import ArrayList Class-
import java.util.ArrayList;
Methods in ArrayList Class-
Return type | Method Name |
boolean | add(E e) Apply element end to this ArryLis. |
void | add(int index, E element) Select the special position and apply to element to this List. |
void | clear() Remove all element in this list. |
Object | clone() Show copy of ArrayList instance. |
boolean | contains(Object o)
If this list contains the specified element object returns true. |
E | get(int Index) Return element at special position. |
boolean | isEmpty() Return true when the list is empty. |
E | remove(int Index) Remove the element at specified position . |
boolean | remove(Object x) Removes the first occurrence of the specified element from this list, if it is present. |
E | set(int Index, E element) Replace the element at the special position using given element. |
int |
size() Return number of element in this list. |
Object[] |
toArray() Returns an array containing all of the elements in this list in proper sequence (from first to last element). |
ArrayList class has 20 methods. Most impotent methods only given by the above table. You want to know additional knowledge about it visit- Class ArrayList<E> --> Method Summary
Declare the ArrayList Object-
ArrayList variableName=new ArrayList();
or
ArrayList variableName=new ArrayList(length);
How to access ArrayList class-
You can access ArrayList class using the above methods given by the table.
First Example ArrayList:-
import java.util.ArrayList;
public class arraylist {
public static void main(String args[]){
ArrayList a=new ArrayList(); //declare the ArrayList
String word[]={"Apple","Mango","Graps","Mangus","Orange"};
// add element using for loop
System.out.println("Input=\n");
for(int i=0;i<word.length;i++){
a.add(word[i]);
System.out.println(i+1+")"+word[i]);
}
// pring the element using ArrayList
int temp=0;
System.out.println("\nOutput=\n");
while(a.isEmpty()==false){ //when the ArrayLit is empty
System.out.println(a.get(temp)); //print the first element in ArrayList
a.remove(temp); //Reomve the first element in list,then second element come first
}
}
}
Output-
Discussion-
- First we create the ArrayList
- Add values to the ArrayList using add(), one by one.
- Finally print the ArrayList using the while loop(You can use the clone() for print the ArrayList ).
Create the variable temp and assign it to 0, Because first index is 0.
Check the ArrayList isEmpty using isEmpty() method.
Till the isEmpty is return false while loop is working.
Print the first element using get() method.
Read word was removed using remove() method.
Continue it till the ArrayList return isEmpty() is true.
Second Example ArrayList (with all methods are accessed):-
Do you want to get complete example Java code click this button and download it free and then run arraylist.java file-
No comments:
Post a Comment