• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in List.indexOf()

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers
why are the two outputs in the following code -1(unfound). As you can see, there is 4 in the array and I created a list using Array.asList()
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here it is :


I think that when the JVM autoboxes int to Integer, the Integer.equals() method is ignored or something like that.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like the indexOf is using identity of the reference, not the content of the object.

Bill
 
Fissehaye Kahsay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your help
but how can I get the index of 4 in the int[] not in Integer[] ?. I'm forced to use the int[] instead of Integer[] (just calling a method which takes int[])
 
Evan Caballero
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, when you do i.asList(), it returns a list of int[] (List<int[]>) with one element :/

like [{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }]

so, there is no element at index 4 ; there is only an element at index 0, which is your original int[].

the asList method considers that you want to build a list from one int[], which IS an object. It is like if you were doing Arrays.asList(i1, i2, i3, i4), where i1 i2 i3 and i4 are int[] objects.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Evan is right. Arrays.asList will only work on objects, not on primitives. You will need to box the entire array - convert each element into Integer objects first. You may want to switch to a "regular" List implementation:
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic