【问题标题】:How can you reduce the indices of elements in a string array after deleting an element from it? [duplicate]从字符串数组中删除元素后,如何减少字符串数组中元素的索引? [复制]
【发布时间】:2020-12-23 08:41:30
【问题描述】:

我有一个任务,创建一个获取字符串数组元素的方法,检查是否存在重复项,然后删除它(我尝试使用“null”),然后将所有其他元素移向索引值 [0] 以缩小差距。

现在看起来像这样:

public static boolean deleteTask() {
    boolean removed = false;
    for (int pos = 0; pos < todos.length; pos++) {
        if (todos[pos].equals(titel)) {
            todos[pos] = null;
            removed = true;
   
            if (removed){
                //set pos+1 = pos to reduce each value -1. 
                //repeat process for each index [10]
                }
            }
        }
        return removed;
    }
}

在图片中,我展示了我看到的结果。 例如。 pos.4 是重复的 - 然后它被设置为 null。现在必须将以下所有索引更改为 -1 以填补空白。 显然,索引然后设置回 456 而不是 567 这只是为了说明字符串的移动。

你能帮我在 [pos] null 之后向 -1 方向移动索引吗?

如果您可以帮助对 2 个以上的重复项做同样的事情,那就更好了。

【问题讨论】:

  • 您知道被删除元素的位置,并且您知道基本的for 循环是什么。你也知道算法是如何的,你只是画了它。那么这里到底有什么不清楚的地方呢?
  • [pos +1] = [pos] ... 为什么 +1?
  • 在 [pos] 4/null 上,我想从右边/下一个索引(或当前位置 +1 [pos+1] )中取出字符串,即图片中的 5 和将其拉回左侧当前索引位置 [pos]/4 。然后对 5.6.7 等执行相同操作,直到 todos.length-1

标签: java arrays


【解决方案1】:

代替

todos[pos + 1] = todos[pos];

你应该使用

todos[pos] = todos[pos + 1];

这是工作代码:

public static boolean deleteTask() {
    boolean removed = false;
    for (int pos = 0; pos < todos.length; pos++) {
        if (todos[pos].equals(titel)) {
            todos[pos] = null;
            removed = true;
        }
        if (removed && pos < todos.length - 1) {
            // swap the string with the next one
            // you can't do this with the last
            // element because [pos + 1] will
            // throw an indexoutofbounds exception
            todos[pos] = todos[pos + 1];
        } else if (removed && pos == todos.length - 1) {
            // here you can swap the last one with null
            todos[pos] = null;
        }
    }
    return removed;
}

【讨论】:

  • 谢谢你这个工作,虽然这里的逻辑有点扭曲恕我直言。据我了解,这是修复重复项,并在每次迭代后将其移到 else if 将其更改为 null 之后。一般来说,肯定有一种更优雅的方式,但现在这个方式很棒:)
【解决方案2】:

可以使用Arrays.stream方法遍历这个数组的元素,filter去掉不必要的元素,或者只保留distinct元素,重新组装相同长度的数组如下:

String[] arr = {"aaaa", "cccc", "aaaa", "bbbb", "aaaa"};

System.out.println(Arrays.toString(arr)); // [aaaa, cccc, aaaa, bbbb, aaaa]
String[] arr1 = Arrays.stream(arr)
        .filter(s -> !s.equals("aaaa"))
        .toArray(q -> new String[arr.length]);

System.out.println(Arrays.toString(arr1)); // [cccc, bbbb, null, null, null]
String[] arr2 = Arrays.stream(arr)
        .distinct()
        .toArray(q -> new String[arr.length]);

System.out.println(Arrays.toString(arr2)); // [aaaa, cccc, bbbb, null, null]

另见:
How to find duplicate elements in array in effective way?
How to get elements of an array that is not null?

【讨论】:

    【解决方案3】:

    使用数组时,您要么将值向下移动以缩小差距,要么将元素复制到一个新的更小的数组中。

    【讨论】:

    • 是的,如果这就是你所说的“降档”的话,基本上这就是我想要做的。对于后续字符串,我需要保持数组大小相同。
    猜你喜欢
    • 2015-06-23
    • 2017-12-24
    • 1970-01-01
    • 2023-04-09
    • 2020-08-29
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    相关资源
    最近更新 更多