Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List in java using Random numbers

Tags:

java

I am getting the following error.

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

public class ShufflingListAndArray
{
  public static void main(String[] args) throws IOException

{
    List services = 


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

 Arrays.asList(new String[] {"COMPUTER", "DATA", "PRINTER"}); 

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

    Collections.shuffle(services);


    //Collections.sort(list);


    System.out.println("List sorting :"+ services);
  }
} 

After compiling the above code I get the following error.

C:\>javac ShufflingListAndArray.java
ShufflingListAndArray.java:17: incompatible types
found   : java.lang.Object
required: java.lang.String
  String s = services.get(rnd.nextInt(services.size()));
                         ^
1 error
like image 268
S.PRATHIBA Avatar asked Mar 18 '26 14:03

S.PRATHIBA


1 Answers

Change List services ... to List<String> services

like image 185
polygenelubricants Avatar answered Mar 20 '26 14:03

polygenelubricants