arraylist of arraylist in java code example
Example 1: Lists inside lists in java
public class GuiTest {
public static void main(String[] args) {
List<List<Integer>> MainList = new ArrayList<List<Integer>>();
Random NewRandomNumber = new Random();
for (int i = 0; i < 10; i++) {
List<Integer> SecondList = new ArrayList<Integer>();
MainList.add(SecondList);
for (int i2 = 0; i2 < 10; i2++) {
SecondList.add(NewRandomNumber.nextInt(6));
}
}
Example 2: java arraylist
import java.util.List; //list abstract class
import java.util.ArrayList; //arraylist class
//Object Lists
List l = new ArrayList();
ArrayList a = new ArrayList();
//Specialized List
List<String> l = new ArrayList<String>();
ArrayList<Integer> a = new ArrayList<Integer>();
//only reference data types allowed in brackets <>
//Initial Capacity
List<Double> l = new ArrayList<Double>(5);
//list will start with a capacity of 5
//saves allocation times
Example 3: arraylist in java
import java.util.ArrayList;
public class ArrayListExample
{
public static void main(String[] args)
{
int num = 14;
// declaring ArrayList with initial size num
ArrayList<Integer> al = new ArrayList<Integer>(num);
// append new element at the end of list
for(int a = 1; a <= num; a++)
{
al.add(a);
}
System.out.println(al);
// remove element at index 7
al.remove(7);
// print ArrayList after deletion
System.out.println(al);
// print elements one by one
for(int a = 0; a < al.size(); a++)
{
System.out.print(al.get(a) + " ");
}
}
}
Example 4: arraylist java methds
import java.util.ArrayList;
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(0);
myList.remove(0);//Remove at index 0
myList.size();
myList.get(0);//Return element at index 0
Example 5: arraylist of arraylist
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> outer = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> inner = new ArrayList<Integer>();
inner.add(100);
inner.add(200);
outer.add(inner); // add first list
inner = new ArrayList<Integer>(inner); // create a new inner list that has the same content as
// the original inner list
outer.add(inner); // add second list
outer.get(0).add(300); // changes only the first inner list
System.out.println(outer);
}
Example 6: arraylist in java
Size is dynamic
Only supports objects
not syncronized
array based class