• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 5172
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A nice way, slightly longer, but also slightly simpler (and elegant, IMHO) is:


 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I hope this helps.
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic