user2365917
user2365917

Reputation: 1185

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 i.e., 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. 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..

Upvotes: 1

Views: 14640

Answers (7)

Udara Seneviratne
Udara Seneviratne

Reputation: 2493

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 

Upvotes: 1

takeoffjava
takeoffjava

Reputation: 529

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.

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

Upvotes: 3

arynaq
arynaq

Reputation: 6870

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);
    }
 }

}

Upvotes: 0

anshulkatta
anshulkatta

Reputation: 2064

You can try this ,Let say you have variables like

            double dtime;     
            Timestamp tp;

And listofresults is coming from query results.

listofresults = results.getResultList();

If list is coming from query then put it in the loop this way in the condition of for loop

    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 

    }

Upvotes: 0

Hardik Mishra
Hardik Mishra

Reputation: 14897

You should create a POJO like

public class TimeData {
    double dtime;
    Date uptime;
}

Then add each POJO to array list and then iterate it.

        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

        }

Upvotes: 0

christopher
christopher

Reputation: 27356

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. In a for loop we can express this as:

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));
}

Upvotes: 0

vikingsteve
vikingsteve

Reputation: 40438

Sounds like you could use a domain object containing uptime and downtime.

For example,

public class Stats {
    int dtime;
    int uptime;
}

Then you can have a List<Stats> and access it like this:

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

Upvotes: 0

Related Questions