[英]ArrayList: Getting a String element within an String element?
I have an ArrayList here that contains X number of String elements that also contain their own X number of String elements. 我这里有一个ArrayList,它包含X个String元素,这些元素也包含自己的X个String元素。 As such:
因此:
[ [ c, in, h, i, t , n , at, Th, t, th, ha, e , he, ca],
[ c, t , m, sa, o, n , at, s, Th, t, th, ma, e , he, ca, on],
[ a, b, in, i, bl, gs, s , an, et, n , la, Pi, ke, nk, ig, a ] ]
I need to get a specific element within the element. 我需要在元素中获取特定元素。 eg List at
[0][0]
would be c
, [0][1]
would be in
and etc... List.get()
doesn't appear to be working for me. 例如,
[0][0]
处的列表将是c
, [0][1]
将in
等等... List.get()
似乎不适合我。 I'm not sure what is the correct way to do that. 我不确定这样做的正确方法是什么。 I saw in another example using
List.get(0)[0]
but its not working for me. 我在另一个例子中看到了使用
List.get(0)[0]
但它不适用于我。
Eclipse is saying "The type of the expression must be an array type but it resolved to String"
when I try List.get(0)[0]
. 当我尝试
List.get(0)[0]
时,Eclipse会说"The type of the expression must be an array type but it resolved to String"
。 So I tried List.toArray()
which didn't help. 所以我尝试了
List.toArray()
没有帮助。
EDIT 编辑
HashSet<String> shingleTrimSet = new HashSet<String>();
ArrayList<String> shingleArrayList = new ArrayList<String>();
System.out.println("\nSorted Shingles:");
for(int i = 0; i < lineCount; i++){
shingleTrimSet.clear();
for(int idx = 0, jdx = 1; idx+1 < lines[i].length(); idx++, jdx++){
shingleTrimSet.add( lines[i].substring( idx, jdx+1 ) );
}
shingleArrayList.add(i, shingleTrimSet.toString() );
}
System.out.println( shingleArrayList.get(0).get(0) );
Right now I'm just trying to get a specific element in the print line. 现在我只想尝试在打印行中获取特定元素。 I'm planning to write a for loop that will look for matching substrings from one element to another.
我打算编写一个for循环,它将寻找从一个元素到另一个元素的匹配子串。 I'm doing Jaccard Similarity.
我正在做Jaccard相似性。
如果你的意思是你有一个字符串List<List<String>> test
那么test.get(0).get(0)
将返回第一个子列表中的第一个字符串。
From what you've said, it sounds like you want to split your strings. 从你所说的,听起来你想分裂你的字符串。 Given an initialized variable strings declared as:
给定初始化的变量字符串声明为:
ArrayList<String> strings;
the following code will likely suit your needs: 以下代码可能适合您的需求:
for (String s: strings) {
String[] parts = s.split(",");
String part2 = parts[1].trim(); // "in" for your first row
}
Or maybe you wanted to gather the split fields into a list of string arrays: 或者您可能希望将拆分字段收集到字符串数组列表中:
ArrayList<String[]> rows = new ArrayList<String[]>();
for (String s: strings) {
String[] fields = s.split(",");
for (int i = 0; i < fields.length; i++)
fields[i] = fields[i].trim();
rows.add(fields);
}
which you can now access as you originally intended: 你现在可以按照原定的目的访问它:
String row1field2 = rows.get(0)[1]; // "in"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.