Question

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 difference between List and an array and when you use them.

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");
                                }
                            }
                        }           
                    }
                }
Was it helpful?

Solution

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.

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 ^^

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

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

OTHER TIPS

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.

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

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.");
            }
        }
    }

}

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top