Java中字符串数组的顺序搜索
我应该在字符串数组上编写顺序/线性搜索。我即将完成,但作业的一部分让我感到困惑。它表示将目标项与列表的连续元素进行比较,直到目标匹配或目标小于数组的当前元素。当没有数值时,字符串如何大于或小于另一个元素?也许我只是没有正确思考。到目前为止,这是我的程序:
public class SequentialSearchString {
public static boolean sequential (String[] numbers){
//Set the target item to an arbitrary String that should return true.
String T1 = "Frank";
for (int i = 0; i < numbers.length; i++){
if (numbers[i] == T1){
return true;
}
if (numbers[i] != T1){
numbers[i] = numbers[i+1];
}
}
return false;
}
public static boolean sequential2 (String[] numbers){
//Set the target key to String that should return false.
String T2 = "Ian";
for (int i = 0; i < numbers.length; i++){
if (numbers[i] == T2){
return true;
}
if (numbers[i] != T2){
numbers[i] = numbers[i+1];
}
}
return false;
}
public static void main(String[] args) {
//Create a list of 8 Strings.
String [] numbers =
{"Ada", "Ben", "Carol", "Dave", "Ed", "Frank", "Gerri", "Helen", "Iggy", "Joan"};
//If the first target item (T1) is found, return Succuss. If not, return failure.
if (sequential(numbers) == true){
System.out.println("Success. 'T1' was found");
}
else {
System.out.println("Failure. 'T1' was not found");
}
//If the second target item (T2) is found, return Succuss. If not, return failure.
if (sequential2(numbers) == true){
System.out.println("Success. 'T2' was found");
}
else {
System.out.println("Failure. 'T2' was not found");
}
}
}
第一种方法工作正常,但我似乎在搜索不在列表中的元素时遇到问题。这是运行程序后收到的错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at SequentialSearchString.sequential2(SequentialSearchString.java:32)
at SequentialSearchString.main(SequentialSearchString.java:50)
Success. 'T1' was found
任何帮助理解分配和修复异常的帮助将不胜感激。
I am supposed to write sequential/linear search on a String array. I am very close to finishing, but part of the assignment confuses me. It says to compare the target item with successive element of the list until the target matches or target is less than the current element of the array. How can a String be more or less than another element when there is no numerical value? Maybe I'm just not thinking about it correctly. Here is my program so far:
public class SequentialSearchString {
public static boolean sequential (String[] numbers){
//Set the target item to an arbitrary String that should return true.
String T1 = "Frank";
for (int i = 0; i < numbers.length; i++){
if (numbers[i] == T1){
return true;
}
if (numbers[i] != T1){
numbers[i] = numbers[i+1];
}
}
return false;
}
public static boolean sequential2 (String[] numbers){
//Set the target key to String that should return false.
String T2 = "Ian";
for (int i = 0; i < numbers.length; i++){
if (numbers[i] == T2){
return true;
}
if (numbers[i] != T2){
numbers[i] = numbers[i+1];
}
}
return false;
}
public static void main(String[] args) {
//Create a list of 8 Strings.
String [] numbers =
{"Ada", "Ben", "Carol", "Dave", "Ed", "Frank", "Gerri", "Helen", "Iggy", "Joan"};
//If the first target item (T1) is found, return Succuss. If not, return failure.
if (sequential(numbers) == true){
System.out.println("Success. 'T1' was found");
}
else {
System.out.println("Failure. 'T1' was not found");
}
//If the second target item (T2) is found, return Succuss. If not, return failure.
if (sequential2(numbers) == true){
System.out.println("Success. 'T2' was found");
}
else {
System.out.println("Failure. 'T2' was not found");
}
}
}
The first method works fine, but I appear to be having issues with searching for elements that are not in the list. Here is the error message I get after running the program:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at SequentialSearchString.sequential2(SequentialSearchString.java:32)
at SequentialSearchString.main(SequentialSearchString.java:50)
Success. 'T1' was found
Any help understanding the assignment and fixing the exception would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很可能会导致您的
ArrayIndexOutOfBoundsException
。您的子句检查
i <数字.长度
。所以你设定了界限。但是,如果i ==numbers.length - 1
,那么您将尝试访问i+1
,它比您的数组大,因此它超出了范围。例如:
numbers.length
为4
。所以i
可以是<代码>3。使用
i+1
,您尝试访问numbers[4]
,这将是第五个位置,因为数组以0
和numbers[开头3]
将是最后一个位置。Is likely to cause your
ArrayIndexOutOfBoundsException
.Your clause checks
i < numbers.length
. So you set the bounds. However ifi == numbers.length - 1
then you will try to accessi+1
which is larger then your array so it is out of bounds.For example:
numbers.length
is4
. Soi
can be3
. Withi+1
you try to accessnumbers[4]
which would be the fifth position as arrays start with0
andnumbers[3]
would be the last position.ArrayIndexOutOfBoundsException 是由于您使用:
和后者:
当 i 等于numbers.length-1(最后一次迭代)时,i+1 等于numbers.length。然后你尝试读取numbers[numbers.length],这是错误的(有效索引是从0到numbers.length-1)。
你必须使用 :
来防止异常。现在,我不确定它是否能解决您的整个问题,但异常肯定可以。
The ArrayIndexOutOfBoundsException is due to the fact you use:
and latter:
When i equals numbers.length-1 (last iteration), i+1 equals numbers.length. Then you try to read numbers[numbers.length] which is wrong (valid index is from 0 to numbers.length-1).
You've got to use :
to prevent the exception. Now, I'm not sure it would solve your whole problem, but the Exception certainly.