2013-03-15 60 views
0

這Java表示是在Matlab定義的大小的功能出現在文檔:什麼是大小爲在Matlab

m = size(X,dim) returns the size of the dimension of X specified by scalar dim. 

1)鑑於X是Java中的單個陣列,我將如何複製這一使用Java的方法。

回答

1

Matlab只是將矩陣和n維數據線性地存儲在一個數組中。每個維度的大小與數組一起保存,因此Matlab知道當您不是線性索引時返回哪個元素(例如,對於5x5矩陣,A(3,5),Matlab知道它應該返回元素A(23),即3+(5-1)*5)。

所以在Java中,如果一個數組的大小爲N1xN2x...xNN,和你想找的元素:(X1,X2,...,XN),你應該找到與元素的位置是:X1+(X2-1)*N1+(X3-1)*N1*N2+ ... +(XN-1)*NN-1*..*N1數組中......

+0

我明白了。感謝那! – Poensvah 2013-03-15 17:31:24

0

如果你只想要數組的大小(任何維數),你有一個屬性Array.length

JLS

公共最終字段長度,它包含部件陣列的 的數量。長度可能是正數或零。

例如:

int[] arr = new int[10]; 
int length = arr.length; //would obtain 10 
0

在Java中的陣列僅具有一個尺寸:元件的數量。您只需使用Array.length即可訪問此內容。對於更復雜的容器(例如ArrayList),您可以經常使用成員函數size()

int[] array = new int[5]; 
int arrayLength = array.length; //arrayLength == 5 

ArrayList<int> arrayList = new ArrayList(); 
arrayList.add(1); 
arrayList.add(2); 
int arrayListLength = arrayLis.size(); //arrayListLength == 2