how to insert array item in proper index of sorted array

Hi

i have a class Customer .

class Customerimplements Comparable<Customer>{

int accountID;

String name;

String address;

publicint compareTo(Customer c){

return name.compareToIgnoreCase(c.name);

}

}

in my main(),

publicstaticvoid main(String[] args){

// TODO code application logic here

Customer BankAccount[]=new Customer[30];

for(int i =0;i<BankAccount.length;i++){

BankAccount[i]=new Customer();}

when i want to insert a new record,some records must be right-shifted to make space for the new record.Suppose i want to insert a record with name michael and existing array is

name:low

address:singapore

account id:01

name:shu

address:canada

account id:02

name:juan

address:china

account id:03

so records starting with name shu and onwards to the right can be shifted by one slot to the right so that empty slot is available for the new record.

Does anyone know how to do that?

thanks...>

[1726 byte] By [jutyia] at [2007-10-3 3:32:51]
# 1
Maybe you should use a List, arrays have fixed sizes...
CeciNEstPasUnProgrammeura at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...
# 2
look at collection classes.they have methods already defined for inserting elements
r035198xa at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...
# 3
but my one is not using collections.is a public class Customer and i declared user-defined type array.can u show me a sample or example?thanks
jutyia at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...
# 4
Well why don't you use a list instead? Look at the Arrays.asList() function :D
toruma at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...
# 5
it would be better to use linkedlist as records right shifted can be largeanyhow its still possible with arrayscompareto func returns -ve 0 and +ve valuesjust go through string functnality
vipuluckya at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...
# 6
> it would be better to use linkedlist as records right> shifted can be large> > nyhow its still possible with arraysAnd what do you do with the last element? It is possible, but not without creating a new array.
CeciNEstPasUnProgrammeura at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...
# 7
can u show me an exmple?
jutyia at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...
# 8
If you want to keep everything as arrays, the easiest thing to do is to create a new array big enough to hold the old data plus the new data. Then you simply copy the values from the old one up to a certain point, copy the new data, then copy the remaining old data.
Asbestosa at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...
# 9
eg System.arraycopy
mchan0a at 2007-7-14 21:27:09 > top of Java-index,Java Essentials,Java Programming...