0

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));
                                                  > 
0

7 Answers 7

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The thing is that i am getting a certain turtle from a list, and i want that turtles index, and the index value I want to put in a new arrayList that is named results.
But when i do like this, and when looping trough the results array and printing the values out, it doesn't say the index. This is printed out instead of a number: se.lth.cs.pt.turtle.visible.Turtle@sh2hdh37 something like that
@Jake: this is expected behaviour if you're printing out a Turtle which doesn't have an overloaded toString method, and not an integer.
hmm.. i tried the first one. It still gave me the same error. I can't think of what might be the problem...
That's because you say results is: ArrayList<Turtle> results = new ArrayList<Turtle>(); and you're trying to put an int into a results list typed with Turtle, IF you want to store ints in the results you need to change it to: ArrayList<Integer> results = new ArrayList<Integer>(); BUT if you want to put the turtle in the retults then use results.add(turtles.get(i)); in your loop
1

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;
    }

Comments

0

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.

Comments

0

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.

Comments

0

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.

6 Comments

It is really strange. This made sense and i tried it out but i'm still getting the same error, but now it points at the dot after results
Can we see the definition of "results"? If it is just a Vector, it is not going to accept an int in the add method.
Results is an arrayList created like this: ArrayList<Turtle> results = new ArrayList<Turtle>();
And the Turtles list is created like this: List<Turtle> turtles = new LinkedList<Turtle>();
Ok, the results ArrayList is an array of Turtles then. It is not geared up to accept indices (indexes?) that are ints. If you really wanted to collect ints, you could use: ArrayList results = new ArrayList(); and : results.add(new Integer(i)); Of course, you'd now have to work with Integer objects, and would need to use the "intValue" method to get an int out of the Integer.
|
0

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.

Comments

0

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

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

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.