Tuesday 15 March 2011

arraylist - Java: loop and multiply -



arraylist - Java: loop and multiply -

i want loop through arraylist such way that,

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

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

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

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

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

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

i tried it's not 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 = 0; < 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

for(int = 0; < 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 output of

1*3 1*6 3*6

which wrong.

desired output

3*6 1*6 1*3

just start sec loop i+1 (and if not more needed)

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

edit:

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

java arraylist

No comments:

Post a Comment