• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays.asList(myArray).indexOf(value) return -1  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mooses!

I got a problem with getting the index of arrays field containing particular value. I try it as follows:


which constantly returns me the index of -1.
what is wrong?

cheers
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check what is returned by Arrays.asList(myArray).
It is not what you think it is.
 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, I've checked and... still think the same: it is the list of my int values contained in the array. Isn't it? Of course the new question arrises: what is list? Has it something to do with list interface? There is also some notes that list is generic types. Any prompts?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Przemek Geminski wrote:well, I've checked and... still think the same: it is the list of my int values contained in the array. Isn't it?

So you did not check carefully. This method does not return List<Integer> in this case (there is no such thing as List<int>). It returns List<int[]>.
The list contains only one element. Whole int[] array. No wonder it can not find 45 in the list as there is no such element in the list.

Przemek Geminski wrote:Of course the new question arrises: what is list? Has it something to do with list interface? There is also some notes that list is generic types. Any prompts?


You may find this tutorial useful: https://docs.oracle.com/javase/tutorial/collections/
 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Przemek,

it is indeed a bit confusing. To illustrate Pawels remarks, have a look at the following
code:

This code will give you the expected '1' as outcome. Also note the autoboxing
doing its job.

Greetz,
Piet
 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see it opens a bunch of topics. I know I can easily do it with ArrrayList or LinkedList and I don't get it (yet) why it is so knotty in a case of simple Arrays. Anyway, I did my own index sercher based on loop for.
Thanks for hints guys.

Have a good one
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!