Java 使用与旧阵列相同的数字创建阵列,但不重复
所以我用随机数创建了一个数组,我打印并计算了重复的数,现在我只需要用第一个数组中相同的数创建一个新数组,但不需要任何重复。顺便说一下,不能使用ArrayList 我所拥有的是Java 使用与旧阵列相同的数字创建阵列,但不重复,java,arrays,Java,Arrays,所以我用随机数创建了一个数组,我打印并计算了重复的数,现在我只需要用第一个数组中相同的数创建一个新数组,但不需要任何重复。顺便说一下,不能使用ArrayList 我所拥有的是 public static void main(String[] args) { Random generator = new Random(); int aR[]= new int[20]; for(int i=0;i<aR.length;i++){ int number
public static void main(String[] args) {
Random generator = new Random();
int aR[]= new int[20];
for(int i=0;i<aR.length;i++){
int number=generator.nextInt(51);
aR[i]=number;
System.out.print(aR[i]+" ");
}
System.out.println();
System.out.println();
int countRep=0;
for(int i=0;i<aR.length;i++){
for(int j=i+1;j<aR.length-1;j++){
if(aR[i]==aR[j]){
countRep++;
System.out.println(aR[i]+" "+aR[j]);
break;
}
}
}
System.out.println();
System.out.println("Repeated numbers: "+countRep);
int newaR[]= new int[aR.length - countRep];
}
publicstaticvoidmain(字符串[]args){
随机生成器=新随机();
int aR[]=新int[20];
对于(int i=0;i尝试:
Set insertedNumbers=newhashset(newaR.length);
int指数=0;
对于(int i=0;i
使用Java 8和streams,您可以执行以下操作:
int[] array = new int[1024];
//fill array
int[] arrayWithoutDuplicates = Arrays.stream(array)
.distinct()
.toArray();
这将:
将int[]
转换为IntStream
过滤掉所有重复项,以便保留不同的元素
将其保存在类型为int[]
的新数组中
一种可能的方法是遍历数组,对于每个值,计算它在数组中再次出现的索引(如果该数字不再出现,则为-1)。不再出现的值的数量是唯一值的数量。然后从对应索引为-1的数组中收集所有值
import java.util.Arrays;
import java.util.Random;
public class UniqueIntTest
{
public static void main(String[] args)
{
int array[] = createRandomArray(20, 0, 51);
System.out.println("Array " + Arrays.toString(array));
int result[] = computeUnique(array);
System.out.println("Result " + Arrays.toString(result));
}
private static int[] createRandomArray(int size, int min, int max)
{
Random random = new Random(1);
int array[] = new int[size];
for (int i = 0; i < size; i++)
{
array[i] = min + random.nextInt(max - min);
}
return array;
}
private static int[] computeUnique(int array[])
{
int indices[] = new int[array.length];
int unique = computeIndices(array, indices);
int result[] = new int[unique];
int index = 0;
for (int i = 0; i < array.length; i++)
{
if (indices[i] == -1)
{
result[index] = array[i];
index++;
}
}
return result;
}
private static int computeIndices(int array[], int indices[])
{
int unique = 0;
for (int i = 0; i < array.length; i++)
{
int value = array[i];
int index = indexOf(array, value, i + 1);
if (index == -1)
{
unique++;
}
indices[i] = index;
}
return unique;
}
private static int indexOf(int array[], int value, int offset)
{
for (int i = offset; i < array.length; i++)
{
if (array[i] == value)
{
return i;
}
}
return -1;
}
}
导入java.util.array;
导入java.util.Random;
公共类唯一性测试
{
公共静态void main(字符串[]args)
{
int-array[]=createRandomArray(20,0,51);
System.out.println(“数组”+数组.toString(数组));
int result[]=computeUnique(数组);
System.out.println(“结果”+数组.toString(结果));
}
私有静态int[]createRandomArray(int size、int min、int max)
{
随机数=新随机数(1);
int数组[]=新的int[size];
对于(int i=0;i
这听起来像是一个家庭作业问题,如果是的话,你应该学习的技巧是先对数组排序
数组排序后,重复项将彼此相邻,因此很容易找到:
int[] numbers = //obtain this however you normally would
java.util.Arrays.sort(numbers);
//find out how big the array is
int sizeWithoutDuplicates = 1; //there will be at least one entry
int lastValue = numbers[0];
//a number in the array is unique (or a first duplicate)
//if it's not equal to the number before it
for(int i = 1; i < numbers.length; i++) {
if (numbers[i] != lastValue) {
lastValue = i;
sizeWithoutDuplicates++;
}
}
//now we know how many results we have, and we can allocate the result array
int[] result = new int[sizeWithoutDuplicates];
//fill the result array
int positionInResult = 1; //there will be at least one entry
result[0] = numbers[0];
lastValue = numbers[0];
for(int i = 1; i < numbers.length; i++) {
if (numbers[i] != lastValue) {
lastValue = i;
result[positionInResult] = i;
positionInResult++;
}
}
//result contains the unique numbers
int[]numbers=//以通常的方式获取
java.util.array.sort(数字);
//了解阵列有多大
int sizeWithoutDuplicates=1;//将至少有一个条目
int lastValue=数字[0];
//数组中的数字是唯一的(或第一个重复)
//如果它不等于它前面的数字
对于(int i=1;i
不能使用列表意味着我们必须在单独的过程中计算出数组的大小-如果我们可以使用ArrayList来收集结果,我们只需要在数字数组中进行一个循环
这种方法比通过数组查找重复项的双嵌套循环更快(O(n logn)vs O(n^2)).我猜如果你不允许使用ArrayList
你也不能使用HashSet
,对吧?对我来说这听起来很像一个家庭作业问题。问题是什么?如果你不允许使用合理的东西,比如集合或散列,那么这就是一个简单的嵌套迭代问题,除非需求中还说明了非重复数组的大小必须准确无误。是的,它必须是正确的大小。您在这里特别需要什么帮助?因为它看起来像是您启动的。那么代码是否因某种原因不起作用?或者…?这绝对是您在生产代码中应该做的事情(与作业代码相反:-D).出于好奇,我检查了源代码,这实际上使用了一个隐藏的LinkedHashSet。如果这不是一个家庭作业问题,那么你应该使用@skiwi的答案-这是最简单、最有效的方法,在现实生活中没有理由不使用哈希集。你有什么特别的原因恢复编辑吗?你肯定不需要要执行包含检查,HashSet已经为您这样做了。原因是集
不保留原始数组的顺序。我同意您的看法,即HashSet
进行检查。这实际上就是我使用它的原因:确保在
int[] numbers = //obtain this however you normally would
java.util.Arrays.sort(numbers);
//find out how big the array is
int sizeWithoutDuplicates = 1; //there will be at least one entry
int lastValue = numbers[0];
//a number in the array is unique (or a first duplicate)
//if it's not equal to the number before it
for(int i = 1; i < numbers.length; i++) {
if (numbers[i] != lastValue) {
lastValue = i;
sizeWithoutDuplicates++;
}
}
//now we know how many results we have, and we can allocate the result array
int[] result = new int[sizeWithoutDuplicates];
//fill the result array
int positionInResult = 1; //there will be at least one entry
result[0] = numbers[0];
lastValue = numbers[0];
for(int i = 1; i < numbers.length; i++) {
if (numbers[i] != lastValue) {
lastValue = i;
result[positionInResult] = i;
positionInResult++;
}
}
//result contains the unique numbers