How to convert arraylist in java? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to convert arraylist in java? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Is it possible to convert integer arraylist in java to double arraylist. For example, I have;

ArrayList<Integer> array  = new ArrayList<Integer>();

one example of how I am using it, is;

System.out.println(array.get(2));

but in the process of printing this out, I want to covert it into double, is it possible?

Answer

If you must use Java 6/7 a simple for-loop will do the trick:

List<Integer> ints = Arrays.asList(1, 2, 3, 4);
List<Double> doubles = new ArrayList<Double>(ints.size());

for (Integer i : ints) {
    doubles.add(Double.valueOf(i));
}

Or, if you simply wish to print it just assign it or cast it to a double:

double d = ints.get(0);
We are here to answer your question about How to convert arraylist in java? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji