Java 数组中的搜索和删除错误
在我的程序中,我首先搜索一个值,如果它存在,则给出数组位置,如果不存在,则给出-1,但当我尝试在程序中返回错误消息时,无法找到输入的值,因此未删除,我收到一个实际错误。这听起来可能令人困惑,如果您看不到我的代码,我可以尝试解释更多。 有人能帮忙吗Java 数组中的搜索和删除错误,java,arrays,class,search,Java,Arrays,Class,Search,在我的程序中,我首先搜索一个值,如果它存在,则给出数组位置,如果不存在,则给出-1,但当我尝试在程序中返回错误消息时,无法找到输入的值,因此未删除,我收到一个实际错误。这听起来可能令人困惑,如果您看不到我的代码,我可以尝试解释更多。 有人能帮忙吗 private int search(int s){ int position = 0; while(nums[position] != s){ if(position < size) po
private int search(int s){
int position = 0;
while(nums[position] != s){
if(position < size)
position++;
else
position = -1;
}
return position;
}
public void delete(int d){
int value = search(d);
if(search(d) == -1){
System.out.println("The value " + d + " was not found and cannot be deleted");
}
else
for(int n = size; n > value; n--)
nums[value] = nums[size];
}
我不懂Java,但我懂C。我认为搜索方法搜索现有条目 如果出现条件,则不要使用
在搜索代码和Catch块中捕获异常,执行以下操作:
System.out.println("The value " + d + " was not found and cannot be deleted");
确保变量size
等于数组长度nums.length-1代码>
int size=nums.length-1;
您的信息不足以完全跟踪代码,但这一行:
for(int n = size; n > value; n--)
nums[value] = nums[size];
看起来很可疑
根据OP的评论进行编辑:
while(nums[position] != s){
if(position < size)
position++;
编辑3:
在这里,我添加了比上次编辑更好的代码
这已删除数组元素。
通常我不会为你工作,但如果没有一点技术知识,这是很难编写的代码,至少在我看来,除非你想继续编码和修复
您的搜索成功了,但删除实现没有成功。如前所述,for循环不正确。它不断重复相同的指令(将搜索到的值放在数组的末尾,永远删除before值),如果您想保留它,结果是您没有删除任何重要的内容,甚至丢失了信息
下面的代码将帮助您了解如何完成程序的预期功能
public class SearchTest {
static int nums[] = new int[] { 1, 2, 3, 4, 5 };
public static void main(String[] args) {
delete(2);
//deletes the number 2, if found
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
public static void delete(int d) {
int value = search(d);
if (value == -1) {
System.out.println("The value " + d
+ " was not found and cannot be deleted");
} else
//removes the element, since it was found
nums = removeElement(nums, value);
}
/**
*http://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array
* This is the remove method from this post, look at this link for more
**/
public static int[] removeElement(int[] original, int index) {
int[] n = new int[original.length - 1];
System.arraycopy(original, 0, n, 0, index);
System.arraycopy(original, index + 1, n, index, original.length - index
- 1);
return n;
}
private static int search(int s) {
int position = 0;
while (nums[position] != s) {
// changed to nums.length
if (position < nums.length)
position++;
else
position = -1;
}
return position;
}
}
公共类搜索测试{
静态int nums[]=新int[]{1,2,3,4,5};
公共静态void main(字符串[]args){
删除(2);
//如果找到,则删除数字2
对于(int i=0;i
代码中哪一行是行44?它的while(nums[position]!=s){实际上我只是随机插入了它,它起了作用,我还没有试图找出它背后的逻辑是我最后一句话是真的?“大小等于nums.lenght”实际上,size是一个单独的int,用于跟踪已添加到数组中的int数量,因此如果将4个int添加到长度为10的数组中,那么size将是4以便更好地跟踪,在while
之前打印长度和大小,并在while
的第一行中打印位置,I t你会很容易发现这个问题,如果你不清楚,请报告结果。仍然在检查Delete方法,它似乎有很多错误。就像之前建议你应该使用类似ArrayList的东西一样,正如Mok所说。如果size意味着数组中的元素数,你应该用它替换我的代码大小而不是nums.length(如果使用)。
ArrayList<Integer> nums = new ArrayList<Integer>();
//....
return nums.indexOf(s); //s is an Integer
public class SearchTest {
static int nums[] = new int[] { 1, 2, 3, 4, 5 };
public static void main(String[] args) {
delete(2);
//deletes the number 2, if found
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
public static void delete(int d) {
int value = search(d);
if (value == -1) {
System.out.println("The value " + d
+ " was not found and cannot be deleted");
} else
//removes the element, since it was found
nums = removeElement(nums, value);
}
/**
*http://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array
* This is the remove method from this post, look at this link for more
**/
public static int[] removeElement(int[] original, int index) {
int[] n = new int[original.length - 1];
System.arraycopy(original, 0, n, 0, index);
System.arraycopy(original, index + 1, n, index, original.length - index
- 1);
return n;
}
private static int search(int s) {
int position = 0;
while (nums[position] != s) {
// changed to nums.length
if (position < nums.length)
position++;
else
position = -1;
}
return position;
}
}