首页 > 解决方案 > 按索引排列的arraylist元素

问题描述

我正在尝试比较List<ArrayList<String>>

这是我的代码:

    List<ArrayList<String>> storeStatusAfterAssign = storeManagerPage.getMaterialStatus();
    System.out.println(storeStatusAfterAssign);

给我输出:

[[Concrete Fabric, Universal Store, Pending, , Pending], [Wooden Beam, Office Admin, , Pending, Pending], [Regular Concrete, Universal Store, Pending, , Pending], [Fiber Glass, Office Admin, , Pending, Pending]]

现在我需要比较 arraylist 元素,例如如果在列表的第一个数组中,第二个索引是 Universal Store,那么第三个索引应该是Pending如果第二个索引是 Office Admin,那么第三个索引应该是空白,第四个索引应该在每个列表中等待检查

任何人有任何建议我如何能做到这一点

谢谢

标签: javaarraylistindexingcomparison

解决方案


如果您描述的每个条件都满足每个条件,则此方法返回 trueArrayList

public boolean checkList(List<ArrayList<String>> list) {
    
    boolean returnValue = true;
    for(ArrayList<String> arrayList : list) { //loop through every ArrayList in List
         if(arrayList.get(1) == "Universal Store") { //check if second index is Universal Store
              returnValue = (arrayList.get(2) == "Pending"); //set returnValue to true only if third index is Pending
         }
         else if(arrayList.get(1) == "Office Admin") {//check if second index is Office Admin
              returnValue = ((arrayList.get(2) == "") && (arrayList.get(3) == "Pending")) //set returnValue to true only if third index is blank and fourth index is Pending
         }
         if(!returnValue) { //check for each iteration if returnValue is false, if it is return false since then one ArrayList do not fill the conditions
            return false;
         }
    }
    return true; //return true if all ArrayLists fill the conditions
}

这个 Java 程序给出了预期的输出

public class Main {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        ArrayList<String> s1 = new ArrayList<String>(Arrays.asList("Concrete Fabric","Universal Store", "Pending", "", "Pending"));
        ArrayList<String> s2 = new ArrayList<String>(Arrays.asList("Wooden Beam", "Office Admin", "", "Pending", "Pending"));
        ArrayList<String> s3 = new ArrayList<String>(Arrays.asList("Regular Concrete", "Universal Store", "Pending","", "Pending"));
        ArrayList<String> s4 = new ArrayList<String>(Arrays.asList("Fiber Glass", "Office Admin","" , "Pending", "Pending"));

        List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
        
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);

        
        System.out.println(checkList(list));
    }
    
    public static boolean checkList(List<ArrayList<String>> list) {
        
        boolean returnValue = true;
        for(ArrayList<String> arrayList : list) { //loop through every ArrayList in List
             if(arrayList.get(1) == "Universal Store") { //check if second index is Universal Store
                  returnValue = (arrayList.get(2) == "Pending"); //set returnValue to true only if third index is Pending
             }
             else if(arrayList.get(1) == "Office Admin") {//check if second index is Office Admin
                  returnValue = ((arrayList.get(2) == "") && (arrayList.get(3) == "Pending")); //set returnValue to true only if third index is blank and fourth index is Pending
             }
             if(!returnValue) { //check for each iteration if returnValue is false, if it is return false since then one ArrayList do not fill the conditions
                return false;
             }
        }
        return true; //return true if all ArrayLists fill the conditions
    }
}

推荐阅读