Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an array from a list [Java]

Tags:

java

arrays

list

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?

like image 541
NBC Avatar asked Sep 17 '26 22:09

NBC


1 Answers

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);
like image 117
Elliott Frisch Avatar answered Sep 20 '26 20:09

Elliott Frisch



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!