Thursday, July 30, 2009

Problem in Collection in java ?

Dear all,


I am getting a compilation error with my file.


import java.util.*;


public class CollectionList {


public static Collections get() {


Collection sorted = new LinkedList();


sorted.add("B");





sorted.add("C");


sorted.add("A");


return sorted;


}


public static void main(String[] args) {


for(Object obj: get()) {


System.out.print(obj+" , ");


}


}


}





Output :


D:\%26gt;javac CollectionList.java


CollectionList.java:9: incompatible types


found : java.util.Collection


required: java.util.Collections


return sorted;


^


CollectionList.java:12: foreach not applicable to expression type


for(Object obj: CollectionList.get()) {


^


Note: CollectionList.java uses unchecked or unsafe operations.


Note: Recompile with -Xlint:unchecked for details.


2 errors

Problem in Collection in java ?
The method "get" has the return type java.util.Collections, but you return a java.util.Collection. I guess you want the signature of get to look like this: 'public static Collection get() '...





Btw, If you look at the compiler output, you'll see that the compiler output told you exactly what and where the problem is:





CollectionList.java:9: incompatible types


found : java.util.Collection


required: java.util.Collections


return sorted;
Reply:%26gt;%26gt; public static Collections get() {





public static Collection get() {





This should help!

tarot cards

No comments:

Post a Comment