views:

67

answers:

4

Hello guys, 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.

+3  A: 

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()][]);
mihi
A: 

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.

ZZ Coder
+1  A: 

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.

polygenelubricants
+1  A: 

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.

Chris Dennett