Lets start with this:
I tried printing out the result of Arrays.asList(j), but I'm not really sure what to make of the result: [[F@307af497].
You are printing a list using (effectively) the toString()
method. So ...
The outer '[' and ']' are from the list formatting.
You have a list consisting of one element.
That element is an array of float
. (The [F@307af497
is produced by Object.toString()
, and [F
is how the type float[]
is rendered ...)
This is actually an important clue. Arrays.asList(float[]) is returning a "list of float[]" ...
But why?
Well, because that's what it is supposed to do!!
The Arrays.asList
signature is Arrays.asList<T>(T ...)
. That means it expects either an explicit T[]
... or a sequence of zero or more T
instances, which it will then wrap as an Object[]
. The Object[]
is then wrapped as a List<Object>
(roughly speaking).
The critical thing here is that the actual type T must be a reference type ... not a primitive type.
But your code seems to be expecting an overloaded method like this Arrays.asList(float ...)
... and expecting that that will give you your float[]
wrapped as a List<Float>
.
Unfortunately, there is no such overload for asList
.
So what is actually happening is that:
your call is binding to Arrays.asList<float[]>(float[] ...)
the varargs is causing j
to be wrapped in an array; i.e. equivalent to new float[][]{j}
the result is an instance of List<float[]>
... with one element.
So what is the solution?
In general ...
One solution would be to represent your floats as a Float[]
rather than a float[]
. Ideally, you would push this change back through the code that created the array in the first place, etcetera.
A second solution would be to convert the float[]
to a Float[]
before calling asList
. The simple way to do that is with a simple loop. (There may also be a 3rd-party library for doing this.) The downsides are:
the conversion needs to happen each time you call this method which could be expensive if you call it a lot, and
there is no connection between the original array and the array that you have wrapped ... if you wanted to update the array through the list wrapper.
But in this case, the best solution is to simply replace this:
Arrays.asList(j).indexOf(k[0])
with a simple loop that iterates over the original float[]
testing for an entry that matches k[0]
.
The moral of this story: you can easily shoot yourself in the foot by striving for an elegant solution in Java.
Often, dumb code is better. (Both faster, and more correct.)