+ 1

Can someone explain why this is not working?

import java.util.*; public class RandomList{ public static void main(String[] args){ Random rnd=new Random(); int[]arr= new int[5]; for(int i=0;i<5;i++) arr[i] = rnd.nextInt(10)+1; List<int> It= new ArrayList<int>(); for(int z:arr); It.add(z); } }

6th Mar 2017, 4:35 PM
Anthony
Anthony - avatar
7 Answers
+ 10
; after for each == empty statement > add is only executed once.
6th Mar 2017, 5:36 PM
Tashi N
Tashi N - avatar
+ 5
replace <int> with <Integer> Sorry man
6th Mar 2017, 4:38 PM
Meharban Singh
Meharban Singh - avatar
+ 2
@Enes Nope
11th Mar 2017, 8:24 PM
Meharban Singh
Meharban Singh - avatar
0
When I run that im getting an error message saying "RandomList.java:9" RandomList.java:9error: type List does not take parameters And ; expected at for(int i=0, i<5;i++;) ^
6th Mar 2017, 5:43 PM
Anthony
Anthony - avatar
0
@Anthony replace , with ; in for loop for(int i=0; i<5;i++)
6th Mar 2017, 6:01 PM
Meharban Singh
Meharban Singh - avatar
0
@meharban: thats what i was thinking. Im still getting the parameters error message. Thanks for trying to help though.
6th Mar 2017, 6:04 PM
Anthony
Anthony - avatar
0
Instead of 'List' in your ---> List<int> It= new ArrayList<int>(); shouldn't it be ArrayList<Integer> It= new ArrayList<Integer>(0); ?
11th Mar 2017, 8:23 PM
Enes
Enes - avatar