英文:
How to get Identical values from 3 arrayLists in java
问题
I have 3 Array Lists as below, need to get all Lists those are with INDIVIDUAL using Java code
Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam S, 2/15/1980, DOLLARS, Cash In]
Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam S, 2/15/1980, DOLLARS, Cash In]
Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam Kin, 2/15/1980, DOLLARS, Cash In]
Array List: [08/16/2020 11:00, Y, Business, King S, 2/15/1980, DOLLARS, Cash In]
From above I need to get INDIVIDUAL mentioned lists as output in Java code
Please help
英文:
I have 3 Array Lists as below, need to get all Lists those are with INDIVIDUAL using Java code
Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam S, 2/15/1980, DOLLARS, Cash In]
Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam S, 2/15/1980, DOLLARS, Cash In]
Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam Kin, 2/15/1980, DOLLARS, Cash In]
Array List: [08/16/2020 11:00, Y, Business, King S, 2/15/1980, DOLLARS, Cash In]
From above I need to get INDIVIDUAL mentioned lists as output in Java code
Please help
答案1
得分: 1
假设`"INDIVIDUAL"`始终出现在索引2处:
```java
List<List<String>> individualLists = new ArrayList<>();
for (List<String> list: allLists) {
if (list.get(2).equals("INDIVIDUAL")) {
individualLists.add(list);
}
}
<details>
<summary>英文:</summary>
Assuming `"INDIVIDUAL"` always appears at index 2:
```java
List<List<String>> individualLists = new ArrayList<>();
for (List<String> list: allLists) {
if (list.get(2).equals("INDIVIDUAL")) {
individualLists.add(list);
}
}
答案2
得分: 0
如果`INDIVIDUAL`始终在索引2处,您可以使用以下函数:
```java
ArrayList<ArrayList<String>> filter(ArrayList<ArrayList<String>> lists){
ArrayList<ArrayList<String>> out = new ArrayList<>();
for(ArrayList<String> list : lists){
if(list.get(2).equals("INDIVIDUAL"))
out.add(list);
}
return out;
}
如果您不知道INDIVIDUAL
的索引,请使用类似以下方式的函数替换if条件:
boolean isIndividual(ArrayList<String> list){
for(String str: list){
if(str.equals("INDIVIDUAL"))
return true;
}
return false;
}
英文:
if INDIVIDUAL
is always in index 2 you can have function like:
ArrayList<ArrayList<String>> filter(ArrayList<ArrayList<String>> lists){
ArrayList<ArrayList<String>> out = new ArrayList<>();
for(ArrayList<String> list : lists){
if(list.get(2).equal("INDIVIDUAL"))
out.add(list);
}
return out;
}
if you dont know the individual
index replace the if with a function of this kind:
boolean isIndividual(ArrayList<String> list){
for(String str: list){
if(str.equal('INDIVIDUAL`) == TRUE)
return True;
}
return False;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论