• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections/Generics

 
sushma yeruva
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends
given the following code

import java.util.LinkedList;
public class Family{
public static void main(String argv[]){
new Family();
}
Family(){
LinkedList children = new LinkedList();
children.add("Iain");
children.add("Luke");
children.add("David");
children.add("Karen");
display(children);
}
public void display(LinkedList offspring){
for(String name: offspring){
System.out.print(name);

}
}
}

Hey I got compile time error stating "illegal start of expression at for loop",
but the answer in mock exam is "IianLukeDavidKaren"
Could anybody help me out

Regards
Sushma
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what compiler are you using?
 
Marc Marechal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't have the same error compiling with javac 1.5.0_03 :

C:\Temp\Family.java:22: incompatible types
found : java.lang.Object
required: java.lang.String
for(String name : offspring)


Also, you should change

public void display(LinkedList offspring) {

to

public void display(LinkedList<String> offspring) {

to avoid the preceding error.


Marc.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's why I wondered whether he was using a 1.5 compiler at all. It's an error you'd get using a 1.4 or earlier compiler.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use concrete types rather than raw types:

LinkedList<String> children = new LinkedList<String>();

public void display(LinkedList<String> offspring)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic