Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing List<Integer> by index

    List<Integer> listOne= new ArrayList<>();
    listOne.add(10);
    listOne.add(2);
    listOne.add(3);

    //Second Array

    List<Integer> listTwo= new ArrayList<>();
    listTwo.add(3);
    listTwo.add(7);
    listTwo.add(1);

I want to compare two List and give point to listOneor listTwo depending on which value is greater

[10, 2, 3] compare to [3, 7, 1]
if listOne.get(0) > listTwo.get(0) //add one point to listOne

if listOne.get(0) < listTwo.get(0) //add one point to listTwo

Here is my tested code

    static List<Integer> compareList(List<Integer> a, List<Integer> b) {
        ArrayList<Integer> output = new ArrayList<>();
        output.add(0, 0);
        output.add(1, 0);
        int out = output.get(0);
        int out2 = output.get(1);
        for (int i = 0; i < a.size(); i++) {
            if (a.get(i) > b.get(i)) {
                out = out + 1;
            } 
            if (a.get(i) < b.get(i)) {
                out2 = out2 + 1;
            } 
        }

        output.add(0, out);
        output.add(1, out2);
        return output;

    }

It works, but its not outputting the expected answer

expected output : 2 1 output am getting : 2 1 0 0

like image 657
steveOS Avatar asked Dec 24 '19 12:12

steveOS


People also ask

Can you use == to compare integers?

To compare integer values in Java, we can use either the equals() method or == (equals operator). Both are used to compare two values, but the == operator checks reference equality of two integer objects, whereas the equal() method checks the integer values only (primitive and non-primitive).

How do you compare two integer values?

compare(int x, int y) compare() compares two int values numerically and returns an integer value. If x>y then the method returns an int value greater than zero. If x=y then the method returns zero.

How do you compare integers in an array?

equals(int[] a, int[] a2) method returns true if the two specified arrays of ints are equal to one another. Two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.

How do you compare integers and integers?

In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.


Video Answer


3 Answers

You are adding two 0s to the List, and then you are adding two more numbers at the start of the List, in addition to the 0s.

Either remove these statements:

output.add(0, 0);
output.add(1, 0);

and keep these statements unchanged:

output.add(0, out);
output.add(1, out2);

Or just change these statements:

output.add(0, out);
output.add(1, out2);

to:

output.set(0, out);
output.set(1, out2);
like image 176
Eran Avatar answered Oct 11 '22 16:10

Eran


When you do

output.add(0, out);
output.add(1, out2);

You add items to indices 0 and 1, which push the previously entered zeroes to indices 2 and 3. output is now [2, 1, 0, 0].

Remove

output.add(0, 0);
output.add(1, 0);

And change out and out2 initialization to

int out = 0;
int out2 = 0;

Now you don't need to add the results by index either

output.add(out);
output.add(out2);
like image 42
Guy Avatar answered Oct 11 '22 17:10

Guy


If you remove:

output.add(0, 0);
output.add(1, 0);

Works:

static List<Integer> compareList(List<Integer> a, List<Integer> b) {
    ArrayList<Integer> output = new ArrayList<>();
    int out = 0;
    int out2 = 0;
    for (int i = 0; i < a.size(); i++) {
        if (a.get(i) > b.get(i)) {
            out = out + 1;
        }
        if (a.get(i) < b.get(i)) {
            out2 = out2 + 1;
        }
    }

    output.add(0, out);
    output.add(1, out2);
    return output;
}
like image 1
DaniDekk Avatar answered Oct 11 '22 17:10

DaniDekk