I'm using ArrayList. But the problem is that the indexOf function is not working in a for loop. It's showing 0 for all the elements or objects.
Code goes like that for a FOR loop :

for(or_data or : counts.or_gates)
            {
                if (or.isHit(x, y))
                {
                    mutual=counts.or_gates.indexOf(or);
                    set_move_cursor();
                    or.addX(dx);
                    or.addY(dy);
                    System.out.println("Got the Hit"+mutual);
                    repaint();
                    break;
                }
           }

or_data is a ArrayList but mutual comes out to be 0 all the time. Any suggestion on that?

Are you confident about the isHit method? As long as isHit returns true for the first element in your list, this code will always set mutual = 0 and exit the loop

Whole snipped is confusing, OP absolutely abuses coding standards, plus we have no idea what are these objects "or_data, counts" or what is "isHit(x, y)" doing or if it is doing it job correctly.

Are you confident about the isHit method? As long as isHit returns true for the first element in your list, this code will always set mutual = 0 and exit the loop

I'm pretty sure about isHit method. It's working properly. Even if I omit the "break" then also 0 is only the output.

public boolean isHit(float x, float y)
    {
        myarea.setRect(xpos+30, ypos-20, 30, 40);
        if (myarea.contains(x, y))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Whole snipped is confusing, OP absolutely abuses coding standards, plus we have no idea what are these objects "or_data, counts" or what is "isHit(x, y)" doing or if it is doing it job correctly.

or_data:

public class or_data extends Rectangle2D.Float
{
    public int xpos,ypos;
    
    Rectangle2D.Float myarea= new Rectangle2D.Float();
    
    public boolean isHit(float x, float y)
    {
        myarea.setRect(xpos+30, ypos-20, 30, 40);
        if (myarea.contains(x, y))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void addX(float x)
    {
        this.xpos += x;
    }

    public void addY(float y)
    {
        this.ypos += y;
    }
}

counts:

public class counts
{
    public static ArrayList<or_data> or_gates=new ArrayList<or_data>();
    public static Vector<line_data> lines=new Vector<line_data>();
    
    public static void reset()
    {
        or_gates.clear();
        lines.clear();
    }
}

Sorry, but all these code fragments, without the actual data, don't really help.
I suggest you put a few print statements into your for loop to confirm the actual contents of the list and all the working values as the loop is iterated.

Not sure if the "iterator" is the problem for you. How about modify your code to use a simple loop instead of the "iterator"? You want the index of "or" which is the same as iterating through a loop using real integer.

// from
for(or_data or : counts.or_gates) {
  ...
  mutual = counts.or_gates.indexOf(or);
  ...
}

//to
or_data or;
for(int i=0; i<counts.orgates.size(); i++) {
  or = counts.orgates.get(i);
  ...
  mutual = i;
  ...
}

Edited 5 Years Ago by Taywin: n/a

Not sure if the "iterator" is the problem for you. How about modify your code to use a simple loop instead of the "iterator"? You want the index of "or" which is the same as iterating through a loop using real integer.

// from
for(or_data or : counts.or_gates) {
  ...
  mutual = counts.or_gates.indexOf(or);
  ...
}

//to
or_data or;
for(int i=0; i<counts.orgates.size(); i++) {
  or = counts.orgates.get(i);
  ...
  mutual = i;
  ...
}

Thnx Taywin. It's worked. I've used a simple Interator. But can you suggest anything about why the indexOf method isn't working. Then I can know about that also. Thnx once again.

My guess is that the indexOf() method is counting the position starting from whatever position the "iterator" returns. Because you are using the first item returned by the "iterator." the value of indexOf() is always 0 (the first position). This could be an intention for indexOf() method? I don't know...

By the way, please mark it as solved.

Edited 5 Years Ago by Taywin: n/a

My guess is that the indexOf() method is counting the position starting from whatever position the "iterator" returns. Because you are using the first item returned by the "iterator." the value of indexOf() is always 0 (the first position). This could be an intention for indexOf() method? I don't know...

By the way, please mark it as solved.

So, what's the point of using indexOf method or what's the point of giving this kind of method? Is there any other method which can directly give me the index?

I was going to mark it as solved but as my thread title is about getting a solution of the indexOf method of ArraList, so I've not marked it. Okay, I'm marking it. But please look that matter also. Thnkx.

The indexOf() method is a good method to use without calling iterator. However, you need to apply the method correctly for your own use. The API doc describes what to expect from using it; however, it fails to explain about the "iterator" situation.

ArrayList<String> a = new ArrayList<String>();
a.add("Y"); a.add("A"); a.add("A"); a.add("R"); a.add("R");  // store "YAARR"
// You cannot get the 2nd appearance of "A" using indexOf() because it always returns
// the first occurrence found
System.out.println(a.indexOf("A")); // print 1
// But it is a useful method if you want to randomly check for
// object that may be in the list without going through the whole list
if (a.indexOf("R")>=0) {
  System.out.println("Contains 'R'");
}
else {
  System.out.println("No 'R'");
}

Edited 5 Years Ago by Taywin: n/a

The indexOf() method is a good method to use without calling iterator. However, you need to apply the method correctly for your own use. The API doc describes what to expect from using it; however, it fails to explain about the "iterator" situation.

ArrayList<String> a = new ArrayList<String>();
a.add("Y"); a.add("A"); a.add("A"); a.add("R"); a.add("R");  // store "YAARR"
// You cannot get the 2nd appearance of "A" using indexOf() because it always returns
// the first occurrence found
System.out.println(a.indexOf("A")); // print 1
// But it is a useful method if you want to randomly check for
// object that may be in the list without going through the whole list
if (a.indexOf("R")>=0) {
  System.out.println("Contains 'R'");
}
else {
  System.out.println("No 'R'");
}

Ya. I know this thing. but I understand that somehow it's not working for me in this code.

@soham you should drop your "counts" class it is not helpful, have no specific value to existing code beside adding confusing complexity.

@soham you should drop your "counts" class it is not helpful, have no specific value to existing code beside adding confusing complexity.

Actually It keeps a track of how many OR_Gates and Lines are there. So, I kept it.

Actually these two can be used as two nice unobscured variables that are very easy to comprehend. Nevertheless it is your code, your project, no need to listen to good intended advice

Ya. I know this thing. but I understand that somehow it's not working for me in this code.

Great. :) So there is nothing wrong with the indexOf() method. It is just that you use the method differently and get unexpected result because of missing documentation.

I think it is a good lesson to learn about implementing and documenting a class which are used by others. Sometimes, you, who is the class implementor, do not expect others to use your class in such a way that could give unexpected results. As a result, you do not document it. In this case, I guess that the ArrayList implementor does not expect others to use it the way you do here, so there is no documentation about it.

Edited 5 Years Ago by Taywin: n/a

Hi Taywin.
Forgive me if this is a dumb question, but what exactly is the special problem with iterators and indexOf that's not documented? All I can see here is that indexOf only returns the index of the first occurrence, whether that's being done in an iterator or not, which is clearly documented:

public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list...

(Java 7 API doc)

It seems that when you use iterator to get through each item in a collection, the indexOf() will return the index value starting from the current iterator position.

//i.e.
ArrayList<String> a = new ArrayList<String>();
a.add("Y"); a.add("A"); a.add("A"); a.add("R"); a.add("R");
for (String str : a) {
  System.out.println(a.indexOf(str));  // get 0 all the time per OP post
}

String str;
for (int i=0; i<a.size(); i++) {
  str = a.get(i);
  System.out.println(a.indexOf(str));  // get the i value
}

If I were going to iterate through a collection and wanted to keep the index, I would never use the iterator. I guess that what the class implementor expects as well because it makes sense. So the documentation about iterator involve in indexOf() is not there.

Edited 5 Years Ago by Taywin: n/a

Actually these two can be used as two nice unobscured variables that are very easy to comprehend. Nevertheless it is your code, your project, no need to listen to good intended advice

:):)

OK, I understand that idea, but the following simple code seems to show that it works exactly like it should...

ArrayList <String> a = new ArrayList<>();
      a.add("Hello");
      a.add("world");
      a.add("Hello");
      a.add("world");
      a.add("again");
      for (String s : a) {
         System.out.println(a.indexOf(s));
      }

Output:
0
1
0
1
4

Is it possible the the OPs class had an implementation of equals that confused things maybe?

Edited 5 Years Ago by JamesCherrill: n/a

Hmm... Maybe it is fixed in version 7??? Don't know... This is per OP experience by the way.

Edited 5 Years Ago by Taywin: n/a

It seems that when you use iterator to get through each item in a collection, the indexOf() will return the index value starting from the current iterator position.

//i.e.
ArrayList<String> a = new ArrayList<String>();
a.add("Y"); a.add("A"); a.add("A"); a.add("R"); a.add("R");
for (String str : a) {
  System.out.println(a.indexOf(str));  // get 0 all the time per OP post
}

String str;
for (int i=0; i<a.size(); i++) {
  str = a.get(i);
  System.out.println(a.indexOf(str));  // get the i value
}

If I were going to iterate through a collection and wanted to keep the index, I would never use the iterator. I guess that what the class implementor expects as well because it makes sense. So the documentation about iterator involve in indexOf() is not there.

Can't get you properly. And no idea of documenting something. I only know Java Documentation.
and for the code part I think

String str;
for (int i=0; i<a.size(); i++) {
  str = a.get(i);
  System.out.println(a.indexOf(str));  // get the i value
}

this code also will not give the correct index value as far as I've tried it in my code.

If you posted rest of the code it woud made big difference as we could see exactly what you doing. At the moment we can only guess based on provided data...

PS: You should only use indexOf only on unique value data, no duplicates, as in case of duplicates it will only return first occurance

I just tried that code with the test data array from my previous post, and it also gives the correct results of 0, 1, 0, 1, 4
What values do you get?

Oh I think my assumption could be wrong. :P Thinking that the OP data may consists of all duplicated, it would also result as the OP was experiencing as well. :)

If you posted rest of the code it woud made big difference as we could see exactly what you doing. At the moment we can only guess based on provided data...

PS: You should only use indexOf only on unique value data, no duplicates, as in case of duplicates it will only return first occurance

I can't post the full code as it's divided in some packages. So..I can't add those.

I just tried that code with the test data array from my previous post, and it also gives the correct results of 0, 1, 0, 1, 4
What values do you get?

I got the same old all '0'' values...:'(

This question has already been answered. Start a new discussion instead.