-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]);
    }
}
4

4 回答 4

1

使用此函数深度复制二维数组。

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);
于 2013-05-12T10:24:40.670 回答
0

System.arrayCopy()是从现有数组创建新数组的有效方法。您可以使用自己的编码完成的操作也可以使用它来完成。只是探索

于 2013-05-12T10:51:57.873 回答
0

这取决于您对“某些信息”的定义。如果要将数组的一部分复制到新的部分,则可以使用System.arraycopy

例子

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

System.arraycopy(numbers,0,newNumbers,0,3);
于 2013-05-12T10:22:38.670 回答
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);               
}

另请参阅这个简短的演示

于 2013-05-12T10:37:34.293 回答