Click here to Skip to main content
15,850,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have managed to read the contents of the table into an array, but I now have the oh-so joyous task of deleting rows and updating individual elements.

I understand that, in Java, Arrays are set to a specific dimension (this one is populated from a ResultSet in a while-nested-for loop), making deletion difficult.

The code I have for populating the array is as follows:

Java
public static String[][] dbTable() throws SQLException
	{
		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;
	}


I need the application to work on a SQL-style, so my application can say:
If <insert method="" here=""> contains <string input=""> remove the row that matches or says doesn't exist. There's one catch (so far): Is this possible without the use of ArrayLists?
Posted
Comments
Sergey Alexandrovich Kryukov 27-Feb-15 11:17am    
Sorry for wrong answer which I just removed. And yes, with Java you can use ArrayList. What would be the reason for not using it?
—SA

1 solution

It depends on what do you mean by "edit". You can edit array element immediately accessing it by index, but you will need some collection to add/insert/remove element.

Please read this nice article: http://www.cforcoding.com/2009/12/mutability-arrays-and-cost-of-temporary.html[^].

Basically, don't copy an array to collection and back. Create an array only at the very end, when you already know that you won't need to modify it. Using immutable collection is a good idea. In principle, you can get rid of arrays totally, which can be a viable decision in many cases.

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 27-Feb-15 11:34am    
+5, but Sergey sir, you should also mention in the first paragraph of your answer, that arrays once defined do not change their size. That is why collections are used.
Sergey Alexandrovich Kryukov 27-Feb-15 11:36am    
Thank you for voting and your correction, Afzaal.
I started to answer about .NET, by mistake, and when the inquirer pointed it out, I quickly rewrote it...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900