The first thing you need to do is figure out how many different possible combinations that could exist. In this case, since you're choosing 2 numbers:
3 for 5
3 for 1
3 for 2
3 for 4
5 for 3
5 for 1
5 for 2
5 for 4
and so on so forth. Since each number has 4 other number is can be paired with we can figure out the total by doing the following equation: numberOfElementsInVector*4. In this case, we get 20. this means that we only need to loop for 20 times. We also need to keep track of what results we've already gathered. For that, we will store strings in an ArrayList. Now what we'll do is the following double for loop:
ArrayList<String> results = new ArrayList<>();
for(int i = 0; i < sol.length; i++){
for(int j = 0; j < sol.length; j++){
String result = sol[i] + "," + sol[j] + "";
if(!results.contains(result) && sol[i] != sol[j]){
results.add(result);
}
}
}
System.out.printLn(results.toString());
This code will systematically go through any length of Array and give you all 2 number combos.