Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.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 阵列StringLog单元测试最小方法_Java - Fatal编程技术网

Java 阵列StringLog单元测试最小方法

Java 阵列StringLog单元测试最小方法,java,Java,我是一名新程序员,我正在尝试使用最小的方法通过单元测试来传递这段代码 // returns the first string in a lexicographic ordering of the log public String smallest(){ String smallest = log[0]; for(int i= 0;i <= lastIndex; i++){ if(log[i].compareTo(smallest)<

我是一名新程序员,我正在尝试使用最小的方法通过单元测试来传递这段代码

// returns the first string in a lexicographic ordering of the log
public String smallest(){
    String smallest = log[0];

        for(int i= 0;i <= lastIndex; i++){

            if(log[i].compareTo(smallest)< 0){ //compare to smallest
                                               //known not log[i+1]

                smallest = log[i];

            } else {

                lastIndex++;
        }
        return smallest;
我的问题是,它通过了1个元素,并且在开始测试时通过了两个元素,但是其他两个元素都失败了,我不知道为什么。它应该在数组中单步执行,比较循环所在的当前索引和它旁边的索引


它抛出的错误应为:字符串2,但为字符串1,另一个错误应为:字符串3,但为字符串1。我试图找到数组中最小的字符串,不管它位于什么位置。我不明白为什么assertEquals在查找字符串1时会看到字符串2或3。

在方法
minimest()
的循环中,不确定为什么需要添加else条件
lastIndex++
。此变量不应更改。能否删除该条件并重试?下面是一个代码示例:

public String smallest(){
    String smallest = log[0];
    for(int i= 0;i <= lastIndex; i++){
            if(log[i].compareTo(smallest)< 0){ //compare to smallest
                                               //known not log[i+1]
                smallest = log[i];
    }
    return smallest;
}
公共字符串(){
字符串最小值=log[0];

对于(int i=0;i where is
lastIndex
已定义-当然
log.size()
会更好有时候调试程序会更好(更快)在顶部,很抱歉我没有粘贴它。protected int lastIndex=-1;任何指向
-1-1
的循环都不起作用,你需要根据你的
日志的大小在
最小的
中设置它的值,在我的测试中,我用5个“插槽”初始化了数组。我最初是这样做的(i=0;ipublic String smallest(){
    String smallest = log[0];
    for(int i= 0;i <= lastIndex; i++){
            if(log[i].compareTo(smallest)< 0){ //compare to smallest
                                               //known not log[i+1]
                smallest = log[i];
    }
    return smallest;
}