What is java.util.Arrays$ArrayList?

The java.util.Arrays$ArrayList is a nested class inside the Arrays class. It is a fixed size or immutable list backed by an array. Arrays.java public static <T> List<T> asList(T… a) { return new ArrayList<>(a); } /** * @serial include */ private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable { private static final long serialVersionUID = …

Read more

Java – How to shuffle an ArrayList

In Java, you can use Collections.shuffle to shuffle or randomize a ArrayList TestApp.java package com.mkyong.utils; import java.util.Arrays; import java.util.Collections; import java.util.List; public class TestApp { public static void main(String[] args) { List<String> list = Arrays.asList("A", "B", "C", "D", "1", "2", "3"); //before shuffle System.out.println(list); // again, same insert order System.out.println(list); // shuffle or randomize Collections.shuffle(list); …

Read more

How to loop ArrayList in Java

No nonsense, four ways to loop ArrayList in Java For loop For loop (Advance) While loop Iterator loop package com.mkyong.core; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListLoopingExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Text 1"); list.add("Text 2"); list.add("Text 3"); System.out.println("#1 normal for loop"); for (int i = …

Read more

How to sort an ArrayList in java

By default, the ArrayList’s elements are display according to the sequence it is put inside. Often times, you may need to sort the ArrayList to make it alphabetically order. In this example, it shows the use of Collections.sort(‘List’) to sort an ArrayList. import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SortArrayList{ public static void main(String …

Read more

How to initialize an ArrayList in one line

Here’s a few ways to initialize an java.util.ArrayList, see the following full example: InitArrayList.java package com.mkyong.examples; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class InitArrayList { public static void main(String[] args) { //1. Normal way List<String> list = new ArrayList<String>(); list.add("String A"); list.add("String B"); list.add("String C"); System.out.println("List 1……"); for (String temp : list) { System.out.println(temp); …

Read more