讓我們通過Matlab代碼並將每行轉換爲Java。我們將需要一些抽象,我們將在旅途中介紹這些抽象。
第一行:
x = [1,2,3,4,5,6,7];
的載體被分配給一個變量x
。我們可以簡單地說矢量是一個整數數組,但也許我們稍後需要一些更好的抽象。讓我們定義一個新類Vector
。不要將它與java.util.Vector
混淆:可能有多個相同的非限定名稱的類。
class Vector {
private int[] value;
Vector(int... value) {
this.value = value;
}
int apply(int i) {
return value[i - 1];
}
int length() {
return value.length;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
String prefix = "";
for (int entry : value) {
result.append(prefix).append(entry);
prefix = " ";
}
return result.toString();
}
}
我們使用整數數組作爲Vector
的內部表示。請注意,只要不泄漏到類的界面中,您可以隨時換出內部表示。因此,我們將我們的value
成員的訪問權限限制爲private
,這意味着只允許Vector
類型的對象訪問它。
新建Vector
對象通過調用構造函數Vector(int... value)
來實例化,該構造函數採用可變參數整型參數。內部在Java中,可變參數是相同的陣列,但他們給我們的語法糖,來實例化x
以下列方式:
Vector x = new Vector(1, 2, 3, 4, 5, 6, 7);
看起來非常相似,你的Matlab代碼。
另一件事是,在Java中,數組是零索引的,而Matlab開始索引爲1.我們的Vector
類定義了一個apply
-方法,它應該訪問i
的索引。因此,它返回value[i-1]
。
現在我們要計算
x = perms(x);
perms
返回一個矩陣,含有載體x
的所有排列。所以我們需要其他抽象:Matrix
。
class Matrix {
private Vector[] rows;
Matrix(Vector... value) {
this.rows = value;
}
int apply(int x, int y) {
return rows[x - 1].apply(y);
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
String prefix = "";
for (Vector row : rows) {
result.append(prefix).append(row.toString());
prefix = System.lineSeparator();
}
return result.toString();
}
}
Matrix
被定義非常類似於Vector
,但其內部表示的Vector
陣列,該矩陣的行。再次,我們定義一個方法apply
來檢索一個元素:這一次,它需要兩個參數,即行索引和列索引。
備註:重寫方法toString
總是好的,它在Java的類型層次結構的頂層元素中定義:Object
。您可以嘗試實例化Vector
或Matrix
,並將其作爲參數傳遞給System.out.println
以查看字符串表示形式的外觀。
現在我們仍然需要在Java中實現perms
。方法perms
需要Vector
並返回Matrix
。我有一個非常黑客入侵,醜陋實現這我有點捨不得顯示,但對於一個完整的答案的原因,那就是:
static Matrix perms(Vector vector) {
int[] indices = new int[vector.length()];
for (int i = 0; i < vector.length(); i++)
indices[i] = i;
List<int[]> allPermuationIndices = new ArrayList<int[]>();
permutation(new int[0], indices, allPermuationIndices);
Vector[] perms = new Vector[allPermuationIndices.size()];
for (int i = 0; i < perms.length; i++) {
int[] permutationIndices = allPermuationIndices.get(i);
int[] vectorValue = new int[permutationIndices.length];
for (int j = 0; j < permutationIndices.length; j++)
vectorValue[j] = vector.apply(permutationIndices[j] + 1);
perms[i] = new Vector(vectorValue);
}
return new Matrix(perms);
}
private static void permutation(int[] prefix, int[] remaining, List<int[]> returnValue) {
if (remaining.length == 0)
returnValue.add(prefix);
else {
for (int i = 0; i < remaining.length; i++) {
int elem = remaining[i];
int[] newPrefix = Arrays.copyOf(prefix, prefix.length + 1);
newPrefix[prefix.length] = elem;
int[] newRemaining = new int[remaining.length - 1];
System.arraycopy(remaining, 0, newRemaining, 0, i);
System.arraycopy(remaining, i + 1, newRemaining, i + 1 - 1, remaining.length - (i + 1));
permutation(newPrefix, newRemaining, returnValue);
}
}
}
不要刻意去理解它在做什麼。嘗試自己編寫一個乾淨的實現(或谷歌解決方案)。現在
,如果我們要重新分配我們的x
,我們遇到麻煩:類型不匹配:我們宣佈x
是Vector
類型,但perm
返回一個Matrix
。有多種方法來解決這個問題:
我們可以宣佈Vector
是一個Matrix
,即簽名更改爲Vector extends Matrix
。此解決方案可能有意義,但請注意不要打破行爲子類型:如果類B
是類A
,則B
必須具有與A
相同的行爲,並且可以定義其他行爲。在同一張筆記上查找Liskov Substitution Principle。
我們可以聲明x
屬於雙方的超類型,即Vector
和Matrix
。目前,這是Object
,但我們也可以定義一個新的公共超類型。然而這個解決方案可能會失去我們的靜態類型安全例如,如果我們想用x
作爲參數傳遞給perm
,我們需要動態地將其轉換爲Vector
我們可以定義Matrix
類型的保存結果的第二個變量x2
。我在這種情況下建議這個解決方案。
接下來,我們分配i = 0;
和c=1;
,這在Java中轉化爲
int i = 0;
int c = 1;
現在,for循環:
for m = 1:1:5040
...
end
轉化爲
for (int m = 1; m <= 5040; i++) {
...
}
o NLY東西剩下的,除了把他們放在一起,是if語句:
if(x2(c,n) == (x2(c,(n+1))-1))
...
end
轉化爲
if (x2.apply(c, n) == (x2.apply(c, n+1) - 1)) {
...
}
其中apply
是我們在Matrix
定義的方法。請注意,在Java中,==
如果應用於非原始類型(即除int
,byte
,char
,double
,boolean
和float
以外的所有內容)將會給出奇怪的結果。通常,您使用在Object
上定義的方法equals
來測試等同性。
'我嘗試了幾次,但我失敗了__你試過了嗎? _什麼不起作用? _你預計會發生什麼? _什麼發生呢?請發佈您的代碼。 – BackSlash 2014-10-08 20:31:20
你有MATLAB Builder JA(http://www.mathworks.co。英國/產品/ javabuilder /)?除非你手動將MATLAB代碼翻譯成Java,否則這就是你需要的。 – am304 2014-10-08 20:33:32
不,我沒有MATLAB生成器JA,我不能將MATLAB代碼翻譯成Java,我是java新手。 – 2014-10-08 20:41:40