我有一个二维字符串数组,如下所示:
第一列包含许多字符串的字符,其他列是字符的额外数据。
我想在此数组中搜索一个字符串(可能更改为数组字符)以获取所有匹配索引(开始-结束)。例如,当我用键“ next ”搜索时,结果应为[5-8],[13-16](上图中的高亮部分)。
不久,我需要一个如下所示的方法:
public static List<Interval> search(String searchText, String[][] data, int columnsCount, int rowCount){
// Convert search text to String array
String[] searchArr = getStringArray(searchText);
// then search in data
}
// where Interval is:
public class Interval{
public int start;
public int end;
}
有什么快速的搜索方法,因为我的数据很大吗?
提前致谢!
我建议将改编String[][]
成CharSequence
。然后,您可以自由地使用a进行所有操作,CharSequence
这也意味着您可以使用它java.util.regex.Matcher
来搜索字符串,而无需实现自己的搜索算法。
例如:
public class Main {
public static void main(String[] args) {
String[][] array2d = createArray();
int charSeqColumn = 0;
CharSequence charSequnce = new Array2DColumnCharSequnce(array2d, charSeqColumn);
System.out.println(charSequnce.toString());
Pattern patttern = Pattern.compile("ext");
Matcher matcher = patttern.matcher(charSequnce);
while (matcher.find()) {
String matchGroup = matcher.group();
int start = matcher.start();
int end = matcher.end() - 1;
String msg = MessageFormat.format("{0} matched at: [{1}] - [{2}]", matchGroup, start, end);
System.out.println(msg);
}
}
private static String[][] createArray() {
String[][] array2d = new String[2][10];
array2d[0][0] = "N";
array2d[0][1] = "e";
array2d[0][2] = "x";
array2d[0][3] = "t";
array2d[0][4] = " ";
array2d[0][5] = "N";
array2d[0][6] = "e";
array2d[0][7] = "x";
array2d[0][8] = "t";
array2d[0][9] = " ";
array2d[1][0] = "H";
array2d[1][1] = "e";
array2d[1][2] = "l";
array2d[1][3] = "l";
array2d[1][4] = "o";
array2d[1][5] = "W";
array2d[1][6] = "o";
array2d[1][7] = "r";
array2d[1][8] = "l";
array2d[1][9] = "d";
return array2d;
}
}
将输出
Next Next
ext matched at: [1] - [3]
ext matched at: [6] - [8]
我会CharSequence
像这样实现适应
class Array2DColumnCharSequnce implements CharSequence {
private int column;
private String[][] array2d;
private int endIndex;
private int startIndex;
public Array2DColumnCharSequnce(String[][] array2d, int column) {
this(array2d, column, 0, array2d[column].length);
this.array2d = array2d;
this.column = column;
}
public Array2DColumnCharSequnce(String[][] array2d, int column,
int startIndex, int endIndex) {
this.array2d = array2d;
this.column = column;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
public int length() {
return endIndex - startIndex;
}
public char charAt(int index) {
String charString = array2d[column][startIndex + index];
return charString.charAt(0);
}
public CharSequence subSequence(int start, int end) {
Array2DColumnCharSequnce array2dColumnCharSequnce = new Array2DColumnCharSequnce(
array2d, column, start, end);
return array2dColumnCharSequnce;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(this);
return sb.toString();
}
}
注意:这Array2DColumnCharSequnce
只是一个快速实现,它还没有解决异常处理问题,也没有解决当字符串列中有多个char时发生的情况。
为什么要使用CharSequence
装饰器
使数组适应CharSequence
其他方法的不同之处在于,您使用标准的Java接口,该接口可与许多其他类一起重复使用,因此非常灵活。
一些经常使用的标准Java类,它们将aCharSequence
作为参数
String.contains(CharSequence s)
String.contentEquals(CharSequence cs)
String.replace(CharSequence target, CharSequence replacement)
Appendable.append(CharSequence csq)
StringBuffer.append(CharSequence s)
StringBuilder.append(CharSequence s)
在这里查看完整列表。
使用上面的代码并尝试执行此操作,以查看装饰器的柔韧性。
public static void main(String[] args) {
String[][] array2d = createArray();
CharSequence charSequnce = new Array2DColumnCharSequnce(array2d, 0);
boolean contentEquals = "Next Next ".contentEquals(charSequnce);
System.out.println(contentEquals);
CharSequence column1CharSequnce = new Array2DColumnCharSequnce(array2d, 1);
String replaced = "I want to say Next Next ".replace(charSequnce, column1CharSequnce);
System.out.println(replaced);
}
将输出
true
I want to say HelloWorld
最后,每个人都必须决定他/她想要什么以及适合什么情况。如果我可以免费“几乎”获得它们,我更喜欢可以给我更多选择的实现。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句