【问题标题】:Insert elements or remove element from a specified position in 2d-array in java在java中的二维数组中插入元素或从指定位置删除元素
【发布时间】:2018-02-06 04:36:17
【问题描述】:

我有一个可变数量的行和列的二维数组。该数组以 JSON 格式存储在数据库中。我需要在用户指定的某个元素之后添加给定元素或删除用户指定的元素。数组中的每个元素都是唯一的。示例 json 值类似于 [[4,5],[3,1,6,7],[34,21,55]]。考虑一种情况,我想在元素 7 之后添加 2 个元素 12,13,结果数组看起来像 [[4,5],[3,1,6,7,12,13],[34,21,55]]。如果我给定 1 作为输入,则要删除结果应该是[[4,5],[3,6,7,12,13],[34,21,55]]。使用Gson 我已经解析了存储到数组中的json值。如何在 Java 中以更少的时间复杂度实现它。

我从 db 解析 json 数据的代码看起来像

Gson gson = new GsonBuilder().create();
if (myTemplate.getQuestionOrder() != null) {
    long[][] questionOrder = gson.fromJson(myTemplate.getQuestionOrder(), long[][].class);
}

【问题讨论】:

    标签: java arrays multidimensional-array


    【解决方案1】:

    试试下面的方法。

    private static void insertAfter(long[][] array, int value, long[] insertion) {
        boolean found = false;
        for (int i = 0; i < array.length; i++) {
            long[] sub = array[i];
            for (int j = 0; j < sub.length; j++) {
                if (sub[j] == value) {
                    long[] newSub = new long[sub.length + insertion.length];
                    System.arraycopy(sub, 0, newSub, 0, j + 1);
                    System.arraycopy(insertion, 0, newSub, j + 1, insertion.length);
                    System.arraycopy(sub, j + 1, newSub, j + 1 + insertion.length, sub.length - j - 1);
                    array[i] = newSub;
                    found = true;
                    break;
                }
            }
            if (found) break;
        }
    }
    

    示例用法:

    insertAfter(questionOrder, 7, new long[]{12, 13});
    System.out.println(gson.toJson(questionOrder)); 
    

    这将打印[[4,5],[3,1,6,7,12,13],[34,21,55]]

    要删除元素,您可以使用类似但稍作修改的逻辑:

    private static long[][] remove(long[][] array, int value) {
        boolean found = false;
        int emptyIndex = -1;
        for (int i = 0; i < array.length; i++) {
            long[] sub = array[i];
            for (int j = 0; j < sub.length; j++) {
                if (sub[j] == value) {
                    long[] newSub = new long[sub.length - 1];
                    System.arraycopy(sub, 0, newSub, 0, j);
                    System.arraycopy(sub, j + 1, newSub, j, sub.length - j - 1);
                    array[i] = newSub;
                    if (array[i].length == 0) emptyIndex = i;
                    found = true;
                    break;
                }
            }
            if (found) break;
        }
        if (emptyIndex >= 0) {
            long[][] newArray = new long[array.length - 1][];
            System.arraycopy(array, 0, newArray, 0, emptyIndex);
            System.arraycopy(array, emptyIndex + 1, newArray, emptyIndex, array.length - emptyIndex - 1);
            array = newArray;
        }
        return array.length == 0 ? null : array;
    }
    

    此方法将从内部数组中删除给定的项目,如果内部数组为空,则将其从外部数组中删除。它返回修改后的数组,如果为空则返回null

    示例用法:

    questionOrder = remove(questionOrder, 4);
    

    【讨论】:

    • 你能说说我们需要什么来删除一个特定的元素
    • @SenchuThomas 检查我的更新日期
    • 感谢您的帮助。我发现一个小的差异,例如当我请求删除 5 时,我有一个 [[5]] 的数组,现在结果数组是 [[]]。但实际上它想要为 null,因为它不包含任何元素
    • @SenchuThomas 不客气。要返回 null,只需将 array[i] = newSub; 替换为 array[i] = newSub.length &gt; 0 ? newSub : null;
    • @SenchuThomas 如果你想从外部数组中删除一个空的内部数组,你需要记住它的索引并在退出循环后创建几个System.arraycopy。同样在这种情况下,您必须使函数返回一个新数组 - 因为我们无法更改给定数组的长度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多