使用具有多个文本文件的地图来更改值
Using Map with multiple text files to change values
下面的第一段代码导入文件并将其放入二维数组中。该文件的内容如下。
BufferedReader brStudents = null;
BufferedReader brStudents2 = null;
BufferedReader reader = null;
String[][] spaces = new String[11224][4];
try
{
reader = new BufferedReader(new FileReader("FileMAIN"));
String line = null;
int row = 0;
while ((line = reader.readLine()) != null)
{
String[] arrays = line.split(",");
for (int i = 0; i < 4; i++)
{
spaces[row][i] = arrays[i];
}
row++;
}
}
FileMAIN
包含 11442 行数据,其布局如下:
John, Maths, 534, Green
Sally, Science, 3232, Red
Lilly, Science, 123123, Purple
导入数组后,下面的代码比较 value
和 column 0
和 FileONE
中的值,如果在 FileONE
中找不到,则进行搜索在 FileTWO
。如果仍未找到,则显示丢失。
下面是 FileONE
和 FileTWO
的例子
try
{
Map<String, String> firstFile = getMapFromCSV("FileONE");
Map<String, String> secondFile = getMapFromCSV("FileTWO");
int index = 2;
for(String[] row : spaces) {
String valueToFind = row[index];
if(firstFile.containsKey(valueToFind)) {
row[index] = firstFile.get(valueToFind);
} else if(secondFile.containsKey(valueToFind)) {
row[index] = secondFile.get(valueToFind);
} else {
System.out.println("Missing " + valueToFind);
}
}
FileONE 或 FileTWO 示例
123123, UJDJ7D
3232, YHD2H3
534, DSJI3HJ
最后,我不确定我如何不仅可以声明它丢失了,还可以将值更改为第二列中的值。
例如,spaces[][]
中的值来自:
John, Maths, 534, Green
Sally, Science, 3232, Red
Lilly, Science, 123123, Purple
收件人:
John, Maths, DSJI3HJ, Green
Sally, Science, YHD2H3, Red
Lilly, Science, UJDJ7D, Purple
并且仍然显示失踪人员。
这就是我实现 for
循环的方式,我跳过了 contains
的使用,直接执行 get
并自己验证结果。如果 get
调用 returns 一个值,我将其分配给当前的 row
for (String[] row : spaces) {
String valueToFind = row[index];
String value = firstFile.get(valueToFind);
if (value == nil) {
value = secondFile.get(valueToFind);
}
if (value != nil) {
row[index] = value;
} else {
System.out.println("Missing " + valueToFind);
}
}
我不知道这段代码的上下文,但我仍然建议您创建一个简单的 POJO class 并跳过那个多维数组。它使代码更具可读性,并且在我看来更容易编码
class Example {
private String name;
private String course;
private String courseId;
private String color;
//constructor, get/set methods ..
}
下面的第一段代码导入文件并将其放入二维数组中。该文件的内容如下。
BufferedReader brStudents = null;
BufferedReader brStudents2 = null;
BufferedReader reader = null;
String[][] spaces = new String[11224][4];
try
{
reader = new BufferedReader(new FileReader("FileMAIN"));
String line = null;
int row = 0;
while ((line = reader.readLine()) != null)
{
String[] arrays = line.split(",");
for (int i = 0; i < 4; i++)
{
spaces[row][i] = arrays[i];
}
row++;
}
}
FileMAIN
包含 11442 行数据,其布局如下:
John, Maths, 534, Green
Sally, Science, 3232, Red
Lilly, Science, 123123, Purple
导入数组后,下面的代码比较 value
和 column 0
和 FileONE
中的值,如果在 FileONE
中找不到,则进行搜索在 FileTWO
。如果仍未找到,则显示丢失。
下面是 FileONE
和 FileTWO
的例子
try
{
Map<String, String> firstFile = getMapFromCSV("FileONE");
Map<String, String> secondFile = getMapFromCSV("FileTWO");
int index = 2;
for(String[] row : spaces) {
String valueToFind = row[index];
if(firstFile.containsKey(valueToFind)) {
row[index] = firstFile.get(valueToFind);
} else if(secondFile.containsKey(valueToFind)) {
row[index] = secondFile.get(valueToFind);
} else {
System.out.println("Missing " + valueToFind);
}
}
FileONE 或 FileTWO 示例
123123, UJDJ7D
3232, YHD2H3
534, DSJI3HJ
最后,我不确定我如何不仅可以声明它丢失了,还可以将值更改为第二列中的值。
例如,spaces[][]
中的值来自:
John, Maths, 534, Green
Sally, Science, 3232, Red
Lilly, Science, 123123, Purple
收件人:
John, Maths, DSJI3HJ, Green
Sally, Science, YHD2H3, Red
Lilly, Science, UJDJ7D, Purple
并且仍然显示失踪人员。
这就是我实现 for
循环的方式,我跳过了 contains
的使用,直接执行 get
并自己验证结果。如果 get
调用 returns 一个值,我将其分配给当前的 row
for (String[] row : spaces) {
String valueToFind = row[index];
String value = firstFile.get(valueToFind);
if (value == nil) {
value = secondFile.get(valueToFind);
}
if (value != nil) {
row[index] = value;
} else {
System.out.println("Missing " + valueToFind);
}
}
我不知道这段代码的上下文,但我仍然建议您创建一个简单的 POJO class 并跳过那个多维数组。它使代码更具可读性,并且在我看来更容易编码
class Example {
private String name;
private String course;
private String courseId;
private String color;
//constructor, get/set methods ..
}