a few array related questions, probably quite trivial
hi
i have the following for loop in a method. the loop cycles through the elements of an array of client objects (c), stopping when it reaches the end of the array (noClients) and if it finds the name that the user is searching for it stores this integer in poss and then uses poss to display the info of the client the person searched for. however the loop only ever returns the first client of the array and not the correct one, could someone let me kno if they can see why?
for (int j=0;j<noClients;j++)
{
if (c[j].getName() == name)
{
poss = j;
}
}
another question is if u have an array and the user wishes to delete one element, i kno u can have a second array with 1 less element than the original and transfer the other elements over that are to be kept. but the only way i know how to do this is with a loop and the loop i am thinking of willl copy each element without excluding the one the client wishes to delete. can someone provide me with any advice on this?
lastly. i hav a method to resize an array, but when it does so it makes the first and second element null when these elements previously had data in them, here is the code, hoping someone can point out what is the cause of it making the first 2 elements nulls in the new enlarged array
publicstaticvoid resizeArray()
{
Client[]c2;
c2 =new Client[c.length+1];
int i=0;
for (i=0;i<c2.length;i++)
{
c2[i]=new Client();
}
if (noClients>=3)
{
for (i=0;i<(c.length-1);i++);
{
c2[i]=c[i];
}
}
c=c2;
}
cheers
> another question is if u have an array and the user wishes to delete one
> element, i kno u can have a second array with 1 less element than the original
> and transfer the other elements over that are to be kept. but the only way i
> know how to do this is with a loop and the loop i am thinking of willl copy each
> element without excluding the one the client wishes to delete. can someone
> provide me with any advice on this?
Why use an array. if you use an ArrayList (or some other implementation of List) you can just delete the offending element with list.delete(index); Dunski.
These days we use List's in java much more than arrays, for exactly the kind of reasons that you're finding.
If you're stuck with arrays coz that's what your numbnutts lecturer is teaching this week then check out the Arrays class (1.5 onwards I think)... and it's static Array.copy method.
Cheers. Keith.
Message was edited by: corlettk
> lastly. i hav a method to resize an array, but when it does so it makes the first
> and second element null when these elements previously had data in them,
> here is the code, hoping someone can point out what is the cause of it
> making the first 2 elements nulls in the new enlarged array
My previous prothletising of Lists over Arrays applies here also.
<unabashed-google-plug>
Have you heard of google? Google is your friend. Google, google, google. Google uses many on open source technologies, and it is by far and away the best search engine for open source stuff, and is an invaluable tool for wannabe hackers such as me & thee.
</unabashed-google-plug>
In this case google "java resize araay" and ta da... top of the list is:
http://www.source-code.biz/snippets/java/3.htm
which looks like a money shot to me.
Cheers again. Keith.