0

I'm trying to create a program that alphabetizes a list, using the Collections.sort method and java.util.List, the Error is: 1 error and 15 warnings found:

Error: java.util.List is abstract; cannot be instantiated
--------------
** Warnings **
--------------
Warning: unchecked call to add(E) as a member of the raw type java.util.List
Warning: unchecked method invocation: method sort in class java.util.Collections is applied to given types
  required: java.util.List<T>
  found: java.util.List

my code:

public static void preset(){
    List words= new List();
    words.add("apple");
    words.add("country");
    words.add("couch");
    words.add("shoe");
    words.add("school");
    words.add("computer");
    words.add("yesterday");
    words.add("wowza");
    words.add("happy");
    words.add("tomorrow");
    words.add("today");
    words.add("research");
    words.add("project");
    Collections.sort(words);




  } //end of method preset
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

1

Just as the error said, List is abstract, you need some concrete implementation. In the case you posted, ArrayList will do.

Also note that you are using List as a raw type; don't do that (unless you are using a version before Java 5). Parameterize it with a type parameter (here, String).

Yet also: don't change the declaration of words to be ArrayList: List is good enough (normally), and by left it unchanged, you gain the ability to change the implementation later on.

In conclusion:

List<String> words= new ArrayList<String>();

or if using Java 7:

List<String> words= new ArrayList<>();
zw324
  • 26,764
  • 16
  • 85
  • 118
  • THanks that works and also, how do i print this new sorted list? I was trying to print it by calling the method in the main, but that isn't working – user2399999 May 19 '13 at 23:50
  • Could you post how is it not working? System.out.println(words) should be enough. – zw324 May 19 '13 at 23:53
0

You can not instantiate java.util.List, but some of it's implementations. Like (for instance) java.util.ArrayList. Note that they are Generic.

Just fix the following:

 List words= new List();

to

 List<String> words= new ArrayList<String>();
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147