There are some ways to build the data structure in Java. One of these is the ArrayList.
ArrayList is an object, so it lives in heap area in memory. ArrayList has a diynamic construction, there is no no need to give array size.
Declaration of ArrayList
ArrayList<String> myList = new ArrayList<String>();
Most Used Methods
add(obj) : Adds new object to the array.
remove(obj) : Removes given object from the array.
size(obj) : Returns the size of array in integer type.
indexOf(obj) : Returns index number of the given in integer. If return is -1 it means object can not be found in the array.
contains(obj) : Checks whether the object is in the array. If object is in the array, method returns true.
Please follow the link to find out the official documentation about ArrayList
Usage Comparison of ArrayList and Classic Array
Declaration
ArrayList:
ArrayList<String> myList = new ArrayList<String>();
Array:
String[] myList = String[2];
Adding Item To Array
ArrayList:
String a = new String(“test”); s.add(a);
Array:
String a = new String(“test”); myList[0] = a;
Getting Array Size
ArrayList:
int size = myList.size();
Array:
int size = myList.length;
Getting Item Of Array
ArrayList:
Object o = myList.get(0);
Array:
String o = myList[0];
Removing Item From Array
ArrayList:
myList.remove(0);
Array:
myList[0] = null;
Checking Existence Of an Item In The Array
ArrayList:
boolean isIn = myList.contains(a);
Array:
boolean isIn = false; for(String item : myList){ if(a.equals(item)){ isIn = true; break; } }
Be First to Comment