如何在不使用ArrayList的情况下编辑数组? [英] How do I edit an array without using ArrayList?
问题描述
我已经设法将表的内容读入一个数组,但我现在有了删除行和更新单个元素的美好任务。
我明白,在Java中,Arrays被设置为一个特定的维度(这个是从while-nested-for循环中的ResultSet填充的),这使得删除变得困难。
填充数组的代码如下:
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:
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;
}
我需要应用程序处理SQL风格,所以我的应用程序可以说:
如果< insert method =here =>包含< string input =>删除匹配或说不存在的行。有一个问题(到目前为止):这可能不使用ArrayLists吗?
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?
推荐答案
这取决于你的编辑是什么意思。您可以通过索引编辑数组元素立即访问它,但是您需要一些集合来添加/插入/删除元素。
请阅读这篇好文章: http://www.cforcoding.com/2009/12/mutability-arrays-and -cost-of-temporary.html [ ^ ]。
基本上,不要将数组复制到集合中并返回。仅在最后创建一个数组,当您已经知道不需要修改它时。使用不可变集合是个好主意。原则上,你可以完全摆脱阵列,在许多情况下这是一个可行的决定。
-SA
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
这篇关于如何在不使用ArrayList的情况下编辑数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!