简体   繁体   English

如何使用java中另一个列表中的列表获取列表元素

[英]How to get the list elements using list inside another list in java

I have a list like this List contains set of dtime,uptime values.I want to get the list items ie, dtime into one and uptime into another variable.Likewise I want to get all the dtime and uptime pair values seperatly into the variables using for loop in java.How can I achieve this.Is it possible list or vector?Please help me. 我有一个像这样的列表列表包含一组dtime,正常运行时间值。我想得到列表项,即dtime为一个和正常运行时间到另一个变量。我希望得到所有的dtime和正常运行时间对值分离到变量使用for循环在java.How我可以实现这个。它可能列表或向量吗?请帮助我。 Pseudo code 伪代码

List.get(0).get(0)-->gives 1st dtime
List.get(0).get(1)-->gives 1st uptime

List.get(1).get(0)-->gives 2nd dtime
List.get(1).get(1)-->gives 2nd uptime

And so on.. 等等..

How to implement this with for loop I am not getting.I am new to java>please help me.. 如何用for循环实现这个我没有得到。我是java的新手>请帮帮我..

First Convert That ArrayList into Object[] array then get the value like given below code...driver_ModelsObj is an array convert that into drives object array then get the value from inside the array. 首先将ArrayList转换为Object []数组然后获取类似下面代码的值... driver_ModelsObj是一个数组转换成驱动器对象数组然后从数组内部获取值。

for(int indx=0;indx<driver_ModelsObj.size();indx++){
                Object[] drivers=(Object[]) driver_ModelsObj.get(indx);
                String Device_ID=drivers[0].toString();
    }

You can try this ,Let say you have variables like 你可以尝试这个,假设你有变量

            double dtime;     
            Timestamp tp;

And listofresults is coming from query results. listofresults来自查询结果。

listofresults = results.getResultList();

If list is coming from query then put it in the loop this way in the condition of for loop 如果list来自查询,那么在for循环的条件下将它放入循环中

    for(int i=0;i< listofresults.size() ;i=i+2)
    {
        dtime=(double) listofresults.get(i);       
                                   //cast because the value is of object type
        tp=(TimeStamp) listofresults.get(i+1);
                                 //cast because the value is of object type
        //do something with these variables 

    }

Sounds like you could use a domain object containing uptime and downtime . 听起来你可以使用包含uptimedowntime的域对象。

For example, 例如,

public class Stats {
    int dtime;
    int uptime;
}

Then you can have a List<Stats> and access it like this: 然后你可以有一个List<Stats>并像这样访问它:

mylist.get(0).dtime
mylist.get(0).uptime
mylist.get(1).dtime
mylist.get(1).uptime

Part of the (newer) Collcetions framework, List is almost always a better alternative than Vector 作为(较新的)Collcetions框架的一部分, List几乎总是比Vector更好的选择

List.get(0).get(0)-->gives 1st dtime
List.get(0).get(1)-->gives 1st uptime

Well, what you're doing here, is getting the list at position 0 , and getting item 1 from that list. 那么,你在这里做的是将列表放在第0 ,并从该列表中获取第1项。 In a for loop we can express this as: for loop我们可以将其表达为:

for(int x = 0; x < List.size(); x++)
{
    for(int y = 0; y < List.get(x).size(); y++)
    {
         if(y % 2 == 0)
         {
             // It's a dtime object.
         }
         else
         {
             // It's an uptime object.
         }
    }
}

Before this, you could declare some lists of your own: 在此之前,您可以声明一些自己的列表:

List<DTime> listD = new ArrayList<ATimeObject>();
List<UpTime> listUp = new ArrayList<UpTime>();

Now when you're cycling through, all you need to do is add the relevant object: 现在,当您骑车时,您需要做的就是添加相关对象:

if(y % 2 == 0)
{
    listD.add(List.get(x).get(y));
}
else
{
    listUp.add(List.get(x).get(y));
}

You should create a POJO like 你应该创建一个POJO

public class TimeData {
    double dtime;
    Date uptime;
}

Then add each POJO to array list and then iterate it. 然后将每个POJO添加到数组列表中,然后迭代它。

        List<TimeData> oList = new ArrayList<TimeData>();
        int nSize = oList.size();
        for(int i=0;i<nSize;i++){
          TimeData child = oList.get(i);
          // get value using getters

        }

I recommend creating a wrapper for it. 我建议为它创建一个包装器。

public class UpTimeDownTime {
  MyTimeDataClass downtime;
  MyTimeDataClass uptime;

  public UpTimeDownTime(MyTimeDataClass downtime, MyTimeDataClass uptime){
    this.downtime = downtime;
    this.uptime = uptime;
  }

  public MyTimeDataClass getDowntime () {
        return downtime;
    }

  public MyTimeDataClass getUptime () {
        return uptime;
    }

  public static void main (String[] args) {
    List<List<MyTimeDataClass>> List = ...;
    List<UpTimeDownTime> uptimeDowntime = new ArrayList<UpTimeDownTime>();
    for(List<MyTimeDataClass> timeList : List){
        UpTimeDownTime u = new UpTimeDownTime(timeList.get(0), timeList.get(1));
        uptimeDowntime.add(u);
    }
 }

}

If your list is as below 如果您的清单如下

List list = [[1],[2],[3]];

We can retrieve the each value as below. 我们可以检索每个值,如下所示。

((List)list.get(0)).get(0); //This will retrieve value 1
((List)list.get(1)).get(0); //This will retrieve value 2 

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

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