4

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.

2
  • employees.get(row)[column] should work. eG employees.get(0)[1] Commented Nov 17, 2020 at 10:39
  • 4
    Though it's probably easier to create a class Employee rather than use a String[]. Commented Nov 17, 2020 at 10:40

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

Comments

2

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.