Removing more than 1element in Arrays

Hello,

Is it possible to extend the code below to remove more than 1 element the same time in an Array? WithSystem.arraycopy, I was able to remove an element.

public String[] remove(int pos, String[] src)

{

String[] des =new String[src.length-1];

System.arraycopy( src, 0, des, 0, pos );

System.arraycopy( src, pos+1, des, pos, des.length-pos);

return des;

}

I know I can convert the array to a List, delete the elements and re-convert the List back to Array but I just want to know if it is possible to extend the above code.

Thanks,

Jona_T

[808 byte] By [Jona_Ta] at [2007-11-27 11:48:45]
# 1

of course that's possible!

You have to call arraycopy once, plus once per removed element.

So now you call arraycopy twice to remove one element.

To remove two elements, call arraycopy three times,

to remove three elements, call arraycopy four times, etcetra.

also think about how to pass the positions:public String[] remove(int[] positionsToDelete, String[] src)

//or

public String[] remove(boolean[] delete, String[] src)

tom_jansena at 2007-7-29 18:21:07 > top of Java-index,Java Essentials,New To Java...
# 2

Hello Tom_Jansen,

I did not really get your statement "To remove two elements, call arraycopy three times,

to remove three elements, call arraycopy four times".

Jona_Ta at 2007-7-29 18:21:07 > top of Java-index,Java Essentials,New To Java...
# 3

> Hello Tom_Jansen,

> I did not really get your statement "To remove two

> elements, call arraycopy three times,

> to remove three elements, call arraycopy four times".

Think on it, and it will come to you.Play with code.It would be better if you figure out the solution yourself rather than us give it to you. Don't give up.

petes1234a at 2007-7-29 18:21:07 > top of Java-index,Java Essentials,New To Java...
# 4

Hello Petes1234,

Thanks for your encouraging advice. My problem actually is how to achieve a dynamic solution to the "System.arraycopy approach. That is the reason why I initially was unable to understand Tom_Jansen's useful suggestion. Using the static approach like below, will not solve the problem because I do not know how many elements to delete in runtime. public static String[] remove(int []pos, String[] src){

String[] des = new String[src.length - pos.length];

System.arraycopy(src, src.length-src.length, des, des.length-des.length, 1);

System.arraycopy(src, src.length-(src.length-1), des, des.length-(des.length-1), 1);

System.arraycopy(src, src.length-pos.length, des, pos.length, des.length-pos.length);

return des;

}

Any advice?

Thanks,

Jona_T

Jona_Ta at 2007-7-29 18:21:07 > top of Java-index,Java Essentials,New To Java...
# 5

> Using the static approach like

> below, will not solve the problem because I do not

> know how many elements to delete in runtime.

Correction: You will know at runtime. You do not know at compile time. So the question becomes, how will you know at runtime? What type of info will this method receive? That will help guide you to your solution.

petes1234a at 2007-7-29 18:21:07 > top of Java-index,Java Essentials,New To Java...
# 6

Hello Pete1234,

You are right, that was my mistake. I will know at run time the number of elements to delete but the number can vary. The implementation is about an Array of Strings, whereby the String elements must meet certain threshold if not it will be delete. So the number of elements to delete can vary from array to array. It can be 1or 2 or 5 or even the whole elements.

The method will receive the index of the elements to be deleted and the String Array which contains the Elements from where the deletion will be made.

Thanks,

Jona_T

Jona_Ta at 2007-7-29 18:21:07 > top of Java-index,Java Essentials,New To Java...
# 7

so how are you planning on passing the indices that need deleting into the method? as an array itself? Write out in "code" what you think this method would look like, whether it works or not.

petes1234a at 2007-7-29 18:21:07 > top of Java-index,Java Essentials,New To Java...