Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中字符串数组的顺序搜索_Java_Sequential_Linear Search - Fatal编程技术网

Java中字符串数组的顺序搜索

Java中字符串数组的顺序搜索,java,sequential,linear-search,Java,Sequential,Linear Search,我应该在字符串数组上编写顺序/线性搜索。我很快就要完成了,但部分作业让我感到困惑。它表示将目标项与列表中的连续元素进行比较,直到目标匹配或目标小于数组中的当前元素。当没有数值时,一个字符串怎么可能比另一个元素大或小?也许我只是想得不对。以下是我目前的计划: public class SequentialSearchString { public static boolean sequential (String[] numbers){ //Set the target item to a

我应该在字符串数组上编写顺序/线性搜索。我很快就要完成了,但部分作业让我感到困惑。它表示将目标项与列表中的连续元素进行比较,直到目标匹配或目标小于数组中的当前元素。当没有数值时,一个字符串怎么可能比另一个元素大或小?也许我只是想得不对。以下是我目前的计划:

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");  
        }   
    }   
}
如果您能帮助理解此任务并修复异常,我们将不胜感激

numbers[i] = numbers[i+1];
很可能会导致您的
阵列索引超出边界异常

你的子句检查
i
。所以你设定了界限。但是,如果
i==numbers.length-1
,则您将尝试访问
i+1
,它比您的数组大,因此超出范围

例如:
numbers.length
4
。所以
i
可以
3
。使用
i+1
尝试访问
numbers[4]
,这将是数组的第五个位置,因为数组从
0
开始,
numbers[3]
将是最后一个位置。

ArrayIndexOutofBounds异常是由于您使用了以下事实:

for (int i = 0; i < numbers.length; i++)
当i等于numbers.length-1(最后一次迭代)时,i+1等于numbers.length。然后尝试读取错误的数字[numbers.length](有效索引从0到numbers.length-1)

你必须使用:

for(int i=0;i<numbers.length-1;i++) 

for(int i=0;iFor info:您不能使用==和!=比较字符串值,它不起作用(它只比较对象的“地址”)。您必须使用string1。equals(string2)使用
String#compareTo
来比较字符串。这里有一个相关问题解释了更多内容:。至于“小于”调用Comparable.compareTo()时,Java字符串实现Comparable要将一个对象与另一个对象进行比较,返回值表示对象的相对自然顺序。在字符串的情况下,自然顺序是按字典顺序排列的。我按照您的建议进行了操作,确保异常消失,程序运行,但有一个问题。我测试了每个元素以及第一个和最后一个(“Ada”)和“Joan”)返回失败,即使它们实际上在列表中。有什么方法可以修复此问题吗?
 numbers[i] = numbers[i+1];
for(int i=0;i<numbers.length-1;i++)