tags:

views:

104

answers:

5

Hi all,

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);
  }
} 

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

Please help me to resolve this error.Thanks a lot.

A: 

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

mkoryak
+1  A: 

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.

Ricket
If you might ever need to modify that list later, it will throw an UnsupportedOperationException because the list that is returned from Arrays.asList is unmodifiable. To solve this problem copy the list into an ArrayList (or LinkedList or whatever) via the "copy constructor" as in the original question or addAll as mkoryak said.
MatrixFrog
A: 

List<String> services = new ArrayList<String>(

Arrays.asList("COMPUTER", "DATA", "PRINTER"));

Compiles for me

Woot4Moo
A: 

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 wouldn't grab an IDE that soon. First learn the basic concepts properly :)
BalusC
A: 

I wonder how this should compile? Or is this just a snipplet of your code? First of all rnd is not declared and list is not declared either ?!

kukudas