newbie question on generics error

G

gmail.5.mbrna

hi. I'm attempting to learn some java. Currently following the
Collections Trail
at http://java.sun.com/docs/books/tutorial/collections/index.html

On this page
http://java.sun.com/docs/books/tutorial/collections/interfaces/list.html
is an example of sublisting:
public static <E> List<E> dealHand(List<E> deck, int n) {
int deckSize = deck.size();
List<E> handView = deck.subList(deckSize - n, deckSize);
List<E> hand = new ArrayList<E>(handView);
handView.clear();
return hand;
}

When I paste that into a file and try to compile with javac, I get
'class' or 'interface' expected
public static <E> List<E> dealHand(List<E> deck, int n)
--------------^
(the carat is pointing to the first < char on the line)

javac -version reports: javac 1.5.0_07

What am I doing wrong?
thanks,
mikey.
 
O

Oliver Wong

hi. I'm attempting to learn some java. Currently following the
Collections Trail
at http://java.sun.com/docs/books/tutorial/collections/index.html

On this page
http://java.sun.com/docs/books/tutorial/collections/interfaces/list.html
is an example of sublisting:
public static <E> List<E> dealHand(List<E> deck, int n) {
int deckSize = deck.size();
List<E> handView = deck.subList(deckSize - n, deckSize);
List<E> hand = new ArrayList<E>(handView);
handView.clear();
return hand;
}

When I paste that into a file and try to compile with javac, I get
'class' or 'interface' expected
public static <E> List<E> dealHand(List<E> deck, int n)
--------------^
(the carat is pointing to the first < char on the line)

javac -version reports: javac 1.5.0_07

What am I doing wrong?

That's a code snippet, and not a complete source file. Specifically, the
code you're seeing there is a method declaration. It needs to be inside of a
class declaration. You might want to back up and learn the grammar of Java
before moving onto things like the Collections API.

http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html

- Oliver
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top