【问题标题】:Infinite loop when calling a method调用方法时无限循环
【发布时间】:2014-10-26 16:36:01
【问题描述】:

当我尝试在我的 Bubblesort 代码中使用我的 Compare 方法来确定要排序的字符串的字母顺序时,我陷入了 Compare 方法中的无限循环中,否则它会起作用。

有什么想法吗? 谢谢!

public static int Compare(String s1 , String s2) { //compares two strings alphabetically
    int result = 0 ;
    String newS1 = "";
    String newS2 = "";

    for (int i = 0; i< s1.length(); i++){  //loop converts strings from uppercase to lowercase
        char s1Char = s1.charAt(i);
        if (65 <= s1Char && s1Char <=90){
            s1Char = (char)( (s1Char + 32) ); 
        }
        newS1 = newS1 + s1Char;  
    }

    for (int i = 0; i< s2.length(); i++){
        char s2Char = s2.charAt(i);
        if (65 <= s2Char && s2Char<=90){
            s2Char = (char)( (s2Char + 32) ); 
        }
        newS2 = newS2 + s2Char;   
    }

    for (int i = 0 ; i <newS1.length();){    //compares the two lowercase strings by ascii value
        if (newS1.charAt(i) < newS2.charAt(i)){
            result = -1 ;
            System.out.println(newS1+ " is before " +newS2+ " in the dictionary");
            break ;
        }
        if (newS1.charAt(i) > newS2.charAt(i)){
            result = 1 ;
            System.out.println(newS2+ " is before " +newS1+ " in the dictionary");
            break ;
        }
        if (newS1.charAt(i) == newS2.charAt(i)) {
            i++ ;
        }
        else {
            result = 0 ;
            System.out.println("The words are the same.");
            break ;
        }
    }
    return result ;
}

public static int bubbleSort(String[] input_array) {  //sorts an array alphabetically
    int n = input_array.length ;
    String temp = "" ;
    int comparisons = 0 ;
    boolean swap = false ;
    while (swap == true) {
        swap = false ;
        for(int i = 1 ; i < input_array.length - 1 ; ){
            if(Compare(input_array[i] , input_array[i+1]) == -1)  { //use compare method above 
                comparisons++ ;                                     
                temp = input_array[i] ;
                input_array[i] = input_array[i-1] ;                 //swap if sorting needed
                input_array[i-1] = temp ;
                swap = true ;
                System.out.println(input_array);
                i++ ;
            }
            else {
                i++ ;
            }
        }
    }
    return comparisons;
}

public static void main(String[] args) {   // used to test code
    String[] testArray = {"b" , "c" ,"e" , "d"} ;
    bubbleSort(testArray) ;
}

【问题讨论】:

  • 首先:boolean swap = false ;,然后是while (swap == true) {。它永远不会进入while循环

标签: java infinite-loop bubble-sort


【解决方案1】:

在这里您将 swap 分配为等于 true。

 while (swap = true) {

我猜你想比较一下。

boolean swap = true;
while (swap == true){ //Or better while (swap){
   swap = false;

【讨论】:

  • 我已经修复了赋值错误,但现在似乎代码根本没有到达 for 循环。
【解决方案2】:

我认为Compare 中的最后一个if 应该始终是true,所以else 永远不会执行。

但我看到的主要问题是Swap 不起作用。

ij 是对字符串的引用的副本,并且字符串本身是不可变的。 在Swap 的末尾,i 指向最初由j 指向的字符串,反之亦然,但字符串本身并没有改变,并且数组将它们放在相同的位置。

如果您需要单独的 Swap 方法,请改为传递数组和索引。

【讨论】:

  • 我把swap方法切换到了代码本身,这样就不用通过了?但是现在代码根本没有执行。
  • 您在循环之前将swap 初始化为false
猜你喜欢
  • 2020-05-03
  • 2014-02-01
  • 2019-04-02
  • 2021-09-24
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多