如何删除空的对象数组?

发布于 2024-09-02 18:02:35 字数 176 浏览 5 评论 0原文

我定义了一个对象:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我还不会笑 2024-09-09 18:02:35

老实说,如果您不知道需要添加多少项,请不要使用数组。数组将为给定大小保留内存。使用 ListVector,添加您的项目,然后将其转换为数组。或者,如果您的用法(例如 JTable)也可以与向量一起使用,则根本不转换它。

此外,如果您将数据存储在内存中的其他位置并且列表很大,请实现您自己的 TableModel 子类(当您滚动到行时动态调用该子类,并且您必须将它们构建在需求然后)比首先将所有行渲染到数组中要高效得多。

列表示例:

    List<Object[]> tempRows = new ArrayList<Object[]>();
    for (int i = 0; i < 100; i++) {
        Object[] row = new Object[] {"This", "Is", "Just", "Some", "Example", "Data", "Here"};
        tempRows.add(row);
    }
    Object[][] tempRes = (Object[][]) tempRows.toArray(new Object[tempRows.size()][]);

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 a Vector, 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:

    List<Object[]> tempRows = new ArrayList<Object[]>();
    for (int i = 0; i < 100; i++) {
        Object[] row = new Object[] {"This", "Is", "Just", "Some", "Example", "Data", "Here"};
        tempRows.add(row);
    }
    Object[][] tempRes = (Object[][]) tempRows.toArray(new Object[tempRows.size()][]);
稀香 2024-09-09 18:02:35

在 java.util.Arrays 上,

它有一个 copyOf 方法,您可以使用该方法获取数组的“头”部分。

String[] names = { "Alice", "Bob", "Carol", "Dean" };
names = Arrays.copyOf(names, 2);
System.out.println(Arrays.toString(names));
// prints "[Alice, Bob]"

On List over array

Effective 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.

String[] names = { "Alice", "Bob", "Carol", "Dean" };
names = Arrays.copyOf(names, 2);
System.out.println(Arrays.toString(names));
// prints "[Alice, Bob]"

On List over arrays

Effective 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.

花间憩 2024-09-09 18:02:35

您需要分页表模型。 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.

绮筵 2024-09-09 18:02:35

您无法调整 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文