[英]arraylist element comapring by index
我正在嘗試比較List<ArrayList<String>>
這是我的代碼:
List<ArrayList<String>> storeStatusAfterAssign = storeManagerPage.getMaterialStatus();
System.out.println(storeStatusAfterAssign);
給我 output:
[[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 ,那么第三個索引應該是空白,第四個索引應該在每個列表中等待檢查
任何人有任何建議我如何能做到這一點
謝謝
如果您描述的每個條件都為每個ArrayList
填充,則此方法返回 true
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 程序給出了預期的 output
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.