除了 contains 方法,还有什么其他方法可以将 arraylist 与字符串文本文件进行比较?
Beside contains method, what are other alternatives to compare an arraylist with string text file?
我有一个 ArrayList
用于所有 keywords/reserved 个单词,但是当打印输出时它会打印重复的关键字,我知道我可以使用 HashSet
来消除重复的事实(到目前为止,我已经尝试了所有 none 个方法)。但我认为 contains
方法负责打印重复项。
对 contains
方法的替代方案有什么建议吗?
顺便说一句,我正在创建一个词法分析器,这是示例输入和输出
数组列表:
ArrayList<String> keywords = new ArrayList<>(Arrays.asList("auto","case","char","const","continue","default",
"do","double","else","break","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while","then","int","endif"));
显示代码:
System.out.print("Keywords:");
for(int i =0; i<testarray.size(); i++){
String gg = testarray.toString().replace(",","").replace("[","").replace("]","");
String[] key = gg.trim().split(" ");
String gg1 = key[i];
if(keywords.contains(gg1)){
System.out.println(gg1+" ");
}
}
输入:
{
int a[3],t1,t2;
t1= 2 ; a[0]= 1; a[1]= 2; a[t1]= 3;
t2= - (a[2] + t1 * 6 ) / (a[2] - t1);
if t2 > 5
then
print ( t2 );
else {
int t3 ; t3 = 99 ; t2 = -25 ;
print(- t1 +t2 * t3);
} endif
}
输出:
Keywords:int if then else int endif
正在读取文件:
while((line = bufferedReader.readLine()) != null) {
test = line.split(" ");
testarray.addAll(Arrays.asList(test));
}
感觉你在白白做复杂的事情:
数组列表:
ArrayList<String> keywords = Arrays.asList("auto","case","char","const","continue","default",
"do","double","else","break","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while","then","int","endif");
显示代码:
Set<String> keys = new HashSet<>();
for (String key : testarray) {
if(keywords.contains(key)) {
keys.add(key);
}
}
System.out.print("Keywords:");
for (String key : keys) {
System.out.println(key + " ");
}
我有一个 ArrayList
用于所有 keywords/reserved 个单词,但是当打印输出时它会打印重复的关键字,我知道我可以使用 HashSet
来消除重复的事实(到目前为止,我已经尝试了所有 none 个方法)。但我认为 contains
方法负责打印重复项。
对 contains
方法的替代方案有什么建议吗?
顺便说一句,我正在创建一个词法分析器,这是示例输入和输出
数组列表:
ArrayList<String> keywords = new ArrayList<>(Arrays.asList("auto","case","char","const","continue","default",
"do","double","else","break","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while","then","int","endif"));
显示代码:
System.out.print("Keywords:");
for(int i =0; i<testarray.size(); i++){
String gg = testarray.toString().replace(",","").replace("[","").replace("]","");
String[] key = gg.trim().split(" ");
String gg1 = key[i];
if(keywords.contains(gg1)){
System.out.println(gg1+" ");
}
}
输入:
{
int a[3],t1,t2;
t1= 2 ; a[0]= 1; a[1]= 2; a[t1]= 3;
t2= - (a[2] + t1 * 6 ) / (a[2] - t1);
if t2 > 5
then
print ( t2 );
else {
int t3 ; t3 = 99 ; t2 = -25 ;
print(- t1 +t2 * t3);
} endif
}
输出: Keywords:int if then else int endif
正在读取文件:
while((line = bufferedReader.readLine()) != null) {
test = line.split(" ");
testarray.addAll(Arrays.asList(test));
}
感觉你在白白做复杂的事情:
数组列表:
ArrayList<String> keywords = Arrays.asList("auto","case","char","const","continue","default",
"do","double","else","break","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while","then","int","endif");
显示代码:
Set<String> keys = new HashSet<>();
for (String key : testarray) {
if(keywords.contains(key)) {
keys.add(key);
}
}
System.out.print("Keywords:");
for (String key : keys) {
System.out.println(key + " ");
}