java - How to loop through a combination only once? -


I am trying to loop through an array, although I am facing this problem. Through this array I loop as:

  {1,2,3,4}   

I am facing this problem : During the beginning, I will get a combination of 1 and 4, although at the middle I will get a combination of 4 and 1. How can I create it so only unique relationships will be accepted? Nothing like {1,4} and {4,1} can be.

I am using Java, some of it has been answered, although they only use libraries available in other languages.

I can not wrap my head around so that unfortunately there is an attempt on the solution.

Here is the expected output after looping through the array:

  {1, 2} {1, 3} {1, 4} {2, 3} { 2, 4} {3, 4}   

But what actually happens here is looping through the array:

  {1, 1} {1 , 2} {1, 3} {1, 4} {2, 1} {2, 2} {2, 3} {2, 4} {3, 1} {3, 2} {3, 3} {3 , 4} {4, 1} {4, 2} {4, 3} {4, 4}   

There are two requirements that the couple should have a unique relationship (1,2 And can not be 2,1) and they can not be the same. Due to this, it can be done easily by comparing two numbers and if they are equal, then I am having trouble with the first requirement.

After your update, I believe that you are looking for something like this

  int [] arr = {1,2,3,4}; For (int i = 0; i & lt; arr.length; i ++) for (int j = i + 1; j & lt; arr.length; j ++) system.out.println ("{ "+ Arr [i] +", "+ arrival [j]"});   

Output:

  {1,2} {1,3} {1,4} {2,3} {2,4} {3 , 4}    

Comments