First you have to import the java arraylist. This is done by:

import java.util.ArrayList;

Then you can create the arraylist

One of the best things about arraylists is that you can add and remove elements from the arraylist after you initialize it. This means that the length of the arraylist can change after you create it, unlike in arrays.

import java.util.ArrayList; // import the ArrayList class

ArrayList<String> names = new ArrayList<String>();
names.add("Rohan");//adding values to list
names.add("Adi");
names.add("Soma");
names.add("Jazair");
System.out.println("Number of elements: " +names.size());//prints the number of elements in the arraylist

for(int i = 0;i<names.size();i++){
System.out.println(names.get(i));//printing all values in list using for loop
}
Number of elements: 4
Rohan
Adi
Soma
Jazair

Accessing Elements

To access an element in an arraylist, you use the get() method. Inside the method, put the index of element that you are trying to access(remember the arraylist starts at index 0).

ArrayList<Integer> nums = new ArrayList<Integer>();//making list

for(int i = 0;i<10;i++){
    nums.add(i);//adding value
    }


for(int i = 0;i<nums.size();i++){
    nums.set(i,i*i);//cool way of squaring all the values. 
    System.out.println(nums.get(i));//printing these values
}
0
1
4
9
16
25
36
49
64
81

Modifying Elements Inside an Arraylist

As shown above in the code block, to modify or change code from elements that have already been set, use the set() method. The set method takes two parameters. The first parameter is the index that you are trying to modify. The second parameter is the value that you are giving the index in the first parameter

//Examples:
nums.set(0,100);
names.set(1,"John");
letters.set(3,'D');

Removing and Clearing Elements

ArrayList<Integer> nums = new ArrayList<Integer>();

for(int i = 0;i<10;i++){
    nums.add(i);
    }
System.out.println(nums.size());
nums.remove(8);
System.out.println(nums.size());//new size after removing value
System.out.println(names.isEmpty());//checks if list is empty after 
nums.clear();//clears list
System.out.println(nums.size());
System.out.println(nums.isEmpty());//list is now empty
10
9
false
0
true

Arraylist Searching Methods

You can also use methods to search through arraylists and find certain elements. The indexOf() method returns the first occurrence of the given element or -1 if it doesn't exist. The lastIndexOf() returns the last occurrence of the element or -1 if it's not in the list. Another method that searches through the list is the contains() method.

ArrayList<String> names = new ArrayList<String>();//creating list

names.add("Rohan");//adding elements to list
names.add("Adi");
names.add("Soma");
names.add("Jazair");
names.add("John");
names.add("Rohan");

//testing indexOf() and lastIndexOf()
System.out.println("Testing indexOf() and lastIndexOf() ");
System.out.println(names.indexOf("Rohan"));
System.out.println(names.lastIndexOf("Rohan"));
System.out.println(names.indexOf("Billy"));


//testing contains
System.out.println("Testing contains");
System.out.println(names.contains("Rohan"));
System.out.println(names.contains("Billy"));
Testing indexOf() and lastIndexOf() 
0
5
-1
Testing contains
true
false

Sorting Elements of Arraylist

One way and probably the easiest way to sort an arraylist is to use bubble sort. Bubble sort swaps the elements if they aren't in the right order, by iterating through every element one at a time. It isn't the most efficient algorithm, but it works.

ArrayList<Integer> nums = new ArrayList<Integer>();//creating arraylsit
nums.add(1);//adding elements to array
nums.add(6);
nums.add(8);
nums.add(4);
nums.add(36);
nums.add(2);
nums.add(89);
nums.add(100);
nums.add(4);

System.out.println("Unsorted Arraylist");
for(int i = 0;i<nums.size();i++){//printing unsorted elements
    System.out.print(nums.get(i)+" ");
}

//bubble sort algorithm

boolean isInOrder = false;
while (isInOrder == false) {
    isInOrder = true;
    for (int i = 0; i < nums.size() - 1; i++) {
        if (nums.get(i + 1) < nums.get(i)) {
            int temp = nums.get(i);
            nums.set(i,nums.get(i + 1));
            nums.set(i+1,temp);
            isInOrder = false;
        }
    }
}



System.out.println("\nSorted Arraylist");
for(int i = 0;i<nums.size();i++){//printing arraylist again, but sorted this time
    System.out.print(nums.get(i)+" ");
}
Unsorted Arraylist
1 6 8 4 36 2 89 100 4 
Sorted Arraylist
1 2 4 4 6 8 36 89 100 

Homework

import java.util.Random;
import java.util.ArrayList; // import the ArrayList class

public class HomeWork1{
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Random rand = new Random();

    void run(){
        for(int i = 0; i<10;i++){
            int x = rand.nextInt(100);
            numbers.add(x);
        }
         //descending order then reverse first and last element
        System.out.println("Before operations: "+ numbers);
        Collections.sort(numbers, Collections.reverseOrder()); 
        int temp = numbers.get(0);
        numbers.set(0,numbers.get(numbers.size()-1));
        numbers.set(numbers.size()-1,temp);
        System.out.println("After operation: "+ numbers);
    }

    public static void main(String[] args) {
        HomeWork1 hw1 = new HomeWork1();
        hw1.run();
    }
}
HomeWork1.main(null);
Before operations: [18, 48, 47, 0, 65, 22, 85, 94, 67, 84]
After operation: [0, 85, 84, 67, 65, 48, 47, 22, 18, 94]
import java.util.Random;
import java.util.ArrayList; // import the ArrayList class

public class HomeWork2{
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Random rand = new Random();

    void run(){
        for(int i = 0; i<10;i++){
            int x = rand.nextInt(100);
            numbers.add(x);
        }
        
        //hashmaps before and after sort
        System.out.println("Hash map before operation: "+ numbers.hashCode());
        Collections.sort(numbers);
        System.out.println("Hash map after operation: "+ numbers.hashCode());


    }

    public static void main(String[] args) {
        HomeWork2 hw2 = new HomeWork2();
        hw2.run();
    }
}
HomeWork2.main(null);
Hash map before operation: -534237819
Hash map after operation: 1703608345
import java.util.Random;
import java.util.ArrayList; // import the ArrayList class

public class HomeWork3{
    ArrayList<Integer> numbers1 = new ArrayList<Integer>();
    ArrayList<Integer> numbers2 = new ArrayList<Integer>();
    Random rand = new Random();

    void run(){
        //adding numbers to array 1
        for(int i = 0; i<10;i++){
            int x = rand.nextInt(100);
            numbers1.add(x);
        }

        //adding numbers to array 2
        for(int i = 0; i<10;i++){
            int x = rand.nextInt(100);
            numbers2.add(x);
        }
        
         //replace three elements, then reverse order
        System.out.println("Before operations, Arraylist 1: "+ numbers1);
        System.out.println("Before operations, Arraylist 2: "+ numbers2);
        //randomly swap elements from lists three times
        for(int i = 0; i<3;i++){
            int replace = rand.nextInt(10);
            numbers1.set(replace,numbers2.get(replace));
        }
        //reverse order of array 1
        Collections.sort(numbers1, Collections.reverseOrder()); 

        System.out.println("After operations, Arraylist 1: "+ numbers1);
        System.out.println("After operations, Arraylist 2: "+ numbers2);
    }

    public static void main(String[] args) {
        HomeWork3 hw3 = new HomeWork3();
        hw3.run();
    }
}
HomeWork3.main(null);
Before operations, Arraylist 1: [15, 58, 89, 69, 39, 85, 2, 86, 5, 56]
Before operations, Arraylist 2: [96, 17, 49, 7, 0, 77, 34, 43, 96, 83]
After operations, Arraylist 1: [86, 85, 69, 56, 49, 39, 34, 17, 15, 5]
After operations, Arraylist 2: [96, 17, 49, 7, 0, 77, 34, 43, 96, 83]