If you do not know in advance how many elements you will have to process, it is better to use collections ( https://en.wikipedia.org/wiki/Java_collections_framework ). You could also create a new larger 2-dimensional array, copy the old data and paste in new elements, but the data collection environment processes this automatically.
In this case, you can use the line map in line lists:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MyClass { public static void main(String args[]) { Map<String, List<String>> shades = new HashMap<>(); ArrayList<String> shadesOfGrey = new ArrayList<>(); shadesOfGrey.add("lightgrey"); shadesOfGrey.add("dimgray"); shadesOfGrey.add("sgi gray 92"); ArrayList<String> shadesOfBlue = new ArrayList<>(); shadesOfBlue.add("dodgerblue 2"); shadesOfBlue.add("steelblue 2"); shadesOfBlue.add("powderblue"); ArrayList<String> shadesOfYellow = new ArrayList<>(); shadesOfYellow.add("yellow 1"); shadesOfYellow.add("gold 1"); shadesOfYellow.add("darkgoldenrod 1"); ArrayList<String> shadesOfRed = new ArrayList<>(); shadesOfRed.add("indianred 1"); shadesOfRed.add("firebrick 1"); shadesOfRed.add("maroon 1"); shades.put("greys", shadesOfGrey); shades.put("blues", shadesOfBlue); shades.put("yellows", shadesOfYellow); shades.put("reds", shadesOfRed); System.out.println(shades.get("greys").get(0));
source share