The best way to remove an object from an array in the Processing section

I really want Processing to have push and pop methods for working with arrays, but since that is not the case, I remain trying to find the best way to delete an object at a specific position in the array. I am sure that it is as simple as for many people, but I could work with him a little, and I could not understand much by looking at the processing link.

I do not think this is important, but for your reference, the code that I used to initially add objects is used here:

Flower[] flowers = new Flower[0]; for (int i=0; i < 20; i++) { Flower fl = new Flower(); flowers = (Flower[]) expand(flowers, flowers.length + 1); flowers[flowers.length - 1] = fl; } 

For this question, let me assume that I want to remove an object from position 15. Thank you guys.

+4
source share
3 answers

I think it is best to use arracycopy. You can use the same array for src and dest. Something like the following (untested):

 // move the end elements down 1 arraycopy(flowers, 16, flowers, 15, flowers.length-16); // remove the extra copy of the last element flowers = shorten(flowers); 
0
source

You may also consider using an ArrayList , which has more methods available than a simple array.

You can remove the fifteenth element using myArrayList.remove(14)

+6
source

I know this question has been asked for a long time, but it seems that many people are still looking for the answer. I just wrote this. I tested it in several ways, and it seems to work the way I wanted it to.

 var yourArr = [1, 2, 3, 4]; // use your array here var removeIndex = 1; // item to get rid of var explode = function(array, index) { // create the function var frontSet = subset(array, 0, index - 1); // get the front var endSet = subset(array, index , array.length - 1); // get the end yourArr = concat(frontSet, endSet); // join them }; explode(yourArr, removeIndex); // call it on your array 

This is one way. I think you could also scroll through the array. Sort of...

 var yourArr = [1, 2, 3, 4]; var removeIndex = 2; var newArr = []; for(var i = 0; i < yourArr.length; i++) { if(i < removeIndex) { append(newArr, yourArr[i]); } else if(i > removeIndex) { append(newArr, yourArr[i]); } } yourArr = newArr; 

... I think this will work too. Hope this helps anyone who needs it.

-1
source

Source: https://habr.com/ru/post/1304333/


All Articles