如何从Java的3个ArrayList中获取相同的值

huangapple go评论69阅读模式
英文:

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 `&quot;INDIVIDUAL&quot;` always appears at index 2:

```java
List&lt;List&lt;String&gt;&gt; individualLists = new ArrayList&lt;&gt;();
for (List&lt;String&gt; list: allLists) {
    if (list.get(2).equals(&quot;INDIVIDUAL&quot;)) {
        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&lt;ArrayList&lt;String&gt;&gt; filter(ArrayList&lt;ArrayList&lt;String&gt;&gt; lists){
    ArrayList&lt;ArrayList&lt;String&gt;&gt; out = new ArrayList&lt;&gt;();
    for(ArrayList&lt;String&gt; list : lists){
        if(list.get(2).equal(&quot;INDIVIDUAL&quot;))
            out.add(list);
    }
    return out;
}

if you dont know the individual index replace the if with a function of this kind:

boolean isIndividual(ArrayList&lt;String&gt; list){
    for(String str: list){
        if(str.equal(&#39;INDIVIDUAL`) == TRUE)
            return True;
    }
    return False;
} 

huangapple
  • 本文由 发表于 2020年8月31日 23:18:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63673537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定