2

I need to use an array list as I don't know how many rows I will need but I know I'll need 2 columns. I'm unsure of how to create such an array list, add to both columns and read data from both columns. Both columns will contain integers.

I have seen some suggest:

ArrayList<Arraylist<Integer>> name = new ArrayList<ArrayList<Integer>>();

but I can find an explanation of how to add to both columns.

I've also seen:

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

and different variations of where and the number of square brackets. Thanks.

6 Answers6

1

Java is Object Oriented language, so why not create ArrayList<Column> ? You can create a class Column which will cover your requirements: it can have setters and getters, and if you need to support other types other than Integer you can generify it. For example:

class Column<T> {
    private T value;

    public Column(T value) {
        this.value = value;
    }

    public getValue() {
        return this.value;
    }
}

Then you declare:

List<Column<Integer>> list = new LinkedList<>();
list.add(new Column<Integer>(5));
System.out.println(list.get(0).getValue())
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

Example how create two dimension structure use lists like you to do:

   List<List<Integer>> names = new ArrayList<>();

        List<Integer> row = new ArrayList<>();

        row.add(1); // first column
        row.add(2); // second column

        names.add(row); // add row with column


        System.out.println(names.get(0).get(0)); // get first column from first row
        System.out.println(names.get(0).get(1)); // get second column form first row

But best way is use Custom object like this:

class CustomRow {

private int col1;
private int col2;

// getters and setters

}


List<CustomRow> tables;
CustomRow cr = new CustomRow();
cr.setCol1(1);
cr.setCol2(2);
tables.add(cr);
0

You could try

  • a Map and use the key and the value to hold values
  • a List of tuples
  • a List of Lists as you suggested

You didn't give enough information as to what you actually want to do, but if you have to use a List as a base, I'd typically go with a List of custom Tuple objects, each holding two values.

Andreas Hartmann
  • 1,825
  • 4
  • 23
  • 36
  • I want to use the list to store integers, the first column would store an index from one ArrayList and the second column would store an index from a second ArrayList. It will be a Many-To-Many relationship for the integers in the columns. Thank you. –  Dec 03 '17 at 20:44
  • In that case just create a List of Tuples. The Tuples are just classes the hold two integers each and you could call it indices and the fields could be integer x and integer y. For convenience add a constructor and you are done. Consider making the fields final if you want it to be unchangable. – Andreas Hartmann Dec 03 '17 at 20:48
0

Something like this:

public MyObject {
   Integer integer1;
   Integer integer2;
}


List<MyObject> myObjList = new ArrayList<>();

MyObject mo = new MyObject(){
...

myObjList.add(mo);
sinclair
  • 2,812
  • 4
  • 24
  • 53
0

You can try creating a simple POJO class called Row and have two variables as column1 and column2. Then add this Row object to your list.

Santosh
  • 874
  • 11
  • 21
-1

You basically need to create an ArrayList that holds an ArrayList of type Integer. You can then add two of these ArrayLists into the main array list.

List<List<Integer>> myList = new ArrayList<>();
List<Integer> x = new ArrayList<>();
x.add(5);
x.add(6);

List<Integer> y = new ArrayList<>();
y.add(5);
y.add(6);

myList.add(x);
myList.add(y);

Based off this answer:

How do I declare a 2D String arraylist?

xiimoss
  • 775
  • 3
  • 11
  • 21