This question already has an answer here:
这个问题在这里已有答案:
- Java Remove Duplicates from an Array? 9 answers
Java从数组中删除重复项? 9个答案
I am trying to write a program which will generate a random ten integer array(integers between 1 and 6) and then I have to form another array with all duplicates removed. So {1,3,5,5,3,4,2,2,2,1} should return {1,3,5,4,2}. The problem is that I get an answer but the output array contains 0s in the places where the duplicates were and I do not know how to decrease the length of the temp array(if it is even possible). Here is my program.:
我正在尝试编写一个程序,它将生成一个随机的十个整数数组(1到6之间的整数),然后我必须形成另一个数组,删除所有重复项。所以{1,3,5,5,3,4,2,2,2,1}应该返回{1,3,5,4,2}。问题是我得到了答案,但输出数组在重复项所在的位置包含0,我不知道如何减少临时数组的长度(如果可能的话)。这是我的计划:
import java.util.*;
public class Lab9Tut12{
public static void main (String[]args){
int [] numbers = new int[10];
//int length = 10;
int[] temp = new int[length];
for(int i=0;i<10;i++){
numbers [i] = (int)(Math.random()*6+1);
System.out.print(numbers [i]);
System.out.println();
}
for(int i=1;i<10;i++){
if(numbers[i-1]!=numbers[i]){
temp[i]= numbers[i];
//length--;
}
}
System.out.println(Arrays.toString(temp));
}
}
5 个解决方案
#1
6
A nice way to do this is to utilize a Set. That's a structure, that contains only unique values.
一个很好的方法是使用Set。这是一个只包含唯一值的结构。
Set<Integer> set = new HashSet<Integer>();
int[] array = {1,1,2,2,2,3,3,4,5,6,8};
for (int num : array) {
set.add(num);
}
System.out.println(set);
Outputs:
[1, 2, 3, 4, 5, 6, 8]
To convert the set to an array you can use set.toArray()
.
要将集转换为数组,可以使用set.toArray()。
#2
3
Use Set
instead. Put all the array values in a set and then convert back to array.
请改用Set。将所有数组值放在一个集合中,然后转换回数组。
Set<Integer> numbersSet = new HashSet()<> (Arrays.asList(numbers));
Integer[] uniqueNumbers = numbersSet.toArray(new Integer[0]);
Set will eliminate all you duplicates and you don't need to do anything for it. Just put the numbers there.
Set将消除所有重复项,您无需为此做任何事情。把数字放在那里。
#3
1
You could use a Set
to store your unique random numbers. Set API
您可以使用Set来存储唯一的随机数。设置API
Set<Integer> set = new HashSet<Integer>();
set.add(randomNumber);
...
Later convert to a list:
稍后转换为列表:
List<Integer> list = new ArrayList<Integer>(set);
#4
1
Try use this piece of code. Set does not allow you to put 2 same objects.
尝试使用这段代码。 Set不允许您放置2个相同的对象。
import java.util.HashSet;
import java.util.Set;
public class MyClass {
public static void main(String[] args) {
int size = 10;
Set<Integer> numbers = new HashSet<Integer>();
for (int i = 0; i < size; i++) {
numbers.add((int) (Math.random() * 6 + 1));
}
System.out.println(numbers);
}
}
#5
1
Using a Set
is nice, however you'll have a problem: its .toArray()
will return an Integer[]
, not an int[]
(and you cannot have a Set<int>
).
使用Set是很好的,但是你会有一个问题:它的.toArray()将返回一个Integer [],而不是一个int [](并且你不能有一个Set
Here is a solution which still uses a set, but differently:
这是一个仍然使用集合的解决方案,但不同:
public static int[] onlyUniqueElements(final int[] inputArray)
{
final Set<Integer> set = new HashSet<>();
final int[] tmp = new int[inputArray.length];
int index = 0;
for (final int i: inputArray)
if (set.add(i))
tmp[index++] = i;
return Arrays.copyOfRange(tmp, 0, index);
}
#1
6
A nice way to do this is to utilize a Set. That's a structure, that contains only unique values.
一个很好的方法是使用Set。这是一个只包含唯一值的结构。
Set<Integer> set = new HashSet<Integer>();
int[] array = {1,1,2,2,2,3,3,4,5,6,8};
for (int num : array) {
set.add(num);
}
System.out.println(set);
Outputs:
[1, 2, 3, 4, 5, 6, 8]
To convert the set to an array you can use set.toArray()
.
要将集转换为数组,可以使用set.toArray()。
#2
3
Use Set
instead. Put all the array values in a set and then convert back to array.
请改用Set。将所有数组值放在一个集合中,然后转换回数组。
Set<Integer> numbersSet = new HashSet()<> (Arrays.asList(numbers));
Integer[] uniqueNumbers = numbersSet.toArray(new Integer[0]);
Set will eliminate all you duplicates and you don't need to do anything for it. Just put the numbers there.
Set将消除所有重复项,您无需为此做任何事情。把数字放在那里。
#3
1
You could use a Set
to store your unique random numbers. Set API
您可以使用Set来存储唯一的随机数。设置API
Set<Integer> set = new HashSet<Integer>();
set.add(randomNumber);
...
Later convert to a list:
稍后转换为列表:
List<Integer> list = new ArrayList<Integer>(set);
#4
1
Try use this piece of code. Set does not allow you to put 2 same objects.
尝试使用这段代码。 Set不允许您放置2个相同的对象。
import java.util.HashSet;
import java.util.Set;
public class MyClass {
public static void main(String[] args) {
int size = 10;
Set<Integer> numbers = new HashSet<Integer>();
for (int i = 0; i < size; i++) {
numbers.add((int) (Math.random() * 6 + 1));
}
System.out.println(numbers);
}
}
#5
1
Using a Set
is nice, however you'll have a problem: its .toArray()
will return an Integer[]
, not an int[]
(and you cannot have a Set<int>
).
使用Set是很好的,但是你会有一个问题:它的.toArray()将返回一个Integer [],而不是一个int [](并且你不能有一个Set
Here is a solution which still uses a set, but differently:
这是一个仍然使用集合的解决方案,但不同:
public static int[] onlyUniqueElements(final int[] inputArray)
{
final Set<Integer> set = new HashSet<>();
final int[] tmp = new int[inputArray.length];
int index = 0;
for (final int i: inputArray)
if (set.add(i))
tmp[index++] = i;
return Arrays.copyOfRange(tmp, 0, index);
}