3

Here is how my code is set up:

public String[] function(List<String[]> playerList){
i = 5
return playerList[i];

When I try to run this code, I get the error:

Array type expected; found: 'java.util.List<java.lang.String[]>'

I know I'm screwing something up by mixing my arrays and lists, what would be the right way of fixing this code, assuming I can't change my list input to an array?

4 Answers 4

5

You access an element at an index in a List with a call to List.get(int index) (not [], that is accessing an element in an array). Like,

return playerList.get(i);
Sign up to request clarification or add additional context in comments.

Comments

0

not like c++, which can do operator overloading.

in java, playerList is a list, you cannot apply "[]" to it, but the list's element/item is an Array type (String[]). so, you should use list.get() and with it's element, you could use [] operator: (playerList.get(i))[0]

Comments

0

playerList is List type, for accessing element in the List you must call get function.
for example >> playerList.get(0) , then you will get element's value in index-0. In this case, you will return String of Array. And if you want accessing that Array element, you can use playerList.get(0)[i]

Comments

0

So, you were trying to get an array of strings however, there could be two (2) things you wanted to take a look. First, you might want to specify playerList as an ArrayList<String> and then(second), you are trying to return playerList[i] as string and not as an array of string as specified String[].

public String[] function(List<String[]> playerList){
  i = 5
  return playerList[i];

You might want to try this instead?

return playerList.toArray(new String[playerList.size()]);

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.