如何删除空的对象数组?
我定义了一个对象:
tempRes = new Object[100000][7];
例如,现在之后我将其填充到 100 行。 现在如何删除过去的每个对象(从 tempRes[100] 到 tempRes[10000])。
我需要这个用于 JTable。
i define an object:
tempRes = new Object[100000][7];
now after that i fill it up till 100 rows for example.
Now how to delete every object past that(from tempRes[100] too tempRes[10000]).
I need this for a JTable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
老实说,如果您不知道需要添加多少项,请不要使用数组。数组将为给定大小保留内存。使用
List
或Vector
,添加您的项目,然后将其转换为数组。或者,如果您的用法(例如 JTable)也可以与向量一起使用,则根本不转换它。此外,如果您将数据存储在内存中的其他位置并且列表很大,请实现您自己的
TableModel
子类(当您滚动到行时动态调用该子类,并且您必须将它们构建在需求然后)比首先将所有行渲染到数组中要高效得多。列表示例:
Honestly, don't use an array if you don't know how many items you will have to add. An array will reserve memory for the given size. Use a
List
or aVector
, add your items, and convert it to an array later. Or do not convert it at all if your usage (JTable for example) can also work with Vectors.In addition, in case you have the data stored elsewhere in memory and the list is huge, implementing your own
TableModel
subclass (which is called dynamically when you scroll to the rows and you will have to build them on demand then) is a lot more efficient than rendering all your rows into an array first.Example for a List:
在 java.util.Arrays 上,
它有一个 copyOf 方法,您可以使用该方法获取数组的“头”部分。
On
List
over arrayEffective Java 2nd Edition, Item 25: Prefer Lists to arrays
关于模型/视图分离
仅仅因为
JTable
使用数组进行查看,并不意味着您也应该这样对数据进行建模。了解模型-视图-控制器架构的工作原理。On
java.util.Arrays
It has a
copyOf
method that you can use to take the "head" portion of an array.On
List
over arraysEffective Java 2nd Edition, Item 25: Prefer lists to arrays
On model/view separation
Just because
JTable
uses arrays for viewing, doesn't mean that's how you should model your data too. Learn how model-view-controller architectures work.您需要分页表模型。 http://www.java2s.com/Code/Java/Swing- JFC/演示分页模型的快速应用程序.htm。这会根据您向上滚动到的位置动态地从数据库中获取数据。使用带有 LIMIT 语句的 SQL 查询将其链接到 SQL 数据库。代码将非常相似。
You need the paging table model. http://www.java2s.com/Code/Java/Swing-JFC/AquickapplicationthatdemonstratesthePagingModel.htm. This grabs data from the database dynamically depending on where you're scrolled up to. Link it to the SQL database using SQL queries with LIMIT statements. The code will be quite similar.
您无法调整 Java 数组的大小。唯一的方法是将元素复制到较小的数组并放弃原始数组。
如果您想动态调整大小,请使用 ArrayList。
You cannot resize Java arrays. The only way to do it is to copy the elements to a smaller array and abandon the original one.
Use ArrayList if you want resize dynamically.