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.

5
  • Possibly, I had looked at that question but the answers didn't help Commented Mar 6, 2015 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. Commented Mar 6, 2015 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) Commented Mar 6, 2015 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). Commented Mar 6, 2015 at 13:00
  • The modified array will be used to overwrite the table (once i figure out how to do it) Commented Mar 6, 2015 at 13:02

1 Answer 1

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, !]]

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

8 Comments

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?
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 :-)
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).
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.
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<dbTable.length;i++) { for(j=0;j<dbTable[i].length;j++) { cloneOf[i][j] = new String(dbTable[i][j]); } } System.out.println(Arrays.deepToString(cloneOf)); return cloneOf; }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.