I would like to remove a particular number from the array
Integer[] arr = new Integer[7];
for (int i = 0; i < arr.length; i ) {
arr[i] = i;
}
Collections.shuffle(Arrays.asList(arr));
This is creating numbers from 0-7 But I dont need 0,I need value from 1-7
CodePudding user response:
The first value written into your array is 0 because you initialize i
with 0 in the for loop.
Therefore your loop will only insert the values 0 - 6.
Change this initialisation to i = 1
and in addition you also need to change the condition of the for loop to arr.length 1
or i <= arr.length
, so it will count up to 7.
Integer[] arr = new Integer[7];
for (int i = 1; i < arr.length 1; i ) {
arr[i] = i;
}
What you also can do instead of changing the loop itself, is to change the loop body. Just add 1 to i when assigning it to arr[i]:
for (int i = 0; i < arr.length; i ) {
arr[i] = i 1;
}
In this case i will count from 0 to 6, and assign 1 to 7 to your array
CodePudding user response:
Change your int i = 0
to int i = 1
like this:
Integer[] arr = new Integer[7];
for(int i = 1; i <8; i ){
int value = i-1;
arr[value] = i;
}
Collections.shuffle(Arrays.asList(arr));
for(int i = 0; i < arr.length; i ){
System.out.println("Result:" arr[i]);
}
Console message:
Result:7
Result:2
Result:6
Result:5
Result:4
Result:1
Result:3