我似乎在解決問題的邏輯時遇到了麻煩。我正在嘗試做的是有一個數組列表。每個數組包含2個字符串。迭代陣列列表並打印陣列中的每個元素
我想遍歷我的列表,然後打印數組的元素。我似乎卡在打印第一個數組(這是我的代碼所做的),儘管我陷入了邏輯。
public static void main(String[] args) {
List<List<String>> addresses = new ArrayList<List<String>>();
ArrayList<String> singleAddress1 = new ArrayList<String>();
singleAddress1.add("17 Fake Street");
singleAddress1.add("18 Fake Street");
ArrayList<String> singleAddress2 = new ArrayList<String>();
singleAddress2.add("Phoney town");
singleAddress2.add("not real town");
ArrayList<String> singleAddress3 = new ArrayList<String>();
singleAddress3.add("sillyname town");
singleAddress3.add("alsosilly town");
addresses.add(singleAddress1);
addresses.add(singleAddress2);
addresses.add(singleAddress3);
System.out.print("Original contents of al: " + addresses + "\n");
Iterator itr = addresses.iterator();
while (itr.hasNext()) {
Object element = itr.next();
System.out.print(element + "\n");
for (int i = 0; i < singleAddress1.size(); i++) {
System.out.println(singleAddress1.get(i));
}
itr.remove();
}
}
}
你gettingg有哪些誤區?我注意到的一件事是在內循環中,你不使用元素,但singleaddress1 – therealprashant
1)你有列表的列表,而不是數組列表。 2)你總是從列表'singleAddress1'中打印元素,而不是從'itr.next()'返回的列表中的元素。 – SubOptimal