0

How do I get a specific element from a List ?

Double[] weightArray = {240.0, 220d, 230.0, 240.0, 250.0,220.0, 215.0, 220.0,215d,223d};
Double[] pulseArray= {72.0,74.0,75.0,76.0,72.0,78.0,62.0,78.0,76.0,73.0,73.0,79.0};
Double[] sysArray={120.0,120.0,120.0,120.0,120.0,120.0,120.0,120.0,120.0,120.0};
Double[] diaArray={80.0,80.0,80.0,80.0,80.0,80.0,80.0,80.0,80.0,80.0};

List<double[]> values = new ArrayList<double[]>();

for(int i=0; i<weightArray.length; i++){

values.add(new double[] {weightArray[0], weightArray[1], weightArray[2], weightArray[3], weightArray[4],  weightArray[5], weightArray[6], weightArray[7],weightArray[8], weightArray[9] });
values.add(new double[] {pulseArray[0],pulseArray[1],pulseArray[2],pulseArray[3],pulseArray[4],
          pulseArray[5],pulseArray[6],pulseArray[7],pulseArray[8],pulseArray[9] });
values.add(new double[] {sysArray[0],sysArray[1],sysArray[2],sysArray[3],sysArray[4],
          sysArray[5],sysArray[6],sysArray[7],sysArray[8],sysArray[9] });
values.add(new double[] {diaArray[0],diaArray[1],diaArray[2],diaArray[3],diaArray[4],
          diaArray[5],diaArray[6],diaArray[7],diaArray[8],diaArray[9] });    

}

// Does not work
//Object element =values.get(3,2);
//String s= element.toString();
//Toast.makeText(context, s.toString() , Toast.LENGTH_SHORT).show();

For example, I want to get values element 3,2, (diaArrray[1] )

Trey Balut
  • 1,355
  • 3
  • 19
  • 39
  • Do you realize that with your code, `values` ends up with 10 copies of each of the four arrays? Also, there's a lot of overhead with boxing and unboxing between `double` and `Double`; is there a reason for that? – Ted Hopp Sep 06 '11 at 17:04
  • Martin and Mike, Both answers were helpful and solved my issue. I was able to clean up some code as well. Thanks again. – Trey Balut Sep 06 '11 at 19:17

2 Answers2

5
values.get(listIndex)[arrayIndex]

should do it.

You might also try changing

Double[]

to

double[]

unless you have a compelling reason to want an array of Doubles. That will lead to auto-boxing confusion down the road.

And inside

for(int i=0; i<weightArray.length; i++){ ... }

you're not actually using i which looks suspicious to me.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

How do I get a specific element from a List ?

To get an element from a List, use the get(int) method.

This will return your primitive double array. Then pick an element out of that array.

double d = values.get(3)[2];

Be aware of the fact that you can simply add the arrays this way:

double[] weightArray = {240.0d, 220.0d, 230.0d, 240.0d, 250.0d, 220.0d, 215.0d, 220.0d, 215.0d, 223.0d};
List<double[]> values = new ArrayList<double[]>();
values.add(weightArray);
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287