0
我是Java編程的初學者,很難理解循環遍歷Java對象列表的概念。Java遍歷對象列表
輸入數據記錄的形式爲:
TestDataSet1 |4|01-category1|100|43|||
TestDataSet2 |4|02-category2|10044307001|0|100|2016-07-20 21:36:00|
TestDataSet3 |4|03-category3|104331|
TestDataSet4 |5|04-category4|100|2016-07-20 21:36:00|
TestDataSet5 |4|01-category1|100|43|||
TestDataSet3 |4|03-category3|104331|
爲了讀取基於類別中我的程序上面的記錄數據,爲每個類別對象名單,創建爲:
List<Object> category1Orcategory2OrCategory3OrCategory4;
如果輸入數據的形式,
TestDataSet1 |4|01-category1|100|43|||
TestDataSet2 |4|02-category2|10044307001|0|100|2016-07-20 21:36:00|
TestDataSet3 |4|03-category3|104331|
TestDataSet4 |4|04-category4|100|2016-07-20 21:36:00|
訪問對象列表可以用:
Category1 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(0);
Category2 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(1);
Category3 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(2);
Category4 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(3);
因此,循環是可以做到:
for(int index=0 ; index = list.size(); index++){
object.getcategory1Orcategory2OrCategory3OrCategory4().get(index);
}
但有時,一些類別的數據將丟失和輸入的樣子:
case1:
TestDataSet1 |4|01-category1|100|43|||
TestDataSet3 |4|03-category3|104331|
TestDataSet4 |5|04-category4|100|2016-07-20 21:36:00|
或
case2:
TestDataSet1 |4|01-category1|100|43|||
TestDataSet3 |4|02-category2|104331|
TestDataSet4 |5|04-category4|100|2016-07-20 21:36:00|
可以理解的是,在case1中數據ca
Category1 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(0);
Category3 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(1);
Category4 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(2);
或案例2:在正訪問
Category1 - object.getcategor1Orcategory2OrCategory3OrCategory4().get(0);
Category2 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(1);
Category4 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(2);
我很困惑我如何可以使「for循環」通用覆蓋所有這些情況下,通過對象的列表循環:
for(int index=0 ; index = list.size(); index++){
object.getcategory1Orcategory2OrCategory3OrCategory4().get(???);
}
的問題我不清楚。你是否嘗試過重用你的第一個循環?什麼地方出了錯? –