我已经编写了以下短程序,提示用户构造一个整数ArrayList,然后显示删除了重复项的同一个数组。该程序可以很好地处理简单的数字,例如1,10甚至100个。请注意,排序不在此程序中,因为我只为自己编写排序。不过,我确实明白没有将程序分类的含义。从数组中删除重复项的程序不会删除某些项
略大的数字不能被正确检测为重复。例如,如果我选择构造一个具有两个整数的数组,两个数值均为700,则该方法不会删除重复项。是否有其他一些我不明白的概念,或者是我忽略的代码中的一个小错误?
import java.util.*;
public class DuplicateArray
{
public static void main(String [] args)
{
// Variables
Scanner scan = new Scanner(System.in);
// Prompt user and create an array
System.out.print("\nHow many items will your array contain: ");
int size = scan.nextInt();
ArrayList<Integer> numbers = new ArrayList<Integer>(size);
// Construct the array
for(int i = 0; i < size; i++)
{
System.out.print("Item #" + (i+1) + ": ");
numbers.add(scan.nextInt());
}
// Remove duplicates
compress(numbers);
// Print compressed array
System.out.println("\nBelow is that same array with duplicates removed.");
for(int i = 0; i <= numbers.size()-1; i++)
{
System.out.print(numbers.get(i) + " ");
}
}
/*
* Removes duplicates from the input array
*/
public static void compress(ArrayList<Integer> numbers)
{
// If the array is of size 1, then there are no duplicates to check for */
if(numbers.size() <= 1)
{
return;
}
// Traverse the array backwards */
for(int i = numbers.size() - 1; i > 0; i--)
{
if(numbers.get(i) == numbers.get(i-1))
{
numbers.remove(i);
}
}
}
}
我会使用'LinkedHashSet'(如果你需要保留插入顺序)或者一个常规'HashSet'(如果你不这样做)。 –
我还没有知道。我的代码中是否有逻辑错误? – AleksandrH
如果您对'List'进行排序,则您的方法将仅工作(按原样)。 –