PDA

View Full Version : java arraylist problem



zizzdude
July 10th, 2008, 05:08 PM
Hey, I try making an ArrayList for Integer objects, but it gives me a strange error, here's what I typed:



import java.util.*;
...
private ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
...


error:


ArrayTest.java:6: <identifier> expected
list = new ArrayList<Integer>();
^
ArrayTest.java:7: <identifier> expected
list.add(1);
^
ArrayTest.java:7: illegal start of type
list.add(1);
^
3 errors



I'm using java compiler version 1.6.0_06 if it helps.

Zugzwang
July 10th, 2008, 05:34 PM
Works well here.



import java.util.*;

class Test {
private ArrayList<Integer> list = new ArrayList<Integer>();

public Test() {
list.add(1);
}
}


Note that the second line you posted can't follow the first one since the first one is a declaration on class level (since you used the "private" keyword) whereas the second one is a command and has to stand inside of a function or method or so.

themusicwave
July 10th, 2008, 06:36 PM
Also, 1 is not an Integer it is a primitive of type int.

You need to do the following:

list.add(new Integer(1));

Integer is an Object type, meaning it is a child of the Object class, just about everything in Java is an Object.

However, primitives are not Objects, for each primitive type there is a corresponding Object type so you can convert back and forth.

Float and float
Double and double
Integer and int
Byte and byte

The list continues. Your ArrayList is of type Integer, not of type int. Due to this you must send it an Integer.

I hope that helps,

Jon

themusicwave
July 10th, 2008, 06:42 PM
Oops I completely misread the above...delete me

HotCupOfJava
July 10th, 2008, 06:43 PM
Yes, the classes such as Integer, Double, Float, etc. are known as WRAPPER CLASSES because they "wrap" the primitive types to enable you to treat them like objects. You must handle them a little differently, though.

zizzdude
July 10th, 2008, 08:21 PM
Yes, the classes such as Integer, Double, Float, etc. are known as WRAPPER CLASSES because they "wrap" the primitive types to enable you to treat them like objects. You must handle them a little differently, though.

I did try doing the other way of adding an Integer object

list.add(new Integer(2));
but it still gives me that strange error.

themusicwave
July 10th, 2008, 09:43 PM
Could you post some context?

It is hard to know what the problem is without the complete code or more of the code.

tinny
July 11th, 2008, 03:09 AM
Also, 1 is not an Integer it is a primitive of type int.

You need to do the following:

list.add(new Integer(1));

Integer is an Object type, meaning it is a child of the Object class, just about everything in Java is an Object.

However, primitives are not Objects, for each primitive type there is a corresponding Object type so you can convert back and forth.

Float and float
Double and double
Integer and int
Byte and byte

The list continues. Your ArrayList is of type Integer, not of type int. Due to this you must send it an Integer.

I hope that helps,

Jon

You shouldnt have to worry about this.

Java provides Autoboxing support (http://jcp.org/aboutJava/communityprocess/jsr/tiger/autoboxing.html). I think that this functionality was introduced as of JRE 5. The OP says they are using JDK 6, so they should be fine. (also I see that they are using Generics which is a Java 5 feature)

Are you using some sort of IDE? If so have you set-up your complier compliance level? In Eclipse this can be done by navigating to Project > Properties > Java Compiler

Zugzwang
July 11th, 2008, 11:44 AM
I did try doing the other way of adding an Integer object

list.add(new Integer(2));
but it still gives me that strange error.

You did read my post, right? It should answer your question.

zizzdude
July 11th, 2008, 03:04 PM
You did read my post, right? It should answer your question.

yeah, I read it, thanks for the help. :)