3

你能把这个增强的for循环转换成普通的吗?
for (String sentence : notes.split(" *\."))

当数据类型是整数时,我喜欢增强型和普通的 for 循环。但如果它是一个字符串,我很困惑。非常感谢你!

4

8 回答 8

3
String[] sentences = notes.split(" *\.");
String sentence = null ;
int sentencesLength = sentences.length;

for(int i=0;i<sentencesLength;i++){
sentence = sentences[i];
//perform your task here

}

Eclipse Juno具有将 for-each 转换为基于索引的循环的内置功能。看看这个。

于 2012-07-24T11:57:32.673 回答
2

你应该看看For-Each 的 Doc

String[] splitted_notes = notes.split(" *\. ");
for (int i=0; i < splitted_notes.length; i++) {
  // Code Here with splitted_notes[i]
}

或者更类似于的循环for (String sentence : notes.split(" *\."))

ArrayList<String> splitted_notes = new ArrayList<>(Arrays.asList(notes.split(";")));
for(Iterator<String> i = splitted_notes.iterator(); i.hasNext(); ) {
  String sentence = i.next();
  // Code Here with sentence
}
于 2012-07-24T11:59:51.083 回答
1
String[] splitResult=notes.split(" *\.");
for (String sentence : splitResult)
于 2012-07-24T11:57:07.123 回答
1

普通的for循环——

String[] strArray = notes.split(" *\.");
String sentence = null;
  for(int i=0 ;i <strArray.length ; i++){
    sentence = strArray[i];
   }
于 2012-07-24T11:57:19.497 回答
0

split还给你String[]

String[] array=notes.split(" *\.");
for(int i=0;i<array.length();i++) {

    System.out.println(array[i]);
}
于 2012-07-24T11:57:44.827 回答
0

你可以有这样的东西:

String[] splitString = notes.split(" *\."));
for (int i = 0; i < splitString.length; i++)
{
    //...
}

或者

for(String str : splitString)
{
    //...
}
于 2012-07-24T11:57:56.103 回答
0
String [] array = notes.split(" *\."));
String sentence;
for(int i = 0; i < array.length; i ++) {
sentence = array[i];
}
于 2012-07-24T11:59:26.357 回答
0

我猜正则表达式本身是错误的。编译器会说

非法转义字符

如果

“*\。”

是正则表达式。所以我假设你试图通过拥有来分割一个字符串

. (一个点)

作为分隔符。在这种情况下,代码就像

String[] splittedNotes = notes.split("[.]");
for (int index = 0; index < splittedNotes.length; index++) {
   String sentence = splittedNotes[index];
}

礼貌地说,你可以自己尝试并得到这个。干杯。

于 2012-07-24T12:24:36.400 回答