+ 3
Can someone explain how to create an int list with 5 random numbers from 1-10?
I'm trying to create an int list with 5 random numbers each one up to 10
13 Respostas
+ 1
public class random
{
public static void main(String args[])
{
int i,n;
for(i=1;i<=5;i++)//as you want 5 random no.s
{
n=(int)((Math.random()*(10-1))+1);
System.out.println(n);
}
}
}
0
import java.util.*;
Random rnd=new Random();
int[] arr=new int[5];
for(int i=0;i<5;i++)
arr[i] = rnd.nextInt(10) +1;
List<Integer> lt= new ArrayList<Integer>();
for(int z:arr)
lt.add(z);
0
When I run that im getting an error message saying "RandomList.java:12:error: unexpected type
List<int> It=new ArrayList<int>();
^
Required: reference
Found: int
RandomList.java:12 error: type List does not take parameters
0
It does not work in Sololearn bcoz it does not have List libraries
0
It does not work in Sololearn bcoz it does not have Collections
0
Im using it in Jgrasp and it still is not working
0
Its working fine for me on eclipse.
0
Hmm, its still giving me the same error message about List<Interger> not taking parameters. Thanks anyway
0
Have you imported collections and random class?
0
Im new to this, so im not really sure. I imported all in java util with the sentex "import java.util*;"
Is this not how you import these classes as well? I thought if you use the asterisk, you're importing these classes as well.
0
yes, import java.util.*;
0
Yea i used that but was still unsuccessfull
0
@Anthony, There are 2 types of list.
import java.awt.List;
and
import java.util.List;
You need second one.
So use,
import java.util.List;
import java.util.Random;