how to use array lists in java code example

Example 1: how to create an array list in java

List<Integer> list = new ArrayList<>();

Example 2: arrays lists java

ArrayList: part of Collections        
does not support primitives, only support none primitives
size is dynamic, automatically adjusted
has index numbers
ArrayList <DataType>  listName = new ArrayList <DataType> ();
methods:
add(): adds Objects to the arraylist
get(index): gets the object at the given index, returns the object as it is
size(): returns the length (size) of the arraylist as an int
add(Object): adds objects to the arraylist
add(index, Object): adds the object at the given index
set(index, Object):  replacing the original object at given index with the new given object
remove(int index): object at the given index will be removed. ONLY one
 remove(Object): given object will be removed, returns boolean. ONLY one
 clear(): remove everything from arraylist, size will be 0
indexOf(Object): returns the index number of the object, int
contains(Object): returns boolean 
equals(ArrayListName): compares two arrayList 
isEmpty(): returns boolean, depeding on the size

Data Structures:
		1. Array  ==>    Arrays (java.util)
		2. Collection ==> Collections (java.util), does not support primitive
		3. Maps  ==> does not support primitive
 
Methods:
Bulk Operations:
containsAll(CollectionType): verifies if all objects in CollectionType are contained in the list or not, returns boolean
 
addAll(CollectionType): adds multiple objects, adds all the objects from given collection type
 
removeAll(CollectionType): removes multiple objetcs, removes all the matching objects
 
retainAll(CollectionType): removes all the unmatching objects
					{1,2,3,4,5,6,7,1,2,3,4}
					removeAll(1,2,3,4) ==> {5,6,7}
					retainAll(1,2,3,4) ==> {1,2,3,4,1,2,3,4 }
 
             Arrays.asList(object1, object2 ..): returns the collection type (List)
 
 ArrayList<Integer> numList = new ArrayList<>(CollectionType);
 
 sorting arrayList:
	Collections.sort(ArrayListName); ==> Ascending order
 
	Collections: presented in "java.util" package
		import PakageName.Classname;
		import java.util.Collections;
 
  
Collections Class:
sort(CollectionType): sorting any given collectionType
 
frequency(CollectionType, Object): returns the frequency of the given object from the given collectionType
 
max(CollectionType): return the max object from collectiontype
 
min(CollectionType): return the min object from collectiontype
 
swap(CollectionType, index1, index2): swaps the elemnts at the given indexs from the CollectionType
				list: {1,2,3,4,5}
				Collections.swap(list, 1, 2); ==> {1,3,2,4,5}
 
replaceAll(CollectionType, oldValue, newValue):
				list: {1,1,1,2,3,4,5}
				Collections.replaceAll(list, 1, 10); ==>{10,10,10,2,3,4,5}

Predicate:  can be applied to any collection-Type
		number % 2 != 0
 
		Predicate<Ineteger>  oddNumber =  p -> p %2 != 0;
 
ArrayLisst method:
remove If(Predicate): removes everything that's matching with the expression of predicate

Example 3: list of arrays java

List<int[]> A = new List<int[]>();

Tags:

Java Example