我試圖解決一個問題,但我找不到為什么我的代碼不能解決這個問題。我生成了一個包含 100 個元素的隨機向量,我試圖將它們排列成另一個。不知何故,我新生成的向量充滿了隨機向量的最后一個索引值。int[] vetorAleatory = new int[100];for (int i = 0; i < vetorAleatory.length; i++) { vetorAleatory[i] = new Random().nextInt(1000);}int[] vetorByOrder = new int[100];int newVetorPosition = 0;for (int i = 0; i < 100; i++) { for (int x = 0; x < 100; x++) { vetorByOrder[newVetorPosition] = 2000; if (vetorAleatory[i] < vetorByOrder[newVetorPosition]) { boolean newEntry = true; for (int y = 0; y < newVetorPosition; y++) { if (vetorByOrder[y] == vetorByOrder[newVetorPosition]) { newEntry = false; break; } } if (newEntry == true) { vetorByOrder[newVetorPosition] = vetorAleatory[x]; } } if (x == 99) { newVetorPosition++; } }}for (int i = 0;i<100;i++) { System.out.print(vetorAleatory[i] + ", " + vetorByOrder[i] + System.lineSeparator());}
添加回答
舉報
0/150
提交
取消