• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList, can we insert primitive types into it?  RSS feed

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I am able to insert primitive datatypes into an ArrayList but I've seen some places online that we cannot. Am I doing something wrong? or am I right?

Thanks.
 
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because you are using jdk 1.5 or grater,

It implicitly cast it to Integer object

<blockquote>code:
<pre name="code" class="core">list.add(new Integer(i));</pre>
</blockquote>

try with jdk 1.4 it will give you compile time error. you have to cast it manually as shown in the code.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right about Java5, but it's not casting, it's boxing (usually called auto-boxing). If I want to use boxing for ints and Integers I declare the List as List<Integer> and ArrayList<Integer>. Then the compiler takes care of it for me.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that this can be dangerous. While there aren't any problems for adding, removing is another issue.

List has two remove methods: E remove(int) and boolean remove(Object). If you call remove with a primitive int, hoping to remove the wrapped element with the second method, you'll be in for a surprise.

Yes I've made that mistake once
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All these are working for me.

ArrayList c = new ArrayList();
int i=99;
c.add("fdsf");
c.add(100);
c.add(new Integer(10));
c.add(new String("gosh"));
c.add(i);

??
 
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

c.add("fdsf");
c.add(100);


Yes. This should work with JDK 1.5 or Higher with auto-boxing feature. It "boxed" value 100 in to -> new Integer(100). We can add only objects to List but the compiler do the rest.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not sure, but i think with

ArrayList c = new ArrayList();

you're saying "make me an ArrayList that holds an Object reference type. since every object is an Object, everything can go in.

When you try and take stuff OUT, it is going to return an Object reference, so you'll have to go through some hoops to figure out what you have.

at least, i THINK i'm right.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right, Fred. Also (I have never tried it) can you get boxing of the int literal without declaring the List to be of type <Integer> ?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just tried it, and you do appear to get auto-boxing of the 100 int literal.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!