Jake
Jake

Reputation: 123

java - get index of element in array [error]

When i am trying to receive the index of the element i get an error when compiling (cannot find symbol on the code that i have marked with comment). I have the following code:

        for (int i = 0; i < turtles.size(); i++) {

                if (turtles.get(i).getX() == 450) {

                        results.add(turtles.get(i).indexOf(i)); //this line error
                }
        }

The thing is that i want to add the index of the retrieved turtle in the loop into a new array called results. How do i do that?

The error is saying:

cannot find symbol

symbol: method indexOf(int)

location: class se.lth.cs.pt.turtle.visible.Turtle

                        results.add(turtles.get(i).indexOf(i));
                                                  > 

Upvotes: 0

Views: 10819

Answers (7)

Manmohan Soni
Manmohan Soni

Reputation: 6621

Hi I am providing a method u can use it

/**
     * Method to get the index of the given item from the list
     * @param stringArray
     * @param name
     * @return index of the item if item exists else return -1
     */
    public static int getIndexOfItemInArray(String[] stringArray, String name) {
        if (stringArray != null && stringArray.length > 0) {
            ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringArray));
            int index = list.indexOf(name);
            list.clear();
            return index;
        }
        return -1;
    }

Upvotes: 1

Jake
Jake

Reputation: 123

I solved the problem by defining the arraylist to type of integer like this:

ArrayList<Integer> results = new ArrayList<Integer>();

Upvotes: 0

Nayan Wadekar
Nayan Wadekar

Reputation: 11602

indexOf(Object) returns the index of the first occurrence of that object.

for(Turtle _turtle : turtles){

 //-- Getting index of the turtle & adding it to results

        results.add(turtles.indexOf(_turtle));
 }

Retrieving objects based on index (i.e using i etc) may sometimes result in index out of bound exceptions & also difficult to manage during manipulations.

Upvotes: 0

Satwik
Satwik

Reputation: 111

You ought to reconsider Joel's answer above (I hope it is still above!) once more. You say "I am getting a certain turtle...and want to put the index of the turle in a new array...", and it seems that the index is actually in the variable "i" already. There is no extra work needed to "fetch" it.

"results.add(i)" seems to be what you want to do.

Upvotes: 0

darioo
darioo

Reputation: 47183

You must initialize that array, if you haven't done so, before your loop. So, your code becomes:

 List results = new ArrayList();

 for (int i = 0; i < turtles.size(); i++) {

            if (turtles.get(i).getX() == 450) {

                    results.add(turtles.get(i).indexOf(i));
            }
    }

By the way, I hope you know what you're doing when you use turtles.get(i).indexOf(i), because it isn't very obvious from your code.

Edit: after seeing your edited question, I can only assume that something isn't right with your se.lth.cs.pt.turtle.visible.Turtle class. Like, it doesn't have a method named indexOf that takes an int as its parameter.

Upvotes: 0

Joel
Joel

Reputation: 30156

Without the full error & all the code I'm not entirely sure what you're trying to do, but don't you just want to do this?

 results.add(i);

or maybe this:

 results.add(turtles.get(i));

(...the index of the retrieved turtle is i)

depending on whether you expect results to contain the index of the turtle, or the turtle itself.

Upvotes: 3

B3tturTh3nU
B3tturTh3nU

Reputation: 163

I believe your problem is that indexOf takes an object as a parameter, and returns the index. If you want to find the object at index i, you would use the 'Vector.elementAt(int index)' method instead.

Upvotes: 0

Related Questions