• Post Reply Bookmark Topic Watch Topic
  • New Topic

backed List not returning a proprer size  RSS feed

 
A. Aka
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

when I run this :


I have 1, instead of 4
is that normal ? I am using java8

Thanks,

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.  You have a list with one element that is an array.  Maybe you wanted to do this?
 
A. Aka
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping to see autoboxing playing some role here ...
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what way?
 
A. Aka
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1, 4, 2, 7 will be seen as Integer instead of int
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, they are.

The ints 1, 4, 2, 7 are autoboxed to Integers in the List.
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The scenario the OP is seeing here is due to the signature of Arrays.asList:

static <T> List<T> asList(T... a)

When the OP does this:

Then T becomes int[]. Meaning you have a List with a single int[] in it, hence you observe list.size() returning 1.

Make sense?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!