arrays

Hi All

i have an array of 'client' obejects (a client has methods for the name, salary and taxation). in the UI class that contains the main method is where the client array is declared and initialised but we cant instantiate it with x amount of elements as an element can only be instantiated as needed. if i create a method that creates an array with a size of client.length +1 and copy all existing elements into it then return the new array, how do i make this array become the client array and the old array with 1 element less become ignored?

[564 byte] By [Aussie_Nerda] at [2007-11-27 4:44:36]
# 1

> .... the client array is declared and

> initialised but we cant instantiate it with x amount

> of elements as an element can only be instantiated

> as needed. .....

Your array size is obviously not fixed. This is where arrays become kludgy and ArrayLists really shine. Why not use an ArrayList?

petes1234a at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 2
its an assignment for uni and we havent been taught about array lists as of yet so i dont think we will be allowed to use things we havent learned
Aussie_Nerda at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 3

As pete mentioned, using an ArrayList is preferred. However to answer your question, if you can't use an ArrayList, write a method that increases the array and then returns it.

clientList = upsize(clientList);

....

public Client[] upsize(Client[] list) {

// do magic

return newList;

}

floundera at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 4

> As pete mentioned, using an ArrayList is preferred.

> However to answer your question, if you can't use an

> ArrayList, write a method that increases the array

> and then returns it.

> > clientList = upsize(clientList);

> ....

> public Client[] upsize(Client[] list) {

>// do magic

> return newList;

> }

>

For part of magic see the System.arraycopy method

cotton.ma at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 5
Oh yeah. forgot about that. <blushes uncontrollably>
floundera at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 6
thanks flounder, your method that you wrote is a little unclear to me though, could you explain just a little more for me? (i have only JUST started on arrays)please forgive my inability to comprehend fully :)Marc
Aussie_Nerda at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 7
> thanks flounder, your method that you wrote is a> little unclear to me though, could you explain just a> little more for me? (i have only JUST started on> arrays)Could you explain which part is unclear. The method? or the way that he used it?
cotton.ma at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 8
you should create a client array ,then using System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) .
Mr.Greedya at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 9

class Upsize {

public static void main(String[] args) {

int value = 10;

System.out.println(value);

value = doubleUp(value);

System.out.println(value);

}

public static int doubleUp(int number) {

return 2 * number;

}

}

floundera at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 10

what im struggling to comprehend is, wel basicly alot of the method: all i can figure out so far is i need to copy the elements of my client array to a new array that has 1 extra element then the new array becomes the client array is this correct? how do i set this new array to be my original client array? AGGHH i thought i was comprehending arrays yesterday and its confused me again!

Marc

Aussie_Nerda at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...
# 11
Here:clientList = upsize(clientList);Whatever is returned from the method is assigned to the variable which held the original array. I called it clientList but you may have called it something else.
floundera at 2007-7-12 9:56:38 > top of Java-index,Java Essentials,New To Java...