Skip to content
Advertisement

Java: Arraylist of Array

I want to make an arraylist of an array of 2 elements. So, I have unknown rows and known columns (i.e. 2). E.g. [{name1, ID1}, {name2, ID2}, …]

I also have to return this arraylist.

I tried using

ArrayList<arr> alist = new ArrayList<arr>();

but don’t know how to proceed.

Please advise.

Advertisement

Answer

When you define a ArrayList must use a class. In this case, you can use a class Person:

class Person {

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Then, we can define a ArrayList of Persons:

ArrayList<Person> array = new ArrayList<Person>();
array.get(0).getId();
array.get(0).getName();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement