2013-05-12 69 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()是从现有数组中创建新数组的有效方法。你也可以用你自己的编码来完成。只是探索