我的文本文件很大,我想遍历各列,同时比较上一个值和下一个值,然后将与它们关联的列标题存储在列表中,以备后用。请给我一些有关如何有效解决此问题的建议。以下是到目前为止所做的事情,尝试使用“ for循环”已经过去了!谢谢。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Projections {
public static void main(String[] args) {
String fileName= "study_panel.csv";
File file= new File(fileName);
// 2-dimensional list of strings
List<List<String>> lines = new ArrayList<>();
Scanner inputStream;
try{
inputStream = new Scanner(file);
while(inputStream.hasnext()){
String line= inputStream.next();
String[] values = line.split(",");
// Adds the currently parsed line to the 2-dimensional string list
lines.add(Arrays.asList(values));
}
//Compare specific elements in the list
String svalue = lines.get(3).get(1);
String svalue2 = lines.get(3).get(2);
if(svalue.equals(svalue2)){
System.out.println("No recombination");
//store column`s header in list
}
else{
System.out.println("Recombination");
//store column`s header in list
}
inputStream.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
// Iterate through the 2-dimensional data and store column headers
int lineNo = 0;
for(List<String> line: lines) {
int columnNo = 0;
String previousValue=None;
String newValue;
for (String value: line) {
//Compare column elements in the 2-dimensional data
if(previousValue.equals(newValue)){
System.out.println("No recombination");
//store column`s header in list
}
else{
System.out.println("Recombination");
//store column`s header in list
}
// System.out.println("Individual " + lineNo + " Site " + columnNo + ": " + value);
columnNo++;
}
lineNo++;
}
}
}
1。样本研究数据
ID,S1_577905,S1_1066894,S1_1293038,S1_1491834
ind1,A,A
ind2,B,B
ind3,A
ind4,B
ind5,H,A
ind6,-,B
ind7,H
- 样本参考数据
ID,S1_570493,S1_592115,S1_604416,S1_614892,S1_618220,S1_636801,S1_654822,S1_655362,S1_723787,S1_723892,S1_858753,S1_867194,S1_923829,S1_925667,S1_1009779,S1_1009843,S1_1010052,S1_1010123,S1_1010298,S1_1010403,S1_1029733,S1_1039046,S1_1040024,S1_1044174,S1_1044355,S1_1049540,S1_1049657,S1_1050097,S1_1050995,S1_1126726,S1_1166956,S1_1177001,S1_1185437,S1_1188610,S1_1191450,S1_1195593,S1_1195669,S1_1195782,S1_1197394,S1_1207757,S1_1207893,S1_1211271,S1_1211343,S1_1223120,S1_1223377,S1_1237046,S1_1251020,S1_1280051,S1_1280124,S1_1284151,S1_1308043,S1_1340776,S1_1341385,S1_1363675,S1_1363753,S1_1407704,S1_1410354,S1_1431655,S1_1433696,S1_1490941,S1_1507081
A,T,C,G,G
B,T
- 样本预期结果
ID,S1_1507081
ind1,G
ind2,T
ind3,G
ind4,G
ind5,-
ind6,T
ind7,-