Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确定数组中的重复元素?_Java_Arrays - Fatal编程技术网

Java 如何确定数组中的重复元素?

Java 如何确定数组中的重复元素?,java,arrays,Java,Arrays,我尝试在for循环期间比较数组的元素。在我的例子中,由于某些原因,我无法删除一些重复的值,但我必须确定它们 这就是我所拥有的,它对我不起作用 int x; int table[] = {1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10}; for (x = 0; x <= table.length; x++) { if (x == 0 || x + 1 < table.length) { //determine that element is n

我尝试在for循环期间比较数组的元素。在我的例子中,由于某些原因,我无法删除一些重复的值,但我必须确定它们

这就是我所拥有的,它对我不起作用

int x;
int table[] = {1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10};

for (x = 0; x <= table.length; x++) {
    if (x == 0 || x + 1 < table.length) { //determine that element is not last
        if (table[x] == table[x + 1]) { //determine if next element is the same
            System.out.println(table[x] + "if x = x + 1");
        }

        //determine that element is equal to previous but not equal to next
        if (table[x] != table[x + 1] && table[x] == table[x - 1]) {
            System.out.println(table[x] + " if x != x + 1 but x = x - 1");
        } else {
            System.out.println(table[x]);
        }
    }

    if (x + 1 == table.length) { //determine that element is last
        System.out.println(table[x]);
    }
}
intx;
int table[]={1,2,2,3,4,5,5,6,7,8,9,9,10};

对于(x=0;x我看到您的代码中有许多错误

int x;
int table[] ={1,2,2,3,4,5,5,6,7,8,9,9,10};

for (x = 0; x < table.length; x++){ // <-- NOT <=
  if (x + 1 < table.length){ //<-- what if the table only has one element?
    if (table[x] == table[x + 1]) { // <-- Add a {, Java is hard to read without
             //                            braces.
      System.out.println(table[x] + "if x = x + 1");
    } else if (x > 0 && table[x] == table[x - 1]) { // <-- check that x is
                                     // greater then 0 before subtracting 1
        System.out.println(table[x] + " if x != x + 1 but x = x - 1");
    } else {
        System.out.println(table[x]);
    }
    if (x + 1 == table.length){ //determine that element is last
      System.out.println(table[x]);
    }
  }
}
intx;
int表[]={1,2,2,3,4,5,5,6,7,8,9,9,10};

对于(x=0;x
  • 将数组转换为列表对象(例如ArrayList)
  • 然后构造一个新的集合实例(例如HashSet),并将第一步中的列表作为构造函数参数提供。集合将只包含唯一的整数,您可以继续单独使用该集合,或者如果要查找重复的,请执行下一步
  • 很遗憾,您无法使用“removeAll”方法获取重复项。您可能必须在步骤2中对集合编写for循环,并在步骤1的列表上调用remove方法。由于“remove”方法只删除第一次出现的项,因此您应该得到一个仅包含重复项的列表

  • 此外,如果您愿意添加对guava或apache commons集合的依赖,它们都有一个很好的Multimap实现,可以帮助您解决这个问题。

    您可以使用HasmMap来解决这个问题。 如果您有一个数组,如[1,2,2,3,4,5,1,8,4]
    它会给你一张地图:{[1->(0,6)],[2->(1,2)],[3->(3)],[4->(4,8)],[5->(5)],[8->(7)]

    //从数字映射到存在重复数字的数组中的索引
    HashMap duplicates=新的HashMap();
    对于(int i=0;i
    您可以尝试以下代码

    HashSet hs = new HashSet();
    int table[] ={1,2,2,3,4,5,5,6,7,8,9,9,10};
    for (int x = 0; x < table.length; x++)
    {
        hs.add(table[x]);
    }
    System.out.println(hs);
    List<Integer> array = new ArrayList<Integer>(hs);
    System.out.println(array);
    
    HashSet hs=newhashset();
    int表[]={1,2,2,3,4,5,5,6,7,8,9,9,10};
    对于(int x=0;x

    现在你可以知道了…

    你的表保证会被排序吗?或者,换句话说:如果有重复的,它们会一起出现在表中吗?请进一步解释这个问题。我不理解要求。仅供参考:上面的代码只有在你有一个排序数组的情况下才起作用。是的,我会将排序后的数据放在其中@ajbI have data that will be加载到数组中,它里面有dublicate元素。我需要在不删除它的情况下确定这些元素。我可以使用hashSetit@MultithreaderUmmm,您确定
    {
    第6行是否应该添加?您刚刚修复了一些问题,以便下一个
    if
    永远不会为真,因为它的第一部分与第6行的
    if
    条件相矛盾。我认为不添加
    是正确的{
    在那里,但他的缩进太草率了。@ajb我也得出了这个结论。编辑。@ElliottFrisch我检查元素在循环
    if(x==0 | | x+1
    @IlgarMamedov我看到了。当x==table.length时,你会怎么做?为什么要这样做?@ElliottFrisch当我到达最后一个元素时,我只是打印它并打断它…我忘了放'break'语句)
    HashSet hs = new HashSet();
    int table[] ={1,2,2,3,4,5,5,6,7,8,9,9,10};
    for (int x = 0; x < table.length; x++)
    {
        hs.add(table[x]);
    }
    System.out.println(hs);
    List<Integer> array = new ArrayList<Integer>(hs);
    System.out.println(array);