Why won't this compile?
I'm new to Java and I am trying to learn how to use ArrayList. I wrote
a short example, but it doesn't compile, so I found an example on the
web and it fails with the same error message.
Here's the example I downloaded:
import java.util.*;
public class ArrayListGenericDemo {
public static void main(String[] args) {
ArrayList<String> data = new ArrayList<String>();
data.add("hello");
data.add("goodbye");
// data.add(new Date()); This won't compile!
Iterator<String> it = data.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
Heres the error:
D:\Data\java\parser\ArrayListGenericExample.java:5: '(' or '[' expected
ArrayList<String> data = new ArrayList<String>();
^
1 error
I'm using j2sdk1.4.2_12 which I downloaded from sun a few days ago.
I tried an example from the book "Head First Java" and got the
identical error message.
My first hand-written example also go that message until I changed it
to declare a private instance variable as an ArrayList<String>
import java.util.*;
public class PatternLibrary {
private ArrayList<String> pattArray;
/**
* Contructor
*/
PatternLibrary() {
pattArray = new ArrayList();
}
public ArrayList fetchPatterns( String[] matchWords ) {
...
return pattArray;
}
}
and got this message:
D:\Data\java\parser\PatternLibrary.java:5: <identifier> expected
private ArrayList<String> pattArray;
^
1 error
What's going on here?
Any help would be deeply appreciated.
--gary