Hi guys,im learning java and i have an assigment which says
"Make an array with 20 int and replace the first index with the 10th and so on,"
Ive wrtiing the code,but it somewhat works only when i use 8as my first index insted of 9
so..um..whats wrong with it?
package page48; import java.util.Arrays; import java.util.Random; public class Drill4 { public static void main(String[] args) { int[] array = new int[20]; Random rand = new Random(); int temp = 0; for (int i = 0; i < array.length; i++) { array[i] = rand.nextInt(20) + 1; } System.out.println(Arrays.toString(array)); for (int i = 8; i < array.length; i++) { for (int j = 0; j < array.length; j++) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } System.out.println(Arrays.toString(array)); } }
The console::
[16, 6, 9, 8, 4, 5, 8, 16, 15, 14, 18, 6, 12, 18, 19, 13, 7, 19, 13, 1]
[18, 6, 12, 18, 19, 13, 7, 19, 13, 1, 16, 15, 16, 6, 9, 8, 4, 5, 8, 14]