[英]Comparing two arraylists and then adding matches to specific text files
I have two arraylists
of names.我有两个名字的
arraylists
。 I am trying to compare the two.我正在尝试比较两者。 If the name in the first array equals the name in the second array, I want to create a text file that is called "name j" where j is the index the matching name is found.
如果第一个数组中的名称等于第二个数组中的名称,我想创建一个名为“name j”的文本文件,其中 j 是找到匹配名称的索引。 Right now it seems to be able to find two of the matching names but not the last one.
现在它似乎能够找到两个匹配的名称,但不能找到最后一个。 I am not sure why it is not finding the last one.
我不知道为什么它没有找到最后一个。
Here is my code:这是我的代码:
ArrayList <String> names = new ArrayList ();
names.add("Stephen");
names.add("James");
names.add("Billy");
names.add("Connor");
names.add("Katie");
ArrayList <String> names2 = new ArrayList();
names2.add("Carlos");
names2.add("Katie");
names2.add("James");
names2.add("Cameron");
names.add("Blly");
int i,j;
i=0;
j=0;
int nameSize = names.size();
int names2Size = names2.size();
int checkMatches=0;
// boolean endOfList = false;
while (i<nameSize) {
if (names.get(i).equals(names2.get(j))) {
//increase i b/c of match
System.out.println("Match " + names.get(i));
infoToFile(j,i,names);
i++;
} else if (j == names2Size-1) {
System.out.println("No Match");
i++;
j=0;
} else {
System.out.println("No Match");
j++;
}
}
public static void infoToFile(int j,int i,ArrayList <String> names) {
Writer output;
try {
output = new BufferedWriter(new FileWriter("name " + j + ".txt",true));
output.append(names.get(i) + "\n");
output.close();
} catch (IOException ex) {
Logger.getLogger(RosterPractice.class.getName()).log(Level.SEVERE, null, ex);
}
}
So, you can do this by many different ways, but I will show you how I did (and test it):所以,你可以通过很多不同的方式来做到这一点,但我会告诉你我是怎么做的(并测试它):
1st you need to check which list is bigger than other, otherwise you will get a indexOutOfBoundsException after that, do a for over the smallest list, check if the string are the same if this is met, call the method to create a file and pass as parameter then name of the file to crete (I did it passing index and name, but you could do it by passing a concatenation of those too) 1st 你需要检查哪个列表比另一个更大,否则你会得到一个 indexOutOfBoundsException 之后,对最小的列表做一个 for ,如果满足这个,检查字符串是否相同,调用创建文件的方法并通过作为参数,然后将文件的名称传递给 crete(我传递了索引和名称,但您也可以通过传递它们的串联来实现)
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add("Stephen");
names.add("Sepp");
names.add("James");
names.add("Billy");
names.add("Connor");
names.add("Katie");
System.out.println(names);
List<String> names2 = new ArrayList<String>();
names2.add("Carlos");
names2.add("Sepp");
names2.add("Katie");
names2.add("James");
names2.add("Cameron");
names2.add("Blly");
names2.add("Xoce");
names2.add("Marie");
System.out.println(names2);
if (names.size() > names2.size()) {
for (int i = 0; i < names2.size(); i++) {
if (names.get(i).equals(names2.get(0))) {
createFile(i, names.get(0));
}
}
} else {
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equals(names2.get(i))) {
createFile(i, names.get(1));
}
}
}
}
private static void createFile(int i, String name) {
File f = new File(name + "_" + i);
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.