0

I have a 2D String array populated with the following method in my Main class:

public static String[][] dbTable() throws SQLException
{
    String[][] dbTable = null;
    table = conn.createStatement();
    String sql = "select * from Java_Test_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;

"dbTable" is passed between several classes, and I was wondering how I can create a copy of this for editing. I want to do this because i need to "re-load" the contents of the array back to their original state. I have tried to call main.dbTable() but that returns exactly what has already been edited, not what is contained within the database.

  • Possibly, I had looked at that question but the answers didn't help – Michael Wiggins Mar 06 '15 at 12:49
  • It's impossible that calling `main.dbTable()` would return the edited array. Your method is also leaking resources like a wounded soldier is leaking blood. – Kayaman Mar 06 '15 at 12:51
  • @Kayaman nice analogy there... graphic :3 In terms of leaking resources, that isn't an issue as all is cleaned elsewhere in the application. Thanks for the comment, I'll re-work the idea behind it but essentially, I have the main class (ConToDatabase) with a public static[][] method to populate the array. For some reason, calling `ConToDatabase.dbTable()` returns the same. I tested this by doing `Arrays.deepToString(dbTable)` – Michael Wiggins Mar 06 '15 at 12:55
  • Well, I would still suggest you create and close `Connection`, `Statement` and `ResultSet` in your method. But how do you explain that by editing an array you're modifying the contents of the database? Because that's what seems to be happening by your telling, yet the code shows that it should be impossible (unless you're saving the modified array back to the database elsewhere). – Kayaman Mar 06 '15 at 13:00
  • The modified array will be used to overwrite the table (once i figure out how to do it) – Michael Wiggins Mar 06 '15 at 13:02

1 Answers1

1

You can do a clone, but it does a shallow clone. Just need to do a trick to fix that.

First, let's make a clone of the original:

String[][] cloneArray = dbTable.clone();

Then let's populate the cloned array:

for (int i = 0; i < cloneArray.length; i++) {
    cloneArray[i] = cloneArray[i].clone();
}

Code used for testing

String[][] mainArray = new String[1][];
String[] data = new String[3];

data[0] = "Hello";
data[1] = "World";
data[2] = "!";

mainArray[0] = data;

String[][] testArray = mainArray.clone();

for (int i = 0; i < testArray.length; i++) {
    testArray[i] = testArray[i].clone();
}

System.out.println("Main Array: " + Arrays.deepToString(mainArray));
System.out.println("test Array: " + Arrays.deepToString(testArray));

data[2] = "!!!!";

mainArray[0] = data;

System.out.println();
System.out.println("Main Array2: " + Arrays.deepToString(mainArray));
System.out.println("test Array2: " + Arrays.deepToString(testArray));

Output:

Main Array: [[Hello, World, !]]
test Array: [[Hello, World, !]]

Main Array2: [[Hello, World, !!!!]]
test Array2: [[Hello, World, !]]

Ascalonian
  • 14,409
  • 18
  • 71
  • 103
  • Silly question, but would I place this in my main and pass this to the other classes rather than dbTable, and then call dbTable to "reset" cloneArray? – Michael Wiggins Mar 06 '15 at 13:00
  • There are many ways you can handle this, depending on your design. You can create a separate method to return the clone, you can create a global variable called `originalDataset` and in `dbTable()` just set that value to be used anywhere... it's really up to you and how the application is designed :-) – Ascalonian Mar 06 '15 at 13:41
  • I created a new method to copy the array and as soon as I hit this, I got a nullpointerexception. I'm getting beyond frustrated with it as I need this to work to keep my job (i'd hate to lose it because they wouldn't let me use Arraylists). – Michael Wiggins Mar 06 '15 at 13:50
  • And what object was the NPE thrown on? I tested the above code and it does work, so it might be how you set up your method. – Ascalonian Mar 06 '15 at 14:20
  • Here it is, sorry for the delay: `public static String[][] cloneOf(String[][] dbTable) throws SQLException { int i =0; int j=0; String[][] cloneOf = new String[i][j]; for(i=0;i – Michael Wiggins Mar 06 '15 at 15:00
  • You set the size of `cloneOf` to be zero, so you can't put anything into it. i am adding my example code for you to reference. Hope that will help :-) – Ascalonian Mar 06 '15 at 15:19
  • so working of the example provided, if I use the same layout (roughly) as my question (i.e use the database dimmensions as indexes) the code should work? I'll give it a try thanks – Michael Wiggins Mar 06 '15 at 15:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72420/discussion-between-ascalonian-and-michael-wiggins). – Ascalonian Mar 06 '15 at 15:33