java中如何生成10个长度的随机数组并去除数组中重复的元素
展开全部
import java.util.Arrays;
public class Yugi {
public static void main(String[] args) {
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = (int)(Math.random() * n);
}
System.out.println(Arrays.toString(array));
String tmp = ",";
int[] src = new int[0];
for (int i = 0; i < n; i++) {
int a = array[i];
if (tmp.indexOf("," + a + ",") == -1) {
int[] dest = new int[src.length + 1];
System.arraycopy(src, 0, dest, 0, src.length);
dest[dest.length - 1] = a;
src = dest;
tmp += a + ",";
}
}
System.out.println(Arrays.toString(src));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询