Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Usage of indexOf method of ArrayList class

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above program, the commented line gives compilation error - cannot resolve symbol : method Integer (int)
Can anybody tell me why this error is flagged? What is the correct way to use the indexOf method of ArrayList class?
Kezia.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kezia,
i think u missed putting the new when creating the object of Integer i think the code shud be
import java.util.*;class ArrayListDemo{public static void main (String args[]){List arrListObj = new ArrayList();arrListObj.add (new Integer(7));arrListObj.add (new Integer(5));arrListObj.add (new Integer(3));//int index = arrListObj.indexOf(new Integer(3));System.out.println (index);}}
bye
CHERRY
[This message has been edited by Cherry Mathew (edited June 04, 2001).]
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile the code after adding 'new' to the commented, statement as in

I do not get compile error and am able to get the desired result.
But, I have a questiion regarding the usage of 'new' in this context.
In the above statement, I am not creating an object but am trying to get the index of the first occurance of int value 6 wrapped in the class Integer. As far as I know, 'new' is used to create new objects.
1.Can you please explain as to why 'new' has to be used when calling indexOf method?
2. Should 'new' always be used when calling the indexOf method?
Kezia.
[This message has been edited by Kezia Matthews (edited June 04, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create a NEW Integer when you are asking for where it is loaded up in the ArrayList you will ALWAYS get -1, meaning that it is not found. You have to look for an EXISTING Integer to get meaningful results.
The way your code is now, when you loaded your ArrayList, you did not keep a reference to the Integers that you loaded up, so you can not use that reference later to find out WHERE the object (in this case an Integer) is in the ArrayList.
Try it this way.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I make arrayList compare the object values (number values) instead of object references? Thanks in advance! KH.
 
khyau
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I make arrayList to compare value? instead of references?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khyau - please read our name policy and select a username which follows our guidelines. Aso for your question - according to the API for indexOf(), the method makes comparisons using the equals() method of the objects in the list. So if you want to compare objects by value, you need to override the equals() method of those objects to do the comparison this way. Unless this has already been done for you - e.g. the String class equals() method compares by value already. Actually this is true of Integer as well - no equals() override is needed, and two separate instances of Integer are already considered equal if their values are identical.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to admit, I'm not sure of the utility of a method like 'indexOf'. The purpose of an ArrayList is to keep track of objects for you. It has the nice bonus of retaining order of insertion. So if we need to maintain two references to an object (one outside the ArrayList, to use in indexOf, and one inside the ArrayList), then what's the point?

I have tested contains(), and it behaves like khyau
was enquiring about.
Here is the code for contains()But what happens when I want to get() this value? I need an index! So I still need to maintain an external reference to each element in the ArrayList, so I can use indexOf() inside of get(). This seems ridiculous. What do others think?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you do not need to hold an external refernce to the object you are seeking - not for Integer anyway, or for any other class that overrides equals() to compare by value rather than object identity. Cindy was mistaken. The contains() method and the indexOf() method both look things up using the equals() method, as explained in the documentation. Why not try testing this using the code you mysteriously commented out?
<code><pre>
class ArrayListDemo {
public static void main (String args[]) {
Integer a = new Integer(7);
Integer b = new Integer(5);
Integer c = new Integer(3);
List arrListObj = new ArrayList();
arrListObj.add (a);
arrListObj.add (b);
arrListObj.add (c);
int index = arrListObj.indexOf(new Integer(5));
boolean found = arrListObj.contains(new Integer(5));
if (found)
System.out.println ("object Integer(5) found at: " + index);
else
System.out.println("not found");
}
}
</pre></code>
Ths prints out:
<code><pre>object integer(5) found at: 1}</pre></code>
The fact that it printed the index value of 1 indicates that indexOf() worked. Even though it did not use a previously-stored reference to the original Integer(5) object - it used a brand new object that contained an equivalent value. I don't think there is any problem here.
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The output of the above program is
0
1
This indicates that, both approaches of indexOf method can be used, that is

and

This shows clearly from the output that a new Integer object is not created when using 'new' with indexOf method.
My question now is how does the approach using 'new' with indexOf method work ?
Sorry, Jim, I had posted this reply before reading your previous reply. I got the answer to my query in your reply.


it used a brand new object that contained an equivalent value. I don't think there is any problem here.


So, a new object is created when 'new' is used with indexOf method and the int value wrapped in Integer object is checked for existence in the list.
Thank you all for answering my queries.
[This message has been edited by Kezia Matthews (edited June 05, 2001).]
[This message has been edited by Kezia Matthews (edited June 05, 2001).]
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic