Forum Stats

  • 3,728,151 Users
  • 2,245,559 Discussions
  • 7,853,353 Comments

Discussions

How to remove specific element in two dimensional array ?

Arasu Dhanush
Arasu Dhanush Member Posts: 2
edited May 2016 in New To Java

Have some queries regarding on two dimensional array.

For example :

                         Object[][] objectArray = new Object[][]{{ 1.0D,2.0D,3.0D},{ 4.0D,5.0D,6.0D},{ 7.0D,8.0D,9.0D}};

                         Object[][] copyObjectArray = Arrays.stream(objectArray).map(x -> x.clone()).limit(2).toArray(Object[][]::new);

                         System.out.println(Arrays.deepToString(copyObjectArray));

     The above snippet returns the output like [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] because it's limit two rows.

     Is there any function two remove column or limit the column using Java 8 lamda function ?.

     Awaiting for your response.

Answers

  • Unknown
    edited May 2016
    Arasu Dhanush wrote:
    
    Have some queries regarding on two dimensional array.
    
    For example :
    
                             Object[][] objectArray = new Object[][]{{ 1.0D,2.0D,3.0D},{ 4.0D,5.0D,6.0D},{ 7.0D,8.0D,9.0D}};
                             Object[][] copyObjectArray = Arrays.stream(objectArray).map(x -> x.clone()).limit(2).toArray(Object[][]::new);
                             System.out.println(Arrays.deepToString(copyObjectArray));
    
         The above snippet returns the output like [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] because it's limit two rows.
    
         Is there any function two remove column or limit the column using Java 8 lamda function ?.
    
         Awaiting for your response.
    

    What does 'remove column' and 'limit the column' mean to you?

    An array in Java ALWAYS has the defined number of elements. If you define an array of 'x by y' then there will always be 'x' by 'y' elements in the array.

    So you need to SHOW US what the result should be and TELL US how that result should be obtained from the data.

  • Arasu Dhanush
    Arasu Dhanush Member Posts: 2
    edited May 2016

    Thanks for your reply..

    I want the result like [[1.0, 2.0], [4.0, 5.0]]..

    Hope you got my point.

  • 836548
    836548 Member Posts: 286
    edited May 2016

    for the above output you need to iterate through the multi dimension array and copy the values to new object which should be of size 2X2.

    The above code is working as it intended. the objectArray  object you defined is an object of multiple arrays, and when you do limit(2), it is always considering the outter array length which is 3 and copying only two to new array object.

This discussion has been closed.