比较java中相同数组的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23460367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:08:17  来源:igfitidea点击:

comparing elements of the same array in java

javaarrayscompareelementsbetween

提问by no-Name-Is-Still-A-Name

I am trying to compare elements of the same array. That means that i want to compare the 0 element with every other element, the 1 element with every other element and so on. The problem is that it is not working as intended. . What i do is I have two for loops that go from 0 to array.length-1.. Then i have an if statement that goes as follows: if(a[i]!=a[j+1])

我正在尝试比较同一数组的元素。这意味着我想将 0 元素与所有其他元素进行比较,将 1 元素与所有其他元素进行比较,依此类推。问题是它没有按预期工作。. 我所做的是我有两个从 0 到 array.length-1 的循环。然后我有一个 if 语句,如下所示: if(a[i]!=a[j+1])

for (int i = 0; i < a.length - 1; i++) {
    for (int k = 0; k < a.length - 1; k++) {
        if (a[i] != a[k + 1]) {
            System.out.println(a[i] + " not the same with  " + a[k + 1] + "\n");
        }
    }
}

采纳答案by Boris the Spider

First things first, you need to loop to < a.lengthrather than a.length - 1. As this is strictly less than you need to include the upper bound.

首先,您需要循环到< a.length而不是a.length - 1. 因为这严格小于您需要包括的上限。

So, to check all pairs of elements you can do:

因此,要检查您可以执行的所有元素对:

for (int i = 0; i < a.length; i++) {
    for (int k = 0; k < a.length; k++) {
        if (a[i] != a[k]) {
            //do stuff
        }
    }
}

But this will compare, for example a[2]to a[3]and then a[3]to a[2]. Given that you are checking !=this seems wasteful.

但这将比较,例如a[2]toa[3]和 then a[3]to a[2]。鉴于您正在检查!=这似乎很浪费。

A better approach would be to compare each element ito the rest of the array:

更好的方法是将每个元素与数组i其余部分进行比较:

for (int i = 0; i < a.length; i++) {
    for (int k = i + 1; k < a.length; k++) {
        if (a[i] != a[k]) {
            //do stuff
        }
    }
}

So if you have the indices [1...5] the comparison would go

所以如果你有索引 [1...5] 比较会去

  1. 1 -> 2
  2. 1 -> 3
  3. 1 -> 4
  4. 1 -> 5
  5. 2 -> 3
  6. 2 -> 4
  7. 2 -> 5
  8. 3 -> 4
  9. 3 -> 5
  10. 4 -> 5
  1. 1 -> 2
  2. 1 -> 3
  3. 1 -> 4
  4. 1 -> 5
  5. 2 -> 3
  6. 2 -> 4
  7. 2 -> 5
  8. 3 -> 4
  9. 3 -> 5
  10. 4 -> 5

So you see pairs aren't repeated. Think of a circle of people all needing to shake hands with each other.

所以你看到成对没有重复。想想一群人都需要互相握手。

回答by Sumedh

for (int i = 0; i < a.length; i++) {
    for (int k = 0; k < a.length; k++) {
        if (a[i] != a[k]) {
            System.out.println(a[i] + " not the same with  " + a[k + 1] + "\n");
        }
    }
}

You can start from k=1 & keep "a.length-1" in outer for loop, in order to reduce two comparisions,but that doesnt make any significant difference.

您可以从 k=1 开始并在外部 for 循环中保留“a.length-1”,以减少两次比较,但这不会产生任何显着差异。

回答by cooldude

Try this or purpose will solve with lesser no of steps

试试这个或目的将解决更少的步骤

for (int i = 0; i < a.length; i++) 
{
    for (int k = i+1; k < a.length; k++) 
    {
        if (a[i] != a[k]) 
         {
            System.out.println(a[i]+"not the same with"+a[k]+"\n");
        }
    }
}