实例化新的ArrayList时出现编译错误 [英] Compilation error when instantiating a new ArrayList
问题描述
我有以下代码:
import java.util.*;
import java.io.*;
import java.util.*;
import java.io.*;
public class ShufflingListAndArray
{
public static void main(String[] args) throws 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");
按照 Arrays.asList文档.
(您还有两个import java.util.*;
)
考虑其他答案和对我的答案的评论,您的代码似乎确实是正确的,并且该问题更有可能需要您使用Java 5(或更高版本)进行编译,而Java 5则是泛型.如果必须在1.4.2或任何版本上运行它,请删除<String>
的实例,一切都会很好.
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.
这篇关于实例化新的ArrayList时出现编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!