-1

Simple question, but everytime I google it there are only examples using an ArrayList, and list.indexOf(3), doesn't work on int[] nums = new int[10]. Soo im a little confused.

memelord23
  • 84
  • 1
  • 2
  • 11

2 Answers2

4

Here's the real problem. You appear to think that

 int[] nums = new int[10]

is defining a List or a list. It isn't. It is an array. The words "list" and "array" are NOT synonymous in the context of Java. So when you Google for "finding an index of an element in a list" ... you don't get any useful hits.

If you use the >>correct<< terms when searching (Google, SO search, anything) you are far more likely to get useful search results.

For example:


FWIW, if you want a (real) list of integers in Java you declare it like this:

List<Integer> nums = new ArrayList<>();

Notes

  1. You have to use Integer as the type parameter, not int.
  2. You don't need to provide 10 as a parameter. And if you do, it is a hint that tells the ArrayList what the initial capacity of the list should be. (For an array it is the actual size ... and it can't be changed).
Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    ohh yea I thought it created a list with 10 indexes. Lame, alright I better find the difference between list and array thanks. – memelord23 Dec 16 '15 at 02:49
0

If you want to use indexOf(3) on an array you could create a List from it

ArrayList<Integer> myInts = new ArrayList<Integer>(Arrays.asList(array))

and then you can use

myInts.indexOf(3)
Lazy Senior
  • 171
  • 1
  • 8