简体   繁体   中英

How can I create ArrayList of ArrayList in Java?

I am writing a Java code. I want to create something that is similar to extendable matrix or vector. So it can be able to extend when a new varible arise even in each direction. Such as;

Path[1] = a
Path[2] = a,b
Path[3] = a,b,c
Path[4] = a,.........
......
......

How can I create and use it in the code?

You can create an ArrayList of ArrayList of Integer[]:

ArrayList<ArrayList<Integer[]>> path = new ArrayList<>();

Then, to get the data (referring to your example):

ArrayList<Integer[]> path1 = path.get(0);
Integer[] a = path1.get(0);
ArrayList<Integer[]> path2 = path.get(1);
Integer[] a2 = path1.get(0);
Integer[] b2 = path1.get(0);

or by using for-each:

for(ArrayList<Integer[]> list : path) {
        for(Integer[] values : list) {
            for(Integer value : values) {
                System.out.println(value);
            }
        }
    }

To get a specific value:

int value = path.get(0).get(0)[0];

To edit a value:

path.get(0).get(0)[0] = 12;

So you want to create an array list of array lists? Right, let's see how we can create one of strings:

ArrayList<String> array = new ArrayList<> ();

So you can do this to create an array list of array lists:

ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>> ();

Then you can add a row to the matrix like this:

ArrayList<String> row1 = new ArrayList<> ();
row1.add ("stuff1");
row1.add ("stuff2");
row1.add ("stuff3");
row1.add ("stuff4");
row1.add ("stuff5");
list.add (row1);

You can access it like this:

list.get(0).get(3)

And you can loop through it using the enhanced for loop like this:

for (ArrayList<String> oneList : list) {
    for (String s : oneList) {
    }
}

If you want to expand it you just get the corresponding row and call the add method on that dimension.

I'd suggest you either create a new class for this Matrix, or use an existing one (did you google it?). That way you can define access methods as needed and keep the code self-contained, reusable, and testable.

public class MyMatrix<T> {
    private ArrayList<ArrayList<T>> data;

    public MyMatrix() {
        data = new ArrayList<ArrayList<>>();
    }

    // add methods like the ones below, depending on what you need:

    public void set(T t, int x, int y) {
        // exercises for the reader:
        // - check bounds (x and y > 0)
        // - check that row x exists, if not add rows
        // - check that column y exists, if not, add columns in target row
        // - if matrix needs to be flush (i.e., all lines need the same number of columns, extend all columns as needed
        data.get(x).set(y, t);
    }

    T get(int x, int y) {
         // todo: sanity check x and y
         return data.get(x).get(y);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM