Java Array Lists Code Examples

Last Updated On Friday 19th Nov 2021

Java Arrays Lists Code Examples using Iterators, Using for loop, Deleting an Element from List, Removing an Element from a List, Swap a Elements in a List…

	import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
	

Using Iterator

	public class Main {
    public static void main(String[] args) {
        List < String > names = new ArrayList < String > ();

        names.add("Liam");
        names.add("Noah");
        names.add("Oliver");

        System.out.println("Using Iterator");
        Iterator < String > str = names.iterator();

        while (str.hasNext()) {
            System.out.println(str.next());
        }
    }
}
	
	// Using Iterator
// Liam
// Noah
// Oliver
	

Using For Loop

	public class Main {
    public static void main(String[] args) {
        List < String > names = new ArrayList < String > ();

        names.add("Liam");
        names.add("Noah");
        names.add("Oliver");

        System.out.println("Using forLoop");
        int x = names.size();

        for(int i = 0 ; i < x ; i++){
            System.out.println(names.get(i));
        }
    }
}
	
	// Using forLoop
// Liam
// Noah
// Oliver
	

Copying to a new List

	public class Main {
    public static void main(String[] args) {
        List < String > names = new ArrayList < String > ();

        names.add("Liam");
        names.add("Noah");
        names.add("Oliver");

        ArrayList<String> newList = new ArrayList<String>(names);

        //after copyling

        System.out.println("Copying " + newList);

    }
}
	
	// Copying [Liam, Noah, Oliver]
	

Deleting an element from a list

	public class Main {
    public static void main(String[] args) {
        List < String > names = new ArrayList < String > ();

        names.add("Liam");
        names.add("Noah");
        names.add("Oliver");

        names.remove(1);
        names.remove("Noah");

        System.out.println(names);

    }
}
	
	// [Liam, Oliver]
	

Reverse Elements in List

	public class Main {
    public static void main(String[] args) {
        List < String > names = new ArrayList < String > ();

        names.add("Liam");
        names.add("Noah");
        names.add("Oliver");

        Collections.reverse(names);
        System.out.println(names);

    }
}
	
	// [Oliver, Noah, Liam]
	

Shuffles a Elements in List

	public class Main {
    public static void main(String[] args) {
        List < String > names = new ArrayList < String > ();

        names.add("Liam");
        names.add("Noah");
        names.add("Oliver");

        Collections.shuffle(names);
        System.out.println(names);

    }
}
	
	// [Liam, Oliver, Noah]
	

Swap Elements in List

	public class Main {
    public static void main(String[] args) {
        List < String > names = new ArrayList < String > ();

        names.add("Liam");
        names.add("Noah");
        names.add("Oliver");

        Collections.swap(names, 1, 2);
        System.out.println(names);

    }
}
	
	// [Liam, Oliver, Noah]