是我的工作,到目前爲止,我已經用了2個CSV文件,並取得代表2 CSV文件中的數據,現在我所想要做的是2個不同的陣列打印相同的元素我想比較這2個陣列和打印相同vlaues或元件,其在兩個數組一樣,這是我在哪裏有困難比較在Java 2字符串數組,並從兩個陣列下面
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CSVReaderw {
public static void main(String[] args) {
String csvFile1 = "/Users/Documents/AD.csv";
String csvFile2 = "/Users/Desktop/database.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
String[] ad = null;
String[] database = null;
try {
br = new BufferedReader(new FileReader(csvFile1));
while ((line = br.readLine()) != null) {
// use comma as separator
ad = line.split(cvsSplitBy);
//System.out.println("AD [id= " + ad[2] +"]");
}
System.out.println("second file result starts here");
br = new BufferedReader(new FileReader(csvFile2));
while ((line = br.readLine()) != null) {
database = line.split(cvsSplitBy);
//System.out.println("ID =" + database[0]);
}
List<String> commonListLambda = Arrays.stream(database)
.filter(x -> Arrays.asList(ad).contains(x))
.collect(Collectors.toList());
System.out.println(commonListLambda);
// List<String> commonList = new ArrayList<>();
//
// for(int i = 0; i < ad.length; i++){
// if(Arrays.asList(databse).contains(ad[i]))
// commonList.add(ad[i]);
// }
//
// System.out.println(commonList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
編輯: 看到更新後的代碼,因爲我們dicuessed這裏
的[我如何在Java中比較字符串?]可能的複製(https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)在該問題 –
他們只是顯示如何比較字符串我想comapre數組中的值,並從數組中打印相同的值 – newlearner
'if(ad [i] == database [j])'你正在比較你得到的字符串文件。它完全一樣的解決方案。 –