[英]Printing distinct integers in an array
我正在尝试编写一个小程序来打印出数组中的不同数字。 例如,如果用户输入 1,1,3,5,7,4,3,程序将只打印出 1,3,5,7,4。
我在 function checkDuplicate
中的 else if 行出现错误。
到目前为止,这是我的代码:
import javax.swing.JOptionPane;
public static void main(String[] args) {
int[] array = new int[10];
for (int i=0; i<array.length;i++) {
array[i] = Integer.parseInt(JOptionPane.showInputDialog("Please enter"
+ "an integer:"));
}
checkDuplicate (array);
}
public static int checkDuplicate(int array []) {
for (int i = 0; i < array.length; i++) {
boolean found = false;
for (int j = 0; j < i; j++)
if (array[i] == array[j]) {
found = true;
break;
}
if (!found)
System.out.println(array[i]);
}
return 1;
}
}
最简单的方法是将所有元素添加到Set<Integer>
,然后只打印Set
的内容。
首先,“ else if
”语句是不正确的,因为你没有为 if 提供任何条件(如果你想要一个 if,你需要写“ if (condition)...
”)。
其次,您无法在内部循环内部决定是否应该打印一个值:您的代码的工作方式是为每个不同于 array[i] 的值 array[j]编写一个值 array[i]!
第三:内部循环只需要 go 从 0 到外部索引i-1
:对于每个元素,您只需要确定它是否是第一次出现(即相同的值是否出现在任何先前的索引处)。 如果是,打印出来,如果不是,忽略它。
CheckDuplicate()
的正确实现是:
public static void checkDuplicate(int array []) {
for (int i = 0; i < array.length; i++) {
boolean found = false;
for (int j = 0; j < i; j++)
if (array[i] == array[j]) {
found = true;
break;
}
if (!found)
System.out.println(array[i]);
}
}
但是当然,对于更大的 arrays 来说,某种Set
会更有效......
编辑:当然, mmyers (见评论)说得对,因为CheckDuplicate()
不返回任何值,它应该有返回类型void
(而不是int
)。 我在上面的代码中更正了这个......
将它们放入按插入时间排序的集合中,然后在必要时转换回数组。
new LinkedHashSet<Integer>(array).toArray()
尝试将所有整数放入一个集合中。 重复项永远不会添加到集合中,您将留下一组唯一整数。
您可以使用 Java 集合 API 来完成您想要的工作,但不完全是单线,因为事实收集方法适用于Object
s 而不是原语。 J2SE 缺少将int[]
转换为Integer[]
的方法,但 Apache Commons Lang库包含此类有用的方法,例如ArrayUtils.toObject()和ArrayUtils.toPrimitive() 。
使用它们,从 integer 数组中删除重复元素的方法如下所示:
public static int[] removeDuplicates(int... array) {
Integer[] ints = ArrayUtils.toObject(array);
Set<Integer> set = new LinkedHashSet<Integer>(Arrays.asList(ints));
return ArrayUtils.toPrimitive(set.toArray(new Integer[set.size()]));
}
如果您的应用程序可能包含更多的数组/集合操作,我建议您查看该库,而不是从头开始实现。 但是,如果您是出于学习目的而这样做,请远离编码!
将每个数字添加到Set实现而不是数组可能会更好。 集合专门用于存储要过滤掉重复项的元素的 collections。
要么按照其他人的建议使用 Set,要么使用与 List 兼容的 class。 使用列表兼容的 class 只需使用 Contains 方法检查它是否已存在于数组中。
导入 java.util.Scanner; 公共 class PrintDistinctNumbers {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int [] numberArray = createArray();
System.out.println("The number u entered are: ");
displayArray(numberArray);
getDistinctNumbers(numberArray);
}
public static int[] createArray() {
Scanner input = new Scanner(System.in);
int [] numberCollection = new int [10];
System.out.println("Enter 10 numbers");
for(int i = 0; i < numberCollection.length; i++){
numberCollection[i] = input.nextInt();
}
return numberCollection;
}
public static void displayArray(int[] numberArray) {
for(int i = 0; i < numberArray.length; i++){
System.out.print(numberArray[i]+" ");
}
}
public static void getDistinctNumbers(int[] numberArray) {
boolean isDistinct = true;
int temp = 0;
int [] distinctArrayNumbers = new int [10];
for ( int i = 0; i < numberArray.length; i++){
isDistinct = true;
temp = numberArray[i];
for( int j = 0; j < distinctArrayNumbers.length; j++){
if( numberArray[i] == distinctArrayNumbers[j] ){
isDistinct = false;
}
}
if(isDistinct){
distinctArrayNumbers[temp]=numberArray[i];
temp++;
}
}
displayDistinctArray(distinctArrayNumbers);
}
public static void displayDistinctArray(int[] distinctArrayNumbers) {
for( int i = 0; i < distinctArrayNumbers.length; i++){
if(distinctArrayNumbers[i] != 0){
System.out.println(distinctArrayNumbers[i]);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.