Definition

An ArrayList is like an array that has more flexibility, so it can be resized and things can be added and removed after the array is created. No size limit, and it can have duplicate elements.

import java.util.ArrayList;

  ArrayList<String> matches = new ArrayList<String>();
  matches.add("barcelona"); 
  matches.add("beat"); 
  matches.add("real madrid"); 
  matches.add("4-0"); 
  matches.add("last"); 
  matches.add("year"); 

  System.out.println(matches);
[barcelona, beat, real madrid, 4-0, last, year]

Add and addAll

Adding elements using add(). Can also be added at a certain index if specified. addAll() is used to add all the elements from one array to another.

matches.add("."); //adds the ! to the end of the arraylist
matches.add(1, "easily"); //adds the word very to the list at index 4
System.out.println(matches);
[barcelona, easily, beat, real madrid, 4-0, last, year, .]

Size

This returns the size of the list by using size().

ArrayList<String> more_matches = new ArrayList<String>();
more_matches.add("barcelona");
more_matches.add("lost");
more_matches.add("to");
more_matches.add("liverpool");
more_matches.add("4-3"); //this is very true

matches.addAll(more_matches); //adds the second list to the first one
System.out.println(matches);

matches.size();
[barcelona, easily, beat, real madrid, 4-0, last, year, ., barcelona, lost, to, liverpool, 4-3]
13

Remove (int index) and (element)

This method remove(), removes an element from the index, it shifts the other elements and decreases index by the amount of elements removed. Can be used by removing the index number or the name of the element.

matches.remove(1); //you can see that the word "are" has been removed
System.out.println(matches);
[barcelona, beat, real madrid, 4-0, last, year, ., barcelona, lost, to, liverpool, 4-3]
matches.remove("4-3");
matches.remove("lost"); 
System.out.println(matches);
[barcelona, beat, real madrid, 4-0, last, year, ., barcelona, to, liverpool]

Get

The get() method returns elements at the specific index.

matches.get(0);
barcelona

Set

The set() method replaces an element at an index with another element

matches.set(3, new String("CF"));
System.out.println(matches);
[barcelona, beat, real madrid, CF, last, year, ., barcelona, to, liverpool]

IndexOf

The indexOf() method returns when the element occurs first or -1 if its not there.

matches.indexOf("barcelona");
0

lastIndexOf(element)

This method returns the index of when the element comes up last, or -1 if its not in the list.

matches.indexOf("chelsea");
-1

hashCode()

Returns the hashcode value of a list, a hashcode is an integer value that corresponds with an object.

matches.hashCode();
-1011032613

isEmpty

This method checks if the list is empty or not, and returns true or false if it is empty or not respectively.

matches.isEmpty();
false
more_matches.isEmpty();
false

contains(element)

This checks if the list has a certain element, true if it does and false if it doesn't.

matches.contains("real madrid");
true
matches.contains("inter");
false

Sort

This method sorts the elements of a list based on a comparator.

import java.util.Collections;

System.out.println("List : "+matches);

//will sort the string according to alphabetical order
Collections.sort(matches);
System.out.println("Sorted List : "+matches);
List : [barcelona, beat, real madrid, CF, last, year, ., barcelona, to, liverpool]
Sorted List : [., CF, barcelona, barcelona, beat, last, liverpool, real madrid, to, year]
matches.clear(); //clears all the elements in list 
System.out.println(matches); //they're all gone, how sad
[]