ArrayList in java
It is resizable array also called as dynamic array.
It can remove space from array., when delete operation perform over Arraylist or we can say resize it.
It contains all ordinary array properties like indexing, maintains order, contiguous memory allocation etc.
It can be type like Integer, String etc. but, can't be primitive type.
It is not synchronized in case of threads you have to do it explicitly.
import java.util.ArrayList;
public class Main
{
public static void main(String[] args) {
ArrayList<String> list_of_datatypes = new ArrayList<String>();
list_of_datatypes.add("int");
list_of_datatypes.add("float");
list_of_datatypes.add("char");
System.out.println(list_of_datatypes); //array of values stored
//Output: [int, float, char]
list_of_datatypes.remove("int"); //to remove
System.out.println(list_of_datatypes);
//Output: [float, char]
System.out.println(list_of_datatypes.get(0)); //at index 0
//Output: float
list_of_datatypes.set(1,"int"); //to modify
System.out.println(list_of_datatypes);
//Output: [float, int]
System.out.println(list_of_datatypes.size()); // to check size
//Output: 2
}
}
Comentarios