Java随机化并从函数返回数组
我有这个功能Java随机化并从函数返回数组,java,arrays,Java,Arrays,我有这个功能 public static int[] createArray(){ int[] array = new int[5]; array = {5, 6, 8, 10, 0}; Collections.shuffle(Arrays.asList(array)); return array; } 似乎我无法直接按集合随机获得数组。如何正确地随机化和返回数组 首先将数组收集到列表,然后将其洗牌并将元素作为int[]返回。大概 public static i
public static int[] createArray(){
int[] array = new int[5];
array = {5, 6, 8, 10, 0};
Collections.shuffle(Arrays.asList(array));
return array;
}
似乎我无法直接按集合随机获得数组。如何正确地随机化和返回数组 首先将数组收集到
列表
,然后将其洗牌并将元素作为int[]
返回。大概
public static int[] createArray() {
int[] array = { 5, 6, 8, 10, 0 };
List<Integer> al = IntStream.of(array).boxed().collect(Collectors.toList());
Collections.shuffle(al);
return al.stream().mapToInt(Integer::intValue).toArray();
}
public static int[]createArray(){
int[]数组={5,6,8,10,0};
List al=IntStream.of(array.boxed().collect(Collectors.toList());
收藏。洗牌(al);
返回al.stream().mapToInt(Integer::intValue.toArray();
}
首先将数组收集到列表中,然后将其洗牌并将元素作为int[]
返回。大概
public static int[] createArray() {
int[] array = { 5, 6, 8, 10, 0 };
List<Integer> al = IntStream.of(array).boxed().collect(Collectors.toList());
Collections.shuffle(al);
return al.stream().mapToInt(Integer::intValue).toArray();
}
public static int[]createArray(){
int[]数组={5,6,8,10,0};
List al=IntStream.of(array.boxed().collect(Collectors.toList());
收藏。洗牌(al);
返回al.stream().mapToInt(Integer::intValue.toArray();
}
在无序移动之前,您需要存储对列表的引用,并且还要注意数组。asList(array)
将生成一个列表,而这不是您想要的
这里有一种方法:
static int[] createArray(){
int[] array = {5, 6, 8, 10, 0};
List<Integer> temp = Arrays.stream(array).boxed().collect(Collectors.toList());
Collections.shuffle(temp);
return temp.stream().mapToInt(Integer::intValue).toArray();
}
static int[]createArray(){
int[]数组={5,6,8,10,0};
List temp=Arrays.stream(array.boxed().collect(Collectors.toList());
收藏。洗牌(临时);
返回temp.stream().mapToInt(Integer::intValue.toArray();
}
在无序移动之前,您需要存储对列表的引用,并且还要注意数组。asList(array)
将生成一个列表,而这不是您想要的
这里有一种方法:
static int[] createArray(){
int[] array = {5, 6, 8, 10, 0};
List<Integer> temp = Arrays.stream(array).boxed().collect(Collectors.toList());
Collections.shuffle(temp);
return temp.stream().mapToInt(Integer::intValue).toArray();
}
static int[]createArray(){
int[]数组={5,6,8,10,0};
List temp=Arrays.stream(array.boxed().collect(Collectors.toList());
收藏。洗牌(临时);
返回temp.stream().mapToInt(Integer::intValue.toArray();
}
Arrays.asList(array)
复制数组Arrays.asList(array)
复制数组我不断得到“类型列表不是泛型的;它不能用参数参数参数化”@NooglersBIT确保您使用的是java.util.List代码>。是的,我有it@NooglersBIT好的,它在我的IDE上工作,我使用的是Java编译器版本1.7。问题是因为这个吗?我一直得到“类型列表不是泛型的;它不能用参数参数化”@NooglersBIT确保您使用的是java.util.List代码>。是的,我有it@NooglersBIT好的,它在我的IDE上工作,我使用的是Java编译器版本1.7。这是问题的原因吗?