英文:
Removing an element from an array without using list
问题
我一直在努力编写一个方法,通过创建一个新数组,将其大小减小1,并将除了“position”位置上的项目之外的所有内容复制过去,以便从数组中删除项目。有什么原因导致我的代码不起作用吗?不过我不能使用列表函数,必须使用数组。
int newArray[] = new int[originalArray.length - 1];
for (int i = 0; i < originalArray.length; i++) {
if (i < position)
newArray[i] = originalArray[i];
else if (i > position)
newArray[i - 1] = originalArray[i];
}
英文:
I've been trying for ages to make a method that lets me remove an item from an array by making a new array, reducing it's size by 1, and then copying everything over except for the item in "position". Any reason why my code wouldn't be working? I can't use the list function though, I have to use arrays.
int newArray[] = new int[originalArray.length - 1];
for (int i = 0; i < originalArray.length; i++) {
if (i < position)
newArray[i] = originalArray[i];
else if (i == position)
i++;
else if (i > position)
newArray[i] = originalArray[i];
}
答案1
得分: 1
你不应该尝试编写超出范围的元素,尝试这样做:
int newArray[] = new int[originalArray.length - 1];
for (int i = 0; i < newArray.length; i++) {
if (i < position)
newArray[i] = originalArray[i];
else
newArray[i] = originalArray[i+1];
}
英文:
You shouldn't try to write element out of range, try this:
int newArray[] = new int[originalArray.length - 1];
for (int i = 0; i < newArray.length; i++) {
if (i < position)
newArray[i] = originalArray[i];
else
newArray[i] = originalArray[i+1];
}
答案2
得分: 1
public static int[] removeElement(int[] originalArray, int position) {
int newArray[] = new int[originalArray.length - 1];
for (int i = 0; i < originalArray.length; i++) {
if (i < position)
newArray[i] = originalArray[i];
else if (i > position)
newArray[i - 1] = originalArray[i];
}
return newArray;
}
// P.S. I reccomend you to use `System.arraycopy()`:
public static int[] removeElement(int[] arr, int pos) {
if (pos < 0 || pos >= arr.length)
return arr;
if (arr.length == 1)
return new int[0];
int[] res = new int[arr.length - 1];
System.arraycopy(arr, 0, res, 0, pos);
System.arraycopy(arr, pos + 1, res, pos, arr.length - pos - 1);
return res;
}
// Output:
int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] res = removeElement(arr, 4);
System.out.println(Arrays.toString(res)); // [0, 1, 2, 3, 5, 6, 7, 8, 9]
英文:
public static int[] removeElement(int[] originalArray, int position) {
int newArray[] = new int[originalArray.length - 1];
for (int i = 0; i < originalArray.length; i++) {
if (i < position)
newArray[i] = originalArray[i];
else if (i > position)
newArray[i - 1] = originalArray[i];
}
return newArray;
}
P.S. I reccomend you to use System.arraycopy()
:
public static int[] removeElement(int[] arr, int pos) {
if (pos < 0 || pos >= arr.length)
return arr;
if (arr.length == 1)
return new int[0];
int[] res = new int[arr.length - 1];
System.arraycopy(arr, 0, res, 0, pos);
System.arraycopy(arr, pos + 1, res, pos, arr.length - pos - 1);
return res;
}
Output:
int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] res = removeElement(arr, 4);
System.out.println(Arrays.toString(res)); // [0, 1, 2, 3, 5, 6, 7, 8, 9]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论