How do I insert a row into a 2D array in java?

GiGi

The user has to give an index of a row where they want to insert it, like this:

original:
2.546   3.664  2.455
1.489   4.458  3.333

insert an index row: 1
[4.222, 2.888, 7.111]

row inserted:
2.546   3.664  2.455
4.222   2.888  7.111
1.489   4.458  3.333

here is the code:

public double[] getTheDataForRow( int len )
{
    double [] num = new double [len];
    return num;

}

public double[][] insertRow( double[][] m, int r, double[] data){
    m = new double [data.length][3];
    for(int row = 0; row<m.length; row++){
    for(int col = 0; col<m[row].length;col++)
    if(m[row][col] == r){
    m[row][col] = data;
    }
    }
    return m;
}

public void result(double[][] s){
    for(int row=0; row<s.length; row++){
        for(int col=0; col<s[0].length; c++)
            out.printf( "%5.2f", s[row][col] );
        out.println();
    }
    out.println();
}

I keep having an error and I honestly do not know how to fix it. I would appreciate some help.

Elliott Frisch

You are clobbering your input array m on the first line of your insertRow. Instead, create a new array of one more element in size. Then copy everything before the insertion point from the input. Then insert the new row. Then copy everything after that row from the input (shifted back one against the current index). And, I would make the method static. Like,

public static double[][] insertRow(double[][] m, int r, double[] data) {
    double[][] out = new double[m.length + 1][];
    for (int i = 0; i < r; i++) {
        out[i] = m[i];
    }
    out[r] = data;
    for (int i = r + 1; i < out.length; i++) {
        out[i] = m[i - 1];
    }
    return out;
}

Then to test it,

public static void main(String[] args) {
    double[][] arr = { { 2.546, 3.664, 2.455 }, { 1.489, 4.458, 3.333 } };
    System.out.println(Arrays.deepToString(arr));
    arr = insertRow(arr, 1, new double[] { 4.222, 2.888, 7.111 });
    System.out.println(Arrays.deepToString(arr));
}

And I get (as expected)

[[2.546, 3.664, 2.455], [1.489, 4.458, 3.333]]
[[2.546, 3.664, 2.455], [4.222, 2.888, 7.111], [1.489, 4.458, 3.333]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In Java, how do I take the values of a 2D array and store them in another 2D array with different column and row size?

How can I copy a 2D array and add a new row to store the sum of the columns in Java?

How do you check a row in a 2D char array for a specific element and then count how many of that element are in the row? (Java)

How do I do a deep copy of a 2d array in Java?

How do I convert a String to an 2D Array of ints?

How to get largest and smallest element in 2d array in java column wise I have got output for row wise

How do I create a 2d array made of 2d int arrays in java?

How to insert a string into a 2D string array in Java?

How do I store, sort and print a 2D "array" of strings and doubles in Java?

How do I get the function to print out a 2D array into two files in java?

How do I dynamically allocate a 2d array of chars?

Java how to check if a row is empty in a 2d array.

How do i convert a Set into 2D array in java?

How do I find path using 2d array maze in java

How to insert a value in 2D array in java

How do I print this 2 D array next to the appropriate row?

How do I find the middle of a 2d array?

How do I replace a specific string in a 2d array?

How do I sort a 2D array?

How do I use user input to select data from a 2D array in Java?

How do I insert a 1D Torch tensor into an existing 2D Torch tensor into a specific row?

How do I create a table using 2D array in Java?

How do I use BigQuery DML to insert a row with an array within a struct within an array?

How do I add an array into an arraylist (so that it becomes a 2D arraylist) in java?

How do I enter data from user input into a 2D array in Java?

How do i initialize a 2D Array with m(rows), n(cols) in java?

How do I fill my 2D array with a string ? Java

How to get the highest row and column of a 2D array in Java

How do I use a loop to multiply a row of a 2D array to the column of a second 2D array, and etc in Java?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive