• Post Reply Bookmark Topic Watch Topic
  • New Topic

construct ArrayList from int[]  RSS feed

 
Dem Snd
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

As title says, I'm trying to construct and arraylist from int array. The problem is it does not autobox ints. If generics are not used such as ArrayList x = new ArrayList(Arrays.asList(intArray)) then you cannot compare the integers, because they are converted to objects and > operator does not work, but if i do it like ArrayList<Integer> x = new ArrayList(Arrays.asList(intArray)) then i get class cast exception, which leaves me with a question, is there even a way to do this? this kind of conversion is very common so i don't believe there is no way. Thanks for any help in advance.
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go for the manual way:
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

There is not really a shortcut to doing this, the simplest way is just to add it manually.
You may find this thread of some interest though.

Sean

edit: beaten to it!
 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's one- Arrays.asList

Update: Ah I realized its an "int".
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:There's one- Arrays.asList

Have you tried it with a primitive array? It's not so simple, unfortunately.
 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Mohamed Sanaulla wrote:There's one- Arrays.asList

Have you tried it with a primitive array? It's not so simple, unfortunately.


Yeah, I was about to update my earlier post. Realized it late that its an int and not Integer
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the JavaRanch!
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you override java.util.ArrayList with a constructor which takes an int[] as a parmeter?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!