 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jowita Guest
|
Posted: Sun May 20, 2007 3:15 am Post subject: generic ArrayList |
|
|
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? |
|
| Back to top |
|
 |
visionset Guest
|
Posted: Sun May 20, 2007 3:27 am Post subject: Re: generic ArrayList |
|
|
"Jowita" <jowi6 (AT) hotmail (DOT) com> wrote in message
news:1179612919.426589.99260 (AT) u30g2000hsc (DOT) googlegroups.com...
| Quote: |
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.
--
Mike W |
|
| Back to top |
|
 |
Arne Vajhøj Guest
|
Posted: Sun May 20, 2007 3:30 am Post subject: Re: generic ArrayList |
|
|
Jowita wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Tom Hawtin Guest
|
Posted: Sun May 20, 2007 7:12 am Post subject: Re: generic ArrayList |
|
|
Jowita wrote:
| Quote: |
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 |
|
| Back to top |
|
 |
junchengmax Guest
|
Posted: Sun May 20, 2007 7:12 am Post subject: Re: generic ArrayList |
|
|
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. |
|
| Back to top |
|
 |
printdude1968@gmail.com Guest
|
Posted: Sun May 20, 2007 7:12 am Post subject: Re: generic ArrayList |
|
|
On May 19, 11:34 pm, Tom Hawtin <use...@tackline.plus.com> wrote:
| Quote: | Jowita wrote:
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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|