Syarx Syarx - 1 year ago 46
Java Question

Java - ArrayList with multiple int per index

for example I have
int a =1;
int b = 2;

How can I add both values to an index of my arraylist and the be able to read them.(I need to do this in the same class where the list is)

For example with a pseudo-code i need to be able to do

Arraylist<integer> mylist = new Arraylist<integer> ;

int c = mylist.getnode(1).a //gets the value of a in node 1
int d = mylist.getnode(1).b //gets the value of b in node 1

Answer Source

Create a class that contains a and b. Now create a list containing instances of that class.

class Tuple {
public int a, b;
ArrayList<Tuple> mylist = new ArrayList<>();
int a = mylist.get(0).a;