Java – Inserts or deletes elements at specified locations in a two-dimensional array in Java

Inserts or deletes elements at specified locations in a two-dimensional array in Java… here is a solution to the problem.

Inserts or deletes elements at specified locations in a two-dimensional array in Java

I have a two-dimensional array with a variable number of rows and columns. The array is stored as JSON in the database. I need to add a given element or remove a user-specified element after a certain element specified by the user. Each element in the array is unique. Example JSON values are similar to [[4,5],[3,1,6,7],[34,21,55]]. Consider the case where I want to add 2 elements 12,13 after element 7, and the resulting array looks like [[4,5],[3,1,6,7,12,13],[34,21,55]]. If I give 1 as input, the result to be deleted should be [[4,5],[3,6,7,12,13],[34,21,55]]. Using gson, I’ve parsed JSON values stored in arrays. How can I implement it in Java with less time complexity.

My code to parse json data from the database looks like

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

Solution

Try the method below.

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

Example usage:

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

This prints [[4,5],[3,1,6,7,12,13],[34,21,55]].

To delete an element, you can use similar but slightly modified logic:

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

This method will remove the given item from the inner array and remove it from the outer array if the inner array becomes empty. It returns the modified array or, if it is empty, null.

Example usage:

questionOrder = remove(questionOrder, 4);

Related Problems and Solutions