我知道像1 3 7 8
这样的匹配的charwise位置。我需要知道他们的相应行号。Java:根据“ n”的数字计算charwise位置的数字。
实施例:file.txt的
匹配:X
欢,:1 3 7 8
。
旺旺:1 2 4 4
$ cat file.txt
X2
X
4
56XX
[新增:没有注意到许多面向行的比赛,有可能是更容易的方法与栈做]
$ java testt
1
2
4
$ cat testt.java
import java.io.*;
import java.util.*;
public class testt {
public static String data ="X2\nX\n4\n56XX";
public static String[] ar = data.split("\n");
public static void main(String[] args){
HashSet<Integer> hs = new HashSet<Integer>();
Integer numb = 1;
for(String s : ar){
if(s.contains("X")){
hs.add(numb);
numb++;
}else{
numb++;
}
}
for (Integer i : hs){
System.out.println(i);
}
}
}
what aaut“Line 1:at char 0”? – hhh 2010-04-22 15:15:26
用“boolean start”和循环固定:boolean keepSearching = true; boolean start = true; 列表 charPositions = null; 整数charNum = 0,lastCharNum = 0; (keepSearching){ charNum = s.indexOf(“X”,lastCharNum + 1);如果(start == true && lastCharNum == 0){ charNum = s.indexOf(“X”,lastCharNum); start = false; } –
hhh
2010-04-22 16:46:50
你是对的,谢谢。我修正了代码:) – 2010-04-23 07:46:51