简体   繁体   English

Java遍历对象列表

[英]Java Iterate through list of objects

I'm a beginner in Java Programming and having hard time understand the concept of loop through list of objects in Java. 我是Java编程的初学者,很难理解Java对象列表中的循环概念。

Input data records are of the form: 输入数据记录的格式为:

    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|

In order to read the above records data in my program based on category, created of list of objects for each category as: 为了在我的程序中基于类别读取上述记录数据,为每个类别创建对象列表为:

List<Object> category1Orcategory2OrCategory3OrCategory4;

If input data is of the form, 如果输入数据的格式为

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|

accessing list of objects can be done with: 访问对象列表可以通过以下方式完成:

 Category1 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(0);
 Category2 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(1);
 Category3 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(2);
 Category4 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(3);

So, looping can be done as: 因此,循环可以通过以下方式完成:

 for(int index=0 ; index = list.size(); index++){
 object.getcategory1Orcategory2OrCategory3OrCategory4().get(index);
 }

But at times, some of the category data will be missing and input will look like: 但是有时,某些类别数据将丢失,并且输入看起来像:

case1: 
TestDataSet1 |4|01-category1|100|43|||
TestDataSet3 |4|03-category3|104331|
TestDataSet4 |5|04-category4|100|2016-07-20 21:36:00|

or 要么

case2:
TestDataSet1 |4|01-category1|100|43|||
TestDataSet3 |4|02-category2|104331|
TestDataSet4 |5|04-category4|100|2016-07-20 21:36:00|

It is understandable that in case1 the data can be accessed in : 可以理解,在case1中可以在以下位置访问数据:

 Category1 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(0);
 Category3 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(1);
 Category4 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(2);

or in case2: 或在case2中:

 Category1 - object.getcategor1Orcategory2OrCategory3OrCategory4().get(0);
 Category2 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(1);
 Category4 - object.getcategory1Orcategory2OrCategory3OrCategory4().get(2);

I'm confused on how can I make the 'for loop' generic to cover all these cases in looping through the list of objects: 我对如何使“ for循环”通用以涵盖遍历对象列表的所有这些情况感到困惑:

 for(int index=0 ; index = list.size(); index++){
 object.getcategory1Orcategory2OrCategory3OrCategory4().get(???);
 }

Your data seems to be a | 您的数据似乎是| -separated text file, with up to 8 values per row, so you create a class, eg called DataSet , with 8 fields, then create a list of those for storing the rows, ie List<DataSet> . -分隔的文本文件,每行最多8个值,因此您将创建一个类,例如DataSet ,具有8个字段,然后创建一个用于存储行的类的List<DataSet> ,即List<DataSet>

You can then loop the list using an enhanced for statement: 然后,您可以使用增强的for语句循环列表:

for (DataSet dataSet : list) {
    // now get values with calls like list.getName()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM