How to Cast from List<Double> to Double[] in Java

How to cast from ListDouble to double[] in Java?

High performance - every Double object wraps a single double value. If you want to store all these values into a double[] array, then you have to iterate over the collection of Double instances. A O(1) mapping is not possible, this should be the fastest you can get:

 double[] target = new double[doubles.size()];
for (int i = 0; i < target.length; i++) {
target[i] = doubles.get(i).doubleValue(); // java 1.4 style
// or:
target[i] = doubles.get(i); // java 1.5+ style (outboxing)
}

Thanks for the additional question in the comments ;) Here's the sourcecode of the fitting ArrayUtils#toPrimitive method:

public static double[] toPrimitive(Double[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_DOUBLE_ARRAY;
}
final double[] result = new double[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i].doubleValue();
}
return result;
}

(And trust me, I didn't use it for my first answer - even though it looks ... pretty similiar :-D )

By the way, the complexity of Marcelos answer is O(2n), because it iterates twice (behind the scenes): first to make a Double[] from the list, then to unwrap the double values.

How to convert from ArrayListDouble to double[]

You can use Java8 in a same way you used it for int[]

ArrayList<Double> value  = new ArrayList<Double>();
double[] arr = value.stream().mapToDouble(v -> v.doubleValue()).toArray();

OR (As per below comment of yshavit)

double[] arr = value.stream().mapToDouble(Double::doubleValue).toArray();

How to convert a Double[] to double...?

double[] unboxed = Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();

Credits to: https://stackoverflow.com/a/30117592/3635454

Convert a double array to Double ArrayList

Alas, Arrays.asList(..) doesn't work with primitives. Apache commons-lang has

Double[] doubleArray = ArrayUtils.toObject(durationValueArray);
List<Double> list = Arrays.asList(doubleArray);

How to convert double[] to Listdouble[] in Java?

public List<double[]> toListTuple(double [] array){
List<double []> ret = new ArrayList<double[]>() ;

double [] sublist = null;
for(int i = 0;i<array.length;i++){
if(sublist == null)
sublist = new double[3];
sublist[i%3] = array[i];
if(i%3==2) {
ret.add(sublist);
sublist = null;
}
}

if(sublist!=null){
ret.add(sublist); // This means some of the last elements weren't initialized
}
return ret;
}

How to cast from class type object to double type in java

the thing is your list returns the object of Ref_user_interface so you can get value by calling the getter method like

ref_jsp.get(i).getSt1_vs1_bag6_rb()

and if you want all the getter methods the try this in your for loop

for(int i=0;i<st_jsp.size()&&i<ref_jsp.size();i++){
Ref_user_interface refObj = ref_jsp.get(i);
Ref_user_interface stObj = st_jsp.get(i);
Double x = refObj.getSt1_vs1_bag6_rb();
Double y = stObj.getSt1_vs1_bag6_rb();
comparing(x,y);
x =refObj.getSt1_vs1_bag7_rb();
y = stObj.getSt1_vs1_bag7_rb();
comparing(x,y)

// and so on for all the getters method you have for this obj

public void comparing(Double x,Double y){
// write your comparing logic here and add value in map
if(x > 0 && y > 0){
if(x>y){
z.add(x-y);
}else{
z.add(y-x);
}
}else if(x>0){
z.add(x);
}else{
z.add(y);
}
}

and so on you get all the values you want and then use it according to your logic.

Cast an Arraylist of Doubles to ArrayList of Integers

You can not convert List<Double> to List<Integer> directly. Loop on each Double object and call intValue() function to get the integer part of it. For e.g. 13.3 will give 13. I hope thats what you want.

for(Double d : labels.data){
lab.add(d.intValue());
}


Related Topics



Leave a reply



Submit