Doubt in Collections

Hi,

Now I'm studying collections package. In that package I studied Collections not support to store the primitive data types. But while using the following code the didn't throw any error message or run exceptions

import java.util.*;

public class ArrayListDemo{

public static void main(String args[])

{

int i=100;

ArrayList arr=new ArrayList();

arr.add(10);

arr.add(i);

arr.add(1);

System.out.println(arr);

}

}

After compilation run the program It will prints the following output

output:

[10,100,1]

so I couldn't realize what actually made. please explain me about this.

Thanks in Advance,

Maheshwaran Devaraj

[747 byte] By [mheshpmra] at [2007-11-27 11:55:58]
# 1

waht's there to explain ?

U added elements to ArrayList and u r displaying them one by one

AmitChalwade123456a at 2007-7-29 19:04:57 > top of Java-index,Java Essentials,Java Programming...
# 2

@Op. That because autoboxing was added to Java in Java 5.

Kaj

kajbja at 2007-7-29 19:04:57 > top of Java-index,Java Essentials,Java Programming...
# 3

wait how did u compile it ,? , that add method require one more parameter ,,

AmitChalwade123456a at 2007-7-29 19:04:57 > top of Java-index,Java Essentials,Java Programming...
# 4

> wait how did u compile it ,? , that add method

> require one more parameter ,,

No, why?

kajbja at 2007-7-29 19:04:57 > top of Java-index,Java Essentials,Java Programming...
# 5

> wait how did u compile it ,? , that add method

> require one more parameter ,,

No it doesn't. Please stop giving people duff answers!

georgemca at 2007-7-29 19:04:57 > top of Java-index,Java Essentials,Java Programming...
# 6

> ter compilation run the program It will prints the

> following output

>

> output:

>[10,100,1]

> so I couldn't realize what actually made.

> please explain me about this.

If I understand you correctly,

System.out.println(arr);

calls the ArrayList's toString() method, which is inherited from AbstractCollection.

If you look at the java API for that class, it explains why you're getting the ouput you're getting, i.e. the list of elements in square brackets.

karma-9a at 2007-7-29 19:04:57 > top of Java-index,Java Essentials,Java Programming...
# 7

> calls the ArrayList's toString() method, which is

> inherited from AbstractCollection.

> If you look at the java API for that class, it

> explains why you're getting the ouput you're getting,

> i.e. the list of elements in square brackets.

I don't think what he was asking about :)

He said that the collections didn't support primitives, and then tested that and saw that the code compiled, and could execute. He could even see that he had numbers in the collection.

@Op. The reason that it is working is that the primitives that you are using are autoboxed, so the collection does not contain primitives, it contains instances of the wrapper classes. The same code will not compile in e.g. JDK 1.4

kajbja at 2007-7-29 19:04:57 > top of Java-index,Java Essentials,Java Programming...
# 8

> > calls the ArrayList's toString() method, which is

> > inherited from AbstractCollection.

> > If you look at the java API for that class, it

> > explains why you're getting the ouput you're

> getting,

> > i.e. the list of elements in square brackets.

>

> I don't think what he was asking about :)

Looking again at what the OP posted, I'll have to agree.

My speed-reading technique is still alpha-quality.

karma-9a at 2007-7-29 19:04:58 > top of Java-index,Java Essentials,Java Programming...
# 9

> > I don't think what he was asking about :)

>

>

> Looking again at what the OP posted, I'll have to

> agree.

>

> My speed-reading technique is still alpha-quality.

..and it looks like I was typing to fast. Even I have a hard time understanding the quoted sentence :)

kajbja at 2007-7-29 19:04:58 > top of Java-index,Java Essentials,Java Programming...
# 10

Hi kajbj,

Thank u for ur response. I want to know what is autoboxing .

Is there relation between autoboxing and typecasting. Could U please explain what is autoboxing. In java from which places autoboxing occured.

Thanks in Advance,

Maheshwaran Devaraj

mheshpmra at 2007-7-29 19:04:58 > top of Java-index,Java Essentials,Java Programming...
# 11

> Thank u for ur response. I want to know

> what is autoboxing .

You should have googled now when you had something to google on:

http://www.google.com/search?q=java+autoboxing

> is there relation between autoboxing and typecasting.

No

Kaj

kajbja at 2007-7-29 19:04:58 > top of Java-index,Java Essentials,Java Programming...
# 12

http://en.wikipedia.org/wiki/Autoboxing#Autoboxing

SoulTech2012a at 2007-7-29 19:04:58 > top of Java-index,Java Essentials,Java Programming...
# 13

return col.contains(doubt);

ParvatiDevia at 2007-7-29 19:04:58 > top of Java-index,Java Essentials,Java Programming...