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?
> .... 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?
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
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;
}
> 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
Oh yeah. forgot about that. <blushes uncontrollably>
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
> 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?
you should create a client array ,then using System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) .
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;
}
}
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
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.