generic ArrayList

J

Jowita

I was having problems with generic ArrayList, so I created a little
test.
Here it is:

import java.util.*;

public class test{

public void blah(){
ArrayList<String> arString = new ArrayList<String>();
String s1 = "testing";

arString.add(s1);
String s = arString.get(0);

}

}


I cannot compile the above, this is the method I get:
test.java:7: '(' or '[' expected
ArrayList<String> arString = new ArrayList<String>();

I installed the latest Java SE 6.

Can anyone point out what's worng with the code?
 
V

visionset

Jowita said:
I was having problems with generic ArrayList, so I created a little
test.
Here it is:

import java.util.*;

public class test{

public void blah(){
ArrayList<String> arString = new ArrayList<String>();
String s1 = "testing";

arString.add(s1);
String s = arString.get(0);

}

}


I cannot compile the above, this is the method I get:
test.java:7: '(' or '[' expected
ArrayList<String> arString = new ArrayList<String>();

Nothing at all wrong with it.
However it is usual to reference ArrayLists by their List interface.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jowita said:
I was having problems with generic ArrayList, so I created a little
test.
Here it is:

import java.util.*;

public class test{

public void blah(){
ArrayList<String> arString = new ArrayList<String>();
String s1 = "testing";

arString.add(s1);
String s = arString.get(0);

}

}


I cannot compile the above, this is the method I get:
test.java:7: '(' or '[' expected
ArrayList<String> arString = new ArrayList<String>();

I installed the latest Java SE 6.

Can anyone point out what's worng with the code?

Nothing. It compiles here.

Are you by any chance compiling for an old version ?

Arne
 
T

Tom Hawtin

Jowita said:
I cannot compile the above, this is the method I get:
test.java:7: '(' or '[' expected
ArrayList<String> arString = new ArrayList<String>();

I installed the latest Java SE 6.

To check which version of Java you are really using put -J-showversion
on the command line (-J just tells the javac wrapper to pass the
argument onto java, and you can look the java options up in the
documentation).

Presumably you have a problem with your PATH variable (or equivalent).
You need to install the whole JRE, not just the JDK.

In terms of style: Indent correctly. Class names should have initial
caps. Open brace should have a space before it. Using use interface
types (List) in place of implementation types. Do not attempt to prefix
variable names with type information (ar, use the plural of whatever you
have for a collection).

I t may seem trivial, but it will help anyone used to read Java to help
you, and also help you to read conventional Java.

Tom Hawtin
 
J

junchengmax

Generic programming is avaliable since JDK1.5, If you are using
JDK1.6,Of course it shouldn't have any problem.May be you are using
IDE,Just like Jbuilder.JDK was binding with them,I think you'd better
run it in without them and use the original environment -- That is the
command(under Windows),throught which you may understand it more
deeply.
 
P

printdude1968

Jowita said:
I cannot compile the above, this is the method I get:
test.java:7: '(' or '[' expected
ArrayList<String> arString = new ArrayList<String>();
I installed the latest Java SE 6.

To check which version of Java you are really using put -J-showversion
on the command line (-J just tells the javac wrapper to pass the
argument onto java, and you can look the java options up in the
documentation).

Presumably you have a problem with your PATH variable (or equivalent).
You need to install the whole JRE, not just the JDK.

In terms of style: Indent correctly. Class names should have initial
caps. Open brace should have a space before it. Using use interface
types (List) in place of implementation types. Do not attempt to prefix
variable names with type information (ar, use the plural of whatever you
have for a collection).

I t may seem trivial, but it will help anyone used to read Java to help
you, and also help you to read conventional Java.

Tom Hawtin

No problem here either. I copied the code right into Eclipse running
under jdk1.6.0 on windows xp professional.
I changed the blah method to public static void main so that I could
see what it was doing without having to create
another class to test it.

import java.util.*;

public class ArrayListTest{

public static void main (String [] args){
ArrayList<String> arString = new ArrayList<String>();
String s1 = "testing";

arString.add(s1);
String s = arString.get(0);
System.out.println(s);

}
}

testing
 
S

squirrel

I was having problems with generic ArrayList, so I created a little
test.
Here it is:

import java.util.*;

public class test{

public void blah(){
ArrayList<String> arString = new ArrayList<String>();
String s1 = "testing";

arString.add(s1);
String s = arString.get(0);

}
}

I cannot compile the above, this is the method I get:
test.java:7: '(' or '[' expected
ArrayList<String> arString = new ArrayList<String>();

I installed the latest Java SE 6.

Can anyone point out what's worng with the code?

I had met the same problem in using Eclipse. Ensure the "Complier
compliance level" is 5.0 or higher. The "Complier compliance level" is
listed in Preferences->Java->Compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top