Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ArrayList<String[]>

I would like to create a 2D array named employees where I know the number of columns (fixed 5 and represent employee's data) but number of rows is somewhat dynamic. So I did something like this:

static ArrayList<String[]> employees = new ArrayList<String[]>();

I figured how to add employees but I can't figure out how to get a specific data out of one employee only, using .get() for example.

like image 421
Cake Avatar asked Oct 30 '25 11:10

Cake


2 Answers

get would return a row, which is a String[]. If you want a specific value from it, you'll have to subscript it using the [] operator:

String specificData = employees.get(1)[2]; // Or any other indexes
like image 104
Mureinik Avatar answered Nov 01 '25 02:11

Mureinik


the first dimension which is any ArrayList you have to call by get() method to access and then the returned value is an array which you cann access by index:

ArrayList<String[]> employees = new ArrayList<String[]>();
employees.add(new String[] {"a", "b"});
System.out.println(employees.get(0)[0]);
like image 33
Mustafa Poya Avatar answered Nov 01 '25 00:11

Mustafa Poya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!