java - 按索引排列的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,那么第三个索引应该是空白,第四个索引应该在每个列表中等待检查
任何人有任何建议我如何能做到这一点
谢谢
解决方案
如果您描述的每个条件都满足每个条件,则此方法返回 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
}
}
推荐阅读
- flutter - Flutter 复选框状态变化
- python - 在 Python 中导入的困惑
- amp-html - 如何修复在 vách ngăn vệ sinh 不从 CDN 加载 JS
- java - 使用导航图的 Onbackpress
- python - Django crate 自定义可重用应用程序,其中包括对 manage.py 的一些更改
- python - 在 pytest 中捕获标准输出中断
- python - 如何将 on_release 操作添加到 kivy 或 kivymd、python 中的列表项?
- javascript - 第一次编译反应应用程序的问题
- azure - 在 azure 管道输出中显示进度
- matrix - REDCap上的排名问题