你正在尋找獨特對,不只是唯一的編號。因此,第一步是創建一個類來表示一對。
public class Pair {
public final int first;
public final int second;
public Pair(int a, int b) {
first = a;
second = b;
}
public boolean equals(Object o) {
if (! (o instanceof Pair)) return false;
Pair p = (Pair)o;
return first == p.first && second == p.second;
}
public int hashCode() {
return first + second << 16;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public String[] toStringArr() {
String[] s = new String[2];
s[0] = "" + first;
s[1] = "" + second;
return s;
}
}
從那裏,你可以把你的輸入,以對,做加工的需要,並變回字符串數組。
public static void main(String args[]){
int[] arr = { 1, 2, 1, 2, 5, 6, 1, 2, 8, 9, 5, 6, 8, 9 };
Set<Pair> set = new HashSet<>();
for(int i = 0; i < arr.length-1; i+=2) {
set.add(new Pair(arr[i], arr[i+1]));
}
String[][] arr2 = new String[set.size()][];
int i = 0;
for(Pair p : set) {
arr2[i] = p.toStringArr();
}
//Unique pairs now in string array arr2.
}
您對二維數組的期望輸出格式是什麼? – mattymanme
什麼是邏輯具有如此.. – Moumit
如果它的1,2,8,9,5,6,1,2,8,9個獨特的nos對應存儲在[x] [2 ] –