0

I have an interesting dilemma;

I have a String[][] pulled from a MySQL database. For simplicity, I have printed the contents using Arrays.deepToString(<arrayname>):

[[1, gimley, <sanitised>, <sanitised>, <sanitised>, <sanitised>, 1994-10-11, <sanitised>, 2015-02-20 08:55:36.0], 
[2, Eris, Exotic, <sanitised>, <sanitised> <sanitised> <sanitised>, <sanitised>, 1996-02-15, <sanitised>, 2015-02-24 13:02:00.0], 
[3, Macca, Maccan, <sanitised>, <sanitised>, <sanitised>, 1996-11-11, University, Probably on Destiny, 2015-02-24 13:03:17.0], 
[4, <sanitised>, bacon, standard, <sanitised>, <sanitised>, 1996-03-12, <sanitised> or <sanitised>; depends, 2015-02-26 15:44:28.0]]

My question is: How can I remove one of the rows of the array-of-arrays, and replace the existing database with the contents of a new array-of-arrays? I understand that this is bad practice as I can just update the table directly, but this exercise requires me to do this method.

6
  • 2
    So what have you tried? Commented Feb 27, 2015 at 11:08
  • 1
    How did you populate this array in the first place. Or this is existing code? Commented Feb 27, 2015 at 11:09
  • @NeplatnyUdajI have tried converting this array to a List, edit the list then re-compile the list as a new String[][], but I cannot wrap my head around it. I basically want to remove a specific row by name, when the corresponding name is typed Commented Feb 27, 2015 at 11:17
  • @bot this is populated via a method which fills a blank String[][] with contents from a resultset Commented Feb 27, 2015 at 11:18
  • @MichaelWiggins Why do you want read the entire row before you update it? If you have the freedom to connect to the db, why not write a method that allows you to update a feild in the db for a row? Basically, why do you have to use the method that gives you a 2D array. Why not write your own update method? Commented Feb 27, 2015 at 11:21

1 Answer 1

1

do you mean something like this?

public class TestClass {

public static final String[][] removeRow( String[][] source, String rowName ) {
    boolean rowFound = false;
    for ( int i = 0; i < source.length; i++ ) {
        if ( source[ i ][ 0 ].equals( rowName ) ) {
            rowFound = true;
        }
    }
    if ( !rowFound ) {
        return source;
    }

    String[][] result = new String[ source.length - 1][ source[ 0 ].length ];
    int resultIndex = 0;
    for ( int i = 0; i < source.length; i++ ) {
        if ( !source[ i ][ 0 ].equals( rowName ) ) {
            System.arraycopy( source[ i ], 0, result[ resultIndex ], 0, source[ i ].length );
            resultIndex++;
        } 

    }

    return result;
}

@Test
public void test() {
    String[][] source = { 
            { "1", "one", "I", "eins" }, 
            { "2", "two", "II", "zwei" }, 
            { "3", "three", "III", "drei" }, 
            { "4", "four", "IV", "vier" } 
    };

    String[][] result = removeRow( source, "2" );
    assertEquals( "{[1, one, I, eins][3, three, III, drei][4, four, IV, vier]}", toString( result ) );

    result = removeRow( source, "rowNotIn" );
    assertEquals( "{[1, one, I, eins][2, two, II, zwei][3, three, III, drei][4, four, IV, vier]}", toString( result ) );
}

private String toString( String[][] source ) {
    StringBuffer sb = new StringBuffer();
    sb.append( "{" );
    for ( int i = 0; i < source.length; i++ ) {
        sb.append( Arrays.toString( source[ i ] ) );
    }
    sb.append( "}" );
    return sb.toString();
}

}

Sign up to request clarification or add additional context in comments.

2 Comments

Sort of, the array is only printed like that, the method used to obtain it is: String[][] dbTable = null; table = conn.createStatement(); String sql = "select * from Profiles"; ResultSet rs = table.executeQuery(sql); rs.last(); int rowNumb = rs.getRow(); ResultSetMetaData rsmd = rs.getMetaData(); int columnS = rsmd.getColumnCount(); rs.beforeFirst(); dbTable= new String[rowNumb][columnS]; int i=0; while(rs.next() && i<rowNumb && rowNumb<100) { for(int j=0;j<columnS;j++) { dbTable[i][j] = rs.getString(j+1); } i++; } return dbTable;
So there are no hard-coded elements for the array, now do you see my issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.