2013-05-12 93 views
-2
float[][] pesIAlcada = { 
     { 2.4f, 3.1f, 3.07f, 3.7f, 2.7f, 2.9f, 3.2f, 3f, 3.6f, 3.1f }, 
     { 19f, 18.7f, 22f, 24f, 17f, 18.5f, 21f, 20f, 18.7f, 22f, 18f }, 
     { 47f, 48f, 49f, 50f, 51f, 52f, 51.5f, 50.5f, 49.5f, 49.1f, 50f }, 
     { 101f, 104f, 106f, 107f, 107.5f, 108f, 109f, 110f, 112f, 103f } }; 
/* 
* I already created an array. And I want to make a new one but some 
* infomation from the old array. How can I do, plz? 
*/ 
float[][] pesNeixement = new float[ROWS][COLS]; 
for (int i = 0; i < 2; i++) { 
    for (int j = 0; j < pesIAlcada[i].length; j++) { 
     System.out.print(pesIAlcada[i][j]); 
    } 
} 
+0

我建議你避免使用'浮動'不僅使這樣的例子更復雜,但它有更少的精度(這是十億倍的準確性) – 2013-05-12 10:52:34

回答

0

它取決於您對「某些信息」的定義。如果要將數組的一部分複製到新數組中,則可以使用System.arraycopy

int[] numbers = {4,5,6,7,8}; 
int[] newNumbers = new int[10]; 

System.arraycopy(numbers,0,newNumbers,0,3); 
+0

我想做一個新的數組稱爲pesNeixement和對於新陣列(pesNeixement)只需要這兩行 {{2.4f,3.1f,3.07f,3.7f,2.7f,2.9f,3.2f,3f,3.6f,3.1f},{ 19f,18.7f,22f,24f,17f,18.5f,21f,20f,18.7f,22f,18f}, – 2013-05-12 10:31:26

+0

因此,運用一些推理,看看你給出的答案,並計算出如何得到最高兩條線。 – christopher 2013-05-12 10:32:51

+0

是的,我在這裏做 float [] [] [] [] pesNeixement = new float [ROWS] [COLS];對於(int j = 0; j 2013-05-12 10:36:08

1

使用此功能深複製的2D陣列。

public static float[][] deepCopy(float[][] original, Integer offset, Integer numberOfRows) { 
    if (original == null) { 
     return null; 
    } 
    if (offset == null) { 
     offset = 0; 
    }; 

    if (numberOfRows == null) { 
     numberOfRows = original.length; 
    }; 

    final float[][] result = new float[numberOfRows - offset][]; 
    for (int i = offset; i < numberOfRows; i++) { 
     result[i] = Arrays.copyOf(original[i], original[i].length); 
    } 
    return result; 
} 

並在代碼:

float[][] pesNeixement = deepCopy(pesIAlcada, 0, 2); 
+0

我想創建一個新的數組,稱爲pesNeixement 並且只爲這個新數組(pesNeixement)使用這兩行.. {{2.4f,3.1f,3.07f,3.7f,2.7f,2.9f,3.2 f,3f,3.6f,3.1f}, {19f,18.7f,22f,24f,17f,18.5f,21f,20f,18.7f,22f,18f}, – 2013-05-12 10:28:57

+0

不錯的,upvoted .. :) – ridoy 2013-05-12 10:40:20

+0

You可能需要添加一個額外的'offset'參數,以防某人想要複製* x *行從行* y *開始。 – gkalpak 2013-05-12 10:46:48

0

如果你希望將一些行從pesIAlcada在一個新的數組(pesNeixement),你可以使用這樣的複製:

int fromRow = 0;  // Start copying at row0 (1st row) 
int toRow = 2;  // Copy until row2 (3rd row) <- not included 
        // This will copy rows 0 and 1 (first two rows) 
float[][] pesNeixement = new float[toRow - fromRow][]; 

for (int i = fromRow; i < toRow; i++) { 
    pesNeixement[i] = new float[pesIAlcada[i].length]; 
    System.arraycopy(pesIAlcada[i], 0, pesNeixement[i], 0, pesIAlcada[i].length);    
} 

而且看到這個short demo

0

System.arrayCopy()是從現有數組中創建新數組的有效方法。你也可以用你自己的編碼來完成。只是探索

相關問題