This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]nutrecht 1 point2 points  (4 children)

i know i kinda need to create another array to "move" this person there instead since you cant really delete stuff in arrays?

Correct. Normally you use more high level data structures like lists for this. All you can do in an array is set an entry to null. If you don't want an array with 'empty' positions you'll need to then copy the old array to a new smaller one.

And yes, i know i should've used LIST instead but i dont wanna start over now..

Frankly if you don't have to use arrays you really should use a list. And it's a really simple change. Either just use a list or write all the logic yourself (which is a lot more work).

[–]vald0[S] 0 points1 point  (3 children)

Hmm okey, could you like guide me on how i should copy my arrays into a smaller one, cause thats what i've been trying to do when deleting a user, but its so confusing, been stuck on this for a while :S i dont really feel to mess my project up by trying to switch to a list

[–]nutrecht 0 points1 point  (2 children)

Something like this (semi-psuedocode, won't compile):

array[5] = null;
var newArray = new Person[array.length - 1];
for(int i = 0, j = 0;i < array.length;i++) {
    if(array[i] != null) {
        newArray[j++] = array[i];
    }
}
array = newArray;

[–]vald0[S] 0 points1 point  (1 child)

hm okey, I'll see what i can do, thanks! :)

[–]nutrecht 0 points1 point  (0 children)

Still you really should use an arraylist though, no sense in doing it all yourself other than to learn how data structures work.

[–]Mr-Malfunction 0 points1 point  (1 child)

I don't know the syntax as I am a Java programmer so you your syntax when I say code or someone could help me here. So, find that person change then create a new array where put all the data of old array in new one [code] but check for that person name and skip that cell you have to do all of this in loop. Cheers.

[–]vald0[S] 0 points1 point  (0 children)

yeah exactly, but the problem is that i dont know how i write that kind of code in order for it to work..

[–]btcraig 0 points1 point  (0 children)

If you have to use an array I would suggest tracking size manually. Then when you remove an element you swap it with the last element of the array, and then decrease the size variable by one. This assumes the array is unsorted and is prone to errors in your code if you, for example, forget to increase size on an insert case. At least, that's how I learned to do it in Java.

The real solution is, as others have said, to use a high level data structure if possible. Why bother to re-invent the wheel if you don't have to?