比较同一List Java中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23535346/
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:41:36  来源:igfitidea点击:

Comparing elements in the same List Java

javalistarraylist

提问by Anton James

I want to compare the elements in the same List (The List "list)and if they are the same print out a line, I'm fairly new to programming and I dont yet completely understand the differencebetween Listand an arrayand when you use them.

我想比较相同的元素,List (The List "list)如果它们相同,则打印出一行,我对编程还很陌生,我还没有完全理解List数组之间的区别以及何时使用它们。

Someone kindly suggested the code below and although it compliles nothing is being printed out. Since there are two pairs in my array I would expect something to print out at least once. Any help would be most appreciated thanks!

有人好心地建议了下面的代码,虽然它编译了但没有打印出来。由于我的数组中有两对,我希望至少打印一次。任何帮助将不胜感激谢谢!

                        List<Double> list = new ArrayList<Double>();

                        while (search.find()){ 
                            list.add(Double.parseDouble(search.group(1)));
                        }


                        for(int i= 0 ; i < list.size()-1; i++){
                            //System.out.println(list.get(i));  // Successfully prints out all doubles in the SHADING:BUILDING:DETAILED class
                            for(int k = i+1 ; k < list.size() ; k++){
                                if(list.get(i) == list.get(k)){
                                    System.out.println(i + "and" + k + "are pairs");
                                }
                            }
                        }           
                    }
                }

采纳答案by fasaas

The way you have coded the comparation repeats itself. Besides, that is not the correct way you access an element in a list, I'm afraid.

您对比较进行编码的方式会重复。此外,恐怕这不是您访问列表中元素的正确方式。

For example, lets say you have the i = 0 (value 2.0 for example) and k = 2 (value 5.2), but later you have i = 2 (5.2) and k = 0 (2.0), which is the same. I suggest you the following, which avoids this repetition.

例如,假设您有 i = 0(例如值 2.0)和 k = 2(值 5.2),但后来您有 i = 2 (5.2) 和 k = 0 (2.0),这是相同的。我建议你以下,以避免这种重复。

for (int i = 0; i < list.size()-1; i++) 
   for (int k = i+1; k < list.size(); k++) 
      if(list.get(i) == list.get(k))
         System.out.println(i + " and "+ k +" are pairs");

Of course, you have to make sure that the list's size is bigger than 1, otherwise it will throw OutOfBoundsException. Hope this helps ^^

当然,你必须确保列表的大小大于1,否则会抛出OutOfBoundsException。希望这有帮助^^

EDIT #2 Try and use the following if, it contains a null-safe equals:

编辑 #2 如果它包含一个空安全等于,请尝试使用以下内容:

if(Objects.equals(list.get(i), list.get(k)))

回答by Andrew Vitkus

A list is basically just a variable length array. You can access data from it with the get(int) method which gets the n-th value from the list and set(int, Object) to set the n-th value to the given object.

列表基本上只是一个可变长度的数组。您可以使用 get(int) 方法访问其中的数据,该方法从列表中获取第 n 个值,并使用 set(int, Object) 将第 n 个值设置为给定对象。

The javadocs are your friends. http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

javadocs 是您的朋友。http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

回答by alfasin

First, you should fix:

首先,您应该修复:

if(list[i]=  list[k]){

To be:

成为:

if(list.get(i) == list.get(k)){

Second, you should add a condition to make sure i != k(or in other words - you're not comparing an element to itself.

其次,您应该添加一个条件来确保i != k(或者换句话说 - 您没有将元素与其自身进行比较。

And third, you can't access a list like you do: list[i]you should use list.get(i)

第三,你不能像你那样访问列表:list[i]你应该使用list.get(i)

回答by David Navarro Astudillo

You can use the following code:

您可以使用以下代码:

public void compareElementToList() {

    List<Double> list = Arrays.asList(1.0, 1.2, 1.3, 1.4, 1.0);

    for (int i = 0; i < list.size(); i++) {
        Double itemI = list.get(i);
        for (int j = 0; j < list.size(); j++) {
            Double itemJ = list.get(j);
            if (i != j) {
                if (itemI.compareTo(itemJ) == 0) {
                    System.out.println("Element " + i + " and " + j + " are pairs.");
                } else {
                    System.out.println("There are no even elements.");
                }
            } else {
                System.out.println("Element omited.");
            }
        }
    }

}