在处理中从数组中删除对象的最佳方法

发布于 2024-08-26 00:34:58 字数 449 浏览 6 评论 0原文

我真的希望处理有 pushpop 方法来处理数组,但由于它没有,所以我只能尝试找出在某个位置删除对象的最佳方法。数组中的特定位置。我确信这对很多人来说都是最基本的,但我可以使用一些帮助,并且通过浏览处理参考我还没有弄清楚太多。

我认为这并不重要,但供您参考的是我最初用于添加对象的代码:

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;
}

为了解决这个问题,我们假设我想从位置 15 删除一个对象。谢谢,伙计们。

I really wish Processing had push and pop methods for working with Arrays, but since it does not I'm left trying to figure out the best way to remove an object at a specific position in an array. I'm sure this is as basic as it gets for many people, but I could use some help with it, and I haven't been able to figure much out by browsing the Processing reference.

I don't think it matters, but for your reference here is the code I used to add the objects initially:

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 the sake of this question, let's assume I want to remove an object from position 15. Thanks, guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜心 2024-09-02 00:34:58

您可能还需要考虑使用 ArrayList ,它比普通数组有更多可用的方法。

您可以使用 myArrayList.remove(14) 删除第十五个元素

You may also want to consider using ArrayList which has more methods available than a plain array.

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

傻比既视感 2024-09-02 00:34:58

我做了一个函数,基本上将要删除的索引切换到最后一个,然后缩短它。

int[] removeByIndex(int[] array, int index) {
  int index2 = array.length-1;
  int old = array[index];
  array[index] = array[index2];
  array[index2] = old;
  array = shorten(array);
  return array;
}

yourarray = removeByIndex(yourarray , arrayindex);

希望这有帮助!

I made a funtion which basically switches the index to be removed to the last and then shortens it.

int[] removeByIndex(int[] array, int index) {
  int index2 = array.length-1;
  int old = array[index];
  array[index] = array[index2];
  array[index2] = old;
  array = shorten(array);
  return array;
}

yourarray = removeByIndex(yourarray , arrayindex);

hope this helps!

放血 2024-09-02 00:34:58

我认为你最好的选择是使用 arraycopy。您可以对 src 和 dest 使用相同的数组。类似于以下内容(未经测试):

// 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);

I think that your best bet is to use arraycopy. 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);
烟柳画桥 2024-09-02 00:34:58
String[] myArray = { "0", "1", "2", "3", "4", "5", "6"}; 

String[] RemoveItem(String[] arr, int n) {
  if (n < arr.length-1) {
    arrayCopy(subset(arr, n+1), 0, arr, n, arr.length-1-n);
  }
  arr = shorten(arr);
  return arr;
}
String[] myArray = { "0", "1", "2", "3", "4", "5", "6"}; 

String[] RemoveItem(String[] arr, int n) {
  if (n < arr.length-1) {
    arrayCopy(subset(arr, n+1), 0, arr, n, arr.length-1-n);
  }
  arr = shorten(arr);
  return arr;
}
神经暖 2024-09-02 00:34:58

我知道这个问题很久以前就被问过,但似乎很多人仍在寻找答案。我刚刚写了这个。我测试了几种方法,它似乎按照我想要的方式运行。

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

这是一种方法。我想你也可以循环遍历数组。像……

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 know this question was asked a long time ago however it seems a lot of people are still looking for the answer. I just wrote this. I tested it a few ways and it seems to run 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

That is one way. I guess you could loop through the array as well. Something like ...

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;

... think that should work too. Hope this helps anybody who needs it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文