在java中,在2d数组中的指定位置插入元素或删除元素
我有一个可变行数和列数的2d数组。数组作为JSON存储在数据库中。我需要在用户指定的某个元素之后添加给定元素,或者删除用户指定的元素。阵列中的每个元素都是唯一的。示例json值看起来像在java中,在2d数组中的指定位置插入元素或删除元素,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有一个可变行数和列数的2d数组。数组作为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,5] ] < /代码>。如果我给1作为输入,那么要删除,结果应该是[[4,5],[3,6,7,12,13],[34,21,55
[[4,5],[3,1,6,7],[34,21,55]
。考虑一个情况,我想在元素7之后添加2个元素12,13,得到的数组看起来像<代码> [[4,5],[3,1,6,7,12,13],[34,21,5] ] < /代码>。如果我给1作为输入,那么要删除,结果应该是[[4,5],[3,6,7,12,13],[34,21,55]
。如何在Java中以更少的时间复杂性实现它
我从db解析json数据的代码如下
Gson gson = new GsonBuilder().create();
if (myTemplate.getQuestionOrder() != null) {
long[][] questionOrder = gson.fromJson(myTemplate.getQuestionOrder(), long[][].class);
}
尝试以下方法
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;
}
}
这将打印[[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;
}
你能说说我们需要什么来删除一个特定的element@SenchuThomas查看我的UDPDATE谢谢你的帮助。我发现了一个小差异,例如,当我请求删除5时,我有一个[[5]]的数组,现在得到的数组是[[]]。但实际上它希望为null,因为它不包含任何elements@SenchuThomas不客气。要返回
null
只需替换array[i]=newSub代码>与数组[i]=newSub.length>0?newSub:null
@SenchuThomas如果你想从外部数组中删除一个空的内部数组,你需要记住它的索引,并在退出循环后制作两个System.arraycopy
。同样,在这种情况下,您必须使函数返回一个新数组,因为我们不能在适当的位置更改给定数组的长度。
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;
}
questionOrder = remove(questionOrder, 4);