Reputation: 29
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) 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?
Upvotes: 5
Views: 2865
Reputation: 34057
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.
Upvotes: 2
Reputation: 21
You need -source 1.5 or -source 1.6 I think. Or better yet use an IDE. Eclipse, NetBeans, and IntelliJ are all free.
Upvotes: 1
Reputation: 57938
I vaguely remember having to addAll into a list instead of passing a list into ArrayLists constructor
Upvotes: 0