【问题标题】:for-loop not iterating completelyfor循环没有完全迭代
【发布时间】:2016-03-03 13:25:57
【问题描述】:

我正在尝试创建一个算法来测试给定的字符串是否是字符串列表的覆盖字符串。一个字符串是一个字符串列表的覆盖字符串,如果它包含每个字符串中从左到右顺序的字符。例如,“house”和“hotel”的覆盖字符串为“ahogjutsel”,非覆盖字符串的示例为“ahogjsutel”。

我面临的问题是我的 for 循环在返回输出之前只完成了一次迭代。我试图一个一个地遍历列表中的每个字符串,检查每个字符的索引以确保保持从左到右的顺序。

任何关于如何修改我的 for 循环以便算法遍历每个字符串中的每个字符的建议都会非常有帮助。

公共类 StringProcessing {

//Array list to add list of strings for testing.
public static ArrayList<String> stringList = new ArrayList<>();

public static String list1 = "abc";

public static String list2 = "def";  


//Algorithm to iterate through each word in stringList and test if it appears in the cover string
//by testing index values.
public static boolean isCover(String coverString){
    boolean isCover = false;
    stringList.add(list1);
    stringList.add(list2);
    int size = stringList.size();
    int coverSize = coverString.length();

for (int i = 0; i < (size -1) ; i ++){
        for (int j = 0; j<stringList.get(i).length(); j++){              

        if (coverString.indexOf(stringList.get(i).charAt(j)) < coverString.indexOf(stringList.get(i).charAt(j+1))){
            return true;
        }
        else
            return isCover;
        }     
}
return isCover;
}


public static void main(String[] args) {

//For loop only checks if a is before b, then returns true before checking the rest of the characters and strings.
    System.out.println(StringProcessing.isCover("abfdec"));

}
}

【问题讨论】:

  • 如果你的函数的目的是测试参数是否是一个覆盖字符串,那你为什么要在函数内部定义stringList的内容呢?这会产生副作用,因为 stringList 的值将超出每个函数调用。
  • 你能解决这个问题吗?
  • @Perdomoff 我能够解决此算法并使其正常运行,感谢您的所有输入。

标签: java string loops for-loop iteration


【解决方案1】:
  1. 在您的 if 条件中,您将返回一个值,这将结束您的循环。

  2. 编辑 将字符串的 Arraylist 与字符串进行比较。

    编辑 11/30/15:考虑字母顺序以确定单词是否为封面字符串。

改变你的方法:

public class StringProcessing2 {
public static ArrayList<String> stringList = new ArrayList<>();
//Negative Case
public static String list1 = "house";
public static String list2 = "hotel";  

//Positive Case
//public static String list1 = "abc";
//public static String list2 = "def";  


public static boolean isCover(String word){
    int matchedWords = 0;
    stringList.add(list1);
    stringList.add(list2);

    for(int i = 0; i < stringList.size(); i++){
            if(word.contains(String.valueOf(stringList.get(i)) )){
                matchedWords++;                                
      }
}
    if(matchedWords == stringList.size()){
        return true;
    }
    else
        return false;           
}


public static void main(String[] args) {
 System.out.println(isCover("ahogjutsel"));
}
}

【讨论】:

  • 感谢您的澄清,然后我将如何修改此 if 语句的主体以仅在 stringList 中每个字符串中的所有字符的表达式为 true 时才返回 true
  • @Mickd94,主要提供的字符串需要包含字符串数组列表中每个字符串的所有字符?
  • 非常感谢您的回答,这非常有帮助。要成为覆盖字符串,main中提供的字符串必须包含arraylist中每个字符串的所有字符,保持字符串从左到右的字符顺序,我试图在第一段描述中展示一个示例。
  • 我已经测试了代码,如果每个字符串的字符都存在于封面字符串中,代码返回true,但是即使字符从左到右的顺序没有保持在封面字符串中也返回true ,例如在上面的示例字符串中,输出应该是假的,因为 list2 和 list3 的从左到右的顺序没有被维护,但是它返回 true。我将考虑修改内部 if 语句来改变它。我必须再次感谢您的帮助。
  • @Mickd94,这是你直到现在或在问题上才提到的细节。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
相关资源
最近更新 更多