Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays.asList(a) not behaving like I thought it would  RSS feed

 
Carey Brown
Bartender
Posts: 3027
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays.asList(a) doesn't behave like I thought it would. Any suggestions on how to make this more generic?

 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of an int[], try it with an Integer[]

The asList method uses varargs, and varargs don't work with non-reference types

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can not use this as arrays are not generic. So replacing List<int[]> list with List<?> list is not possible
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Arrays#asList method takes T... as its argument type. As you have already been told, an int[] is not a T[], so it takes the array as one object and makes a 1‑length varargs list with it.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Arrays.asList(a) doesn't behave like I thought it would. Any suggestions on how to make this more generic?

No, but you can wrap an int[] as a List, viz:
Give it a try.

I really suggest you have a look at AbstractList (and AbstractMap, and a few of the other 'Abstract...' classes in java.util) because they really are very useful to know about.
That 'IntList' definition above is a fully-functioning immutable List in 19 lines of code, which, considering all the things that a List can do, is pretty impressive. And if you want to be able to change values, you can do that too.

But I'll leave you to read about it. That was just a "taster".

HIH

Winston
 
Carey Brown
Bartender
Posts: 3027
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston, that was helpful. I also checked out the Abstract... classes, they will come in handy too.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once I see methods like that, also the toArray method of Stream, I start wishing I had never seen arrays of primitives. We might start by teaching people about int[] arrays, but they are really awkward to handle compared to Object[] arrays.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I start wishing I had never seen arrays of primitives. We might start by teaching people about int[] arrays, but they are really awkward to handle compared to Object[] arrays.

Except that Integer is immutable. The technique above is great when you need a List of mutable integers without the space (or API) overhead of List<AtomicInteger>.

Winston
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A nice way, slightly longer, but also slightly simpler (and elegant, IMHO) is:


 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:A nice way, slightly longer, but also slightly simpler (and elegant, IMHO) is:

I agree. Have a cow.

Winston
 
Franklin Okeme
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I hope this helps.
 
Carey Brown
Bartender
Posts: 3027
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I had tried using Integer in the beginning and got it to work. What I couldn't get to work was int[]. So, guess I'll have to use Integer then.

In your example you have


What is the difference between
Stream.of(a)
and
Arrays.stream(a)

What is the difference between
map(String::valueOf)
and
map(Object::toString)
I guess the former will handle arrays of built-in types, and the later will not.
 
Franklin Okeme
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.valueOf(Object o) is safer than Object.toString(). If o == null, the first returns a null whereas the latter throws a nullPointerException.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!