-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]);
    }
}
johnchen902
  • 9,531
  • 1
  • 27
  • 69
Muna Lisa
  • 11
  • 2
  • I suggest you avoid using `float` not only does it make examples like this more complicated, but it has much less precision (it is one billion times less accurate) – Peter Lawrey May 12 '13 at 10:52

4 Answers4

1

Use this function to deep-copy a 2d array.

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;
}

And in your code:

float[][] pesNeixement = deepCopy(pesIAlcada, 0, 2);
flavian
  • 28,161
  • 11
  • 65
  • 105
  • I want to make a new Array called pesNeixement and take just these two rows for the new array(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}, – Muna Lisa May 12 '13 at 10:28
  • You might want to add an extra `offset` argument, in case someone wants to copy *x* rows starting at row *y*. – gkalpak May 12 '13 at 10:46
  • @alex23 the code is identical to this : http://stackoverflow.com/a/1564856/951448 And this answer is 3 years older than the question you're referring to ... – Mr.Me May 12 '13 at 10:50
  • @ alex23 Sorry I'm new here, how can I? – Muna Lisa May 12 '13 at 12:19
0

It depends by your definition of "some information". If you want to copy a section of the array to a new one, then you can use System.arraycopy.

Example

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

System.arraycopy(numbers,0,newNumbers,0,3);
christopher
  • 26,815
  • 5
  • 55
  • 89
  • I want to make a new Array called pesNeixement and take just these two rows for the new array(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}, – Muna Lisa May 12 '13 at 10:31
  • So apply some reasoning, look at the answers you've been given and work out how you might get the top two lines. – christopher May 12 '13 at 10:32
  • Yes, i did here 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]); } } The problem is that how can i change the name from pesIAlcada to pesNeixement ?? – Muna Lisa May 12 '13 at 10:36
0

If you want to copy some rows from pesIAlcada in an new array (pesNeixement) you can use something like this:

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

Also see this short demo.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
0

System.arrayCopy() is the efficient way of creating new array from an existing array. What you can do with your own coding can be done using this also. Just explore

K Adithyan
  • 386
  • 4
  • 12