# 你好！我是Java的初学者，关于`Arrays`我有一个问题。

go评论64阅读模式

Hi! I'm a beginner in Java and I have a question regarding `Arrays`

# 问题

``````我是Java的初学者，关于`Arrays`我有一个问题。我一直在解决这个任务，任务要求：编写一个程序，生成1到10的十个随机排列。要生成随机排列，你需要填充一个数组，数组中包含从1到10的数字，确保数组中没有两个相同的数字。创建第二个数组，并填充数字1到10。重复进行10次，从第二个数组中随机选择一个元素，将其从数组中移除，并附加到排列数组中。在解决这个任务时，不允许使用ArrayList。

int[] myArray = {1，2，3，4，5，6，7，8，9，10};
int[] myArray2 = {1，2，3，4，5，6，7，8，9，10};
int[] permutation = new int[10];

for (int i = 0; i < myArray2.length - 1; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
if (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 9);
}
if (myArray2[ranIndex] > 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
``````

I'm a beginner in Java and I have a question regarding `Arrays`. I've been struggling with this task where it says: Write a program that produces ten random permutations of the numbers 1 to 10. To
generate a random permutation, you need to fill an array with the numbers 1 to 10
so that no two entries of the array have the same contents. Make a second array and fill it with the numbers 1 to 10. Repeat 10 times, Pick a random element from the second array. Remove it and append it to the permutation array. I am not allowed to use Arraylist to solve this task.

This is what I have done so far and my issue right now is that it prints out some zeroes while the rest of the numbers are all different like it should be: [2, 4, 10, 3, 9, 0, 0, 7, 5, 0].

What am I doing wrong, Am I even on the right way?
Any help would be appreciated!

``````    int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] permutation = new int[10];
for (int i = 0; i &lt; myArray2.length - 1; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
if (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 9);
}
if (myArray2[ranIndex] &gt; 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
``````

# 答案1

import java.util.Arrays;

public class Permutation {
public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] permutation = new int[10];

``````    for (int i = 0; i < myArray2.length; i++) {
int ranIndex = (int) (Math.random() * 10);
while (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 10);
}
if (myArray2[ranIndex] > 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
}
``````

}

You were close

``````import java.util.Arrays;
public class Permutation {
public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] permutation = new int[10];
for (int i = 0; i &lt; myArray2.length ; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
while (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 10);
}
if (myArray2[ranIndex] &gt; 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));
}
}
``````

Problems were :

• `i &lt; length` is enough, `i &lt; length - 1` discard the last element
• `Math.random()` is included `&gt;=0` and `&lt;1` if you multiply it by 9, you can't get the last element of myArray2 and thus have an infinite loop.
• `if (myArray2[ranIndex] == 0) {` should be replaced by `while (myArray2[ranIndex] == 0) {`. It was your main issue : in case the second attempt failed to find a new number in myArray2, it would leave 0 there. Replacing the `if` by `while` allows the code to try until it founds a new not used item !

• 本文由 发表于 2020年10月1日 23:09:45
• 转载请务必保留本文链接：https://go.coder-hub.com/64158226.html
• arrays
• java

go 63

go 67

go 63

go 119