Comparing int and String from LinkedList

Go To StackoverFlow.com

1

I've got a problem.

I'm trying to compare a String and a int but can't seem to get working. What am I doing wrong?

Getting this from Eclipse:

The type of the expression must be an array type but it resolved to List

    int numberOfMoves;
    List<String> highscoreLinkedList = new LinkedList<String>();

    if (moves < Integer.parseInt(highscoreLinkedList[2])){
      highscoreLinkedList[2] = Integer.toString(moves);
      highscoreLinkedList[1] = name;
    }

This is for a highscore textfile for a game I'm making. The String at index 2 is a number of moves and the int moves is also a number of moves.

2012-04-04 19:54
by user1258829
Well, what is it telling you you're doing wrong - Brian Roach 2012-04-04 19:57
Oops, forgot about that. Edited now - user1258829 2012-04-04 19:59


3

You cannot access a list element using highscoreLinkedList[2] - that syntax is reserved for arrays. To access a list you have to use the get() method, i.e. highscoreLinkedList.get(2)

2012-04-04 19:56
by Michael Borgwardt
Oh my god, I'm so stupid. Totaly mixed Array and LinkedList up... stupid stupid stupid! Thanks - user1258829 2012-04-04 20:01
This doesn't address the setting problem.. - Jon Egeland 2012-04-04 20:02


1

You are trying to treat list as an array, but the only way to access elements of the is through calling get() method. Your code does not compile.

2012-04-04 19:57
by ahanin
Oh my god, I'm so stupid. Totaly mixed Array and LinkedList up... stupid stupid stupid! Thanks - user1258829 2012-04-04 20:02
By the way, random-accessing LinkedList's elements is not efficient. Consider using ArrayList or array - ahanin 2012-04-04 20:06


1

Lists don't work the same way as arrays in Java. To access a certain element, you have to use the get() method, and to get the element, you need to use set(), like so:

// you have highscoreLinkedList[2], it should be:
highscoreLinkedList.get(2);

// you have highscoreLinkedList[2] = ..., it should be:
highscoreLinkedList.set(2, Integer.toString(moves));

You can see all of the methods for LinkedList here.

2012-04-04 19:59
by Jon Egeland
Oh my god, I'm so stupid. Totaly mixed Array and LinkedList up... stupid stupid stupid! Thanks - user1258829 2012-04-04 20:01
Ads