aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections/Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections/Generics" Watch "Collections/Generics" New topic
Author

Collections/Generics

sushma yeruva
Greenhorn

Joined: Jun 17, 2005
Posts: 10
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

Joined: Oct 12, 2000
Posts: 5093
what compiler are you using?


42
Marc Marechal
Greenhorn

Joined: Apr 15, 2005
Posts: 17
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

Joined: Oct 12, 2000
Posts: 5093
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

Joined: Sep 19, 2004
Posts: 531
Use concrete types rather than raw types:

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

public void display(LinkedList<String> offspring)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections/Generics