开发者

Compilation error when instantiating a new ArrayList

I have the following code:

import java.util.*;
import java.io.*;
import java.util.*;
import java.io.*;

public class ShufflingListAndArray
{
   public static void main(String[] args) thro开发者_JAVA百科ws IOException
   {
   List <String> services = new ArrayList<String> (
   Arrays.asList("COMPUTER", "DATA", "PRINTER"));//here I have used List <String> services=new ArrayList<String>( Arrays.asList("COMPUTER", "DATA", "PRINTER"));// followed by next statement Satring s=Services.get(rnd.nextInt(Services.size()));

   String s = services.get(rnd.nextInt(services.size()));

   Collections.shuffle(list);
   //Collections.sort(list);
   System.out.println("List sorting :"+ list);
   }
} 

And when I compile this program I get the following error:

C:\>javac ShufflingListAndArray.java
ShufflingListAndArray.java:6: '(' or '[' expected
    List<String> services = new ArrayList<String>(
                                         ^
1 error

Can someone help me resolve this error?


Replace those first couple lines of your main function with this:

List<String> services = Arrays.asList("COMPUTER", "DATA", "PRINTER");

Following the example at the Arrays.asList documentation.

(You also have double import java.util.*;)

edit:

Considering the other answers and comment made on my answer, your code does seem to be correct, and the problem is more likely that you need to compile it with Java 5 (or newer), which is the version when Generics were introduced. If you must run it on 1.4.2 or whichever version you have, remove the instances of <String> and you'll be good to go.


You need -source 1.5 or -source 1.6 I think. Or better yet use an IDE. Eclipse, NetBeans, and IntelliJ are all free.


I vaguely remember having to addAll into a list instead of passing a list into ArrayLists constructor

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜