Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - expression must be an array type but it resolved to Stack<String>

Tags:

java

I am making a program where I declared a Stack and named it "words". I used this code, like so:

Stack <String> words = new Stack<String>();

but when I tried to use this code to shuffle the words in it, an error about it being a stack appears:

Collections.shuffle(words);
int mistakes = 0;
final String CORRECT_WORD= words[0];

how can i resolve this error?

like image 915
Xelza Avatar asked Jun 27 '26 23:06

Xelza


1 Answers

If you need the first value from words, just use pop():

final String CORRECT_WORD = words.pop();

or Vector's elementAt(0).

final String CORRECT_WORD = words.elementAt(0);
like image 115
Jon Lin Avatar answered Jun 30 '26 12:06

Jon Lin