0

I want to loop through my arraylist such a way that,

if it's int i = 0 = b(0), it will eliminate b(0) and multiply

b.get(1)*b.get(2) = 3*6

if it's int i = 1 = b(1), it will eliminate b(1) and multiply

b.get(0)*b.get(2) = 1*6

if it's int i = 2 = b(2),it will eliminate b(2) and multiply

b.get(0)*b.get(1) = 1*3

I tried but it's not what i wanted

1*3
1*6
3*1
3*6
6*1
6*3

code

ArrayList<Integer> b = new ArrayList<Integer>();
b.add(1);
b.add(3);
b.add(6);

for(int i = 0; i < b.size(); i++) {
    for(int j = 0; j < b.size(); j++) {
        if(b.get(i) != b.get(j)) {
            System.out.println(b.get(i) + "*" + b.get(j));
        }
    }
}

By doing this

for(int i = 0; i < b.size(); i++) {
    for(int j = i+1; j < b.size(); j++) {
        if(b.get(i) != b.get(j)) {
            System.out.println(b.get(i) + "*" + b.get(j));
        }
    }
}

I will get output of

1*3
1*6
3*6

which is wrong.

Desired output

3*6
1*6
1*3

3 Answers 3

2

Just start the second loop from i+1 (and the if is not more needed)

for(int j = i+1; j < b.size(); j++)

EDIT:

for(int i = 0; i < b.size(); i++) {
    for(int j = 0; j < b.size(); j++){
        for(int k = j+1; k < b.size(); k++) {
           if(j != i && k != i)
               System.out.println(b.get(j) + "*" + b.get(k));
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think I didn't explain my question well enough, what I wanted is, if e.g I start the loop, start from int = 0 = b(0) which is 1, it will eliminate b(0)=1 and just multiply b(1) and b(2) so 3*6, now int i = 1 = b(1) so it will eliminate b(1) and multiply b(0) and b(2) so 1*6 and lastly now int = 2 = b(2) so it will eliminate b(2) and multiply b(0) and b(1) which is 1*3
by doing for(int j = i+1; j < b.size(); j++), I will get out of (1*3 1*6 3*6) which is wrong
1

Just set the j loop to start from

i + 1 

This will print the pairs only once.

2 Comments

I think I didn't explain my question well enough, what I wanted is, if e.g I start the loop, start from int = 0 = b(0) which is 1, it will eliminate b(0)=1 and just multiply b(1) and b(2) so 3*6, now int i = 1 = b(1) so it will eliminate b(1) and multiply b(0) and b(2) so 1*6 and lastly now int = 2 = b(2) so it will eliminate b(2) and multiply b(0) and b(1) which is 1*3
by doing for(int j = i+1; j < b.size(); j++), I will get out of (1*3 1*6 3*6) which is wrong
0

Assuming you do not care for order, all you want is to list all possible unordered pairs with distinct elements from the list of integers (1, 3, and 6) you provide.

So let us start by looping through the list for the first element:

for(int i = 0; i < b.size(); ++i) {
    // something with b.get(i)
}

Next we need the second element. If, for j, we start at index 0 again, we would need to check whether j is equal to i. In addition, when we get to some index greater than 0 with i, we would also loop with j through all elements smaller than i. This gives you the duplicates 6 * 3 and 3 * 6 and similar.

So we do not want j = i and we also do not want j < i. So we start with j = i +, which gives us all the pairings we want, and nothing more.

for(int i = 0; i < b.size(); ++i) {
    for(int j = i + 1; j < b.size(); ++j) {
        // something with b.get(i)
    }
}

If you draw a multiplication table

   |  1  |  3  |  6
 1 | 1*1 | 1*3 | 1*6
 3 | 3*1 | 3*3 | 3*6
 6 | 6*1 | 6*3 | 6*6

you see, that we are looking at values from a triangle. We start with 1*3, 1*6 (and more, if we had further integers), and continue in the next row one column furter right with 3*6.

1 Comment

I think I didn't explain my question well enough, what I wanted is, if e.g I start the loop, start from int = 0 = b(0) which is 1, it will eliminate b(0)=1 and just multiply b(1) and b(2) so 3*6, now int i = 1 = b(1) so it will eliminate b(1) and multiply b(0) and b(2) so 1*6 and lastly now int = 2 = b(2) so it will eliminate b(2) and multiply b(0) and b(1) which is 1*3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.