ArrayList Java:
We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it. |
The ArrayList class manages a sequence of objects whose size can change.
The ArrayList class is a generic class: ArrayList<TypeName> collects objects of the given type.
You declare an array list of strings as follows:
ArrayList<String> names = new ArrayList<String>( ); |
ArrayList Method:
- E set (int index, E element)- Replaces item at the specified index in the list with the specified element. Returns the element that was previously at the index. If the specified element is not of type E, throws a run-time exception.
- E remove (int index)- Removes and returns the element at the specified index. Elements to the right of the position index have 1 subtracted from their indices, Size of the list is decreased by 1.

import java.util.ArrayList;
public class Array_list {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> subjects = new ArrayList<String>();
subjects.add("Physics");
subjects.add("Maths");
subjects.add("Science");
subjects.add("Computer");
subjects.add("Biology");
subjects.set(0,"English");
subjects.remove(3);
for(int x=0; x<subjects.size(); x++) {
System.out.println(subjects.get(x));
}
}
}
Comments (No)