한 문자열 배열의 단어가 다른 문자열 배열에 있는지 확인하는 방법을 알아내는 데 어려움이 있습니다. .contains은() 문자열 배열에 적용 할 수 없기 때문에 위의 코드는 현재 나에게 오류를주고있다문자열 배열에 다른 문자열 배열의 단어가 포함되어 있는지 확인하십시오.
FileInputStream fis = new FileInputStream("TranHistory.csv");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
CSVReader reader = new CSVReader(isr);
String[] groceries = new String[]{"albertsons", "costco"};
for (String[] cols; (cols = reader.readNext()) != null;) {
if(cols[4].toLowerCase().contains(groceries)){
System.out.print(cols[4]);
}
}
: 이것은 내가 지금까지있는 것입니다.
if(cols[4].toLowerCase().contains("albertsons")){
System.out.print(cols[4]);
}
내 문제는 문자열 [] 식료품 그래서 난 문자열 [] 식료품에 가장을 문자열 [] COL입니다 비교 생각 많은 식료품 점을 가지고 가고 있다는 것입니다 : 나는이에 if 문을 변경 한 경우에만 작동합니다 이 일을하는 효율적인 방법 나는 그것을 구현하는 데 어려움을 겪고있다.
는 해결책 :
나는 ... 당신은 루프 중첩을 할 필요가 그것을 알아 냈다.
String[] groceries = {"albertsons", "costco"};
for (String[] cols; (cols = reader.readNext()) != null;) {
for (int i = 0; i < groceries.length; i++){
if(cols[4].toLowerCase().contains(groceries[i]))
{
System.out.print(cols[4]);
}
}
}
Set.of – Stephan
'Set # of '가 Java 9에 도입되었습니다. –
업데이트하지 않으려면 다음과 같이'Set '을 초기화 할 수 있습니다 :'new HashSet <> (Arrays.asList ("albertsons", "costco"));' –