How to shift index in a table

Hey,

My program collects 5 pieces of information per object from user. Some of the input data is in String format, rest are Integers. I create an object from this data and put in into a table. My table contains max 10 pieces of information.

I have editing options for this data, which are erase one or all pieces, or modify a piece. I erase data by simply setting the index to null. Erasing one piece is incomplete atm, here's why:

Say I have 5 pieces in table, and I erase the one with index of 0. I can't print my information because the table starts with a null value:

int index = 0;

if (table[index] !=null){ System.out.println(data);}

..

My guess is that I can solve this problem by decreasing the index of pieces greater than the modified one by 1. I can imagine that I'll be using for loop here. This is where I run out of ideas: is it possible to just modify the index, or do I have to rewrite the data again?

Any help is appreciated,

br,

nomi

[1177 byte] By [nomikaia] at [2007-11-26 13:56:43]
# 1
You can use a loop.You can use System.arraycopy to move more than one element at a time.You can use a List (ArrayList or LinkedList) so that you can remove an element and have the list automatically resize (instead of setting an index to null).
doremifasollatidoa at 2007-7-8 1:36:22 > top of Java-index,Java Essentials,New To Java...
# 2

I think using for loop would be the easiest way (?). I'm filling the table as simply as:

items[index] = new table (value, quality..);

Using LinkedList seems a bit far fetched for me. But again, if I am to use for loop, I think I would have to rewrite the data to appropriate index?

nomikaia at 2007-7-8 1:36:22 > top of Java-index,Java Essentials,New To Java...
# 3

I suggest to do one alternate method which we will do in actual database also, to save all deleted datas

Add one more boolean field in ur table object named "isExists". When u delete a record change that value to false. When reading from table, take only records with isExists = true

But here also, when the number of records exceeds the limit, you need to delete the records with isExists = false

Cheers

astelaveestaa at 2007-7-8 1:36:22 > top of Java-index,Java Essentials,New To Java...
# 4

> I think using for loop would be the easiest way (?).

> I'm filling the table as simply as:

> items[index] = new table (value, quality..);

>

> Using LinkedList seems a bit far fetched for me. But

> again, if I am to use for loop, I think I would have

> to rewrite the data to appropriate index?

Why is a List far fetched? Type of List you pick depends on how you use the list (whether you want RandomAccess, etc.).

List items = new ArrayList(); // or LinkedList

items.remove(index); // Remove the item at index.

To use an array:

Table [] items = new Table[20]; // whatever size

items[index] = new Table(value, quality, ...);

// To remove:

System.arraycopy(items, index, items, index+1, items.length-(index+1));

items[items.length-1] = null;

doremifasollatidoa at 2007-7-8 1:36:22 > top of Java-index,Java Essentials,New To Java...
# 5
Thx for the assistance! Problem solved :)
nomikaia at 2007-7-8 1:36:22 > top of Java-index,Java Essentials,New To Java...