0

I am writing a program in java and needed to use Buffers. I used a function which returned a list of arrays i.e. nested arrays and I would like to place these arrays into my buffers. I am presented with the following error:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.nio.DoubleBuffer

Would anyone have any ideas or solutions on how to cast an array list to a buffer?

taskinoor
  • 45,586
  • 12
  • 116
  • 142
TomMcG
  • 1
  • 6

2 Answers2

2

You can't cast an ArrayList to a DoubleBuffer. The two types are unrelated. The only thing you can really do is go through the list values one at a time and stick them in the buffer:

ArrayList<Double> values = ...;
DoubleBuffer buffer = ...;

for (Double value : values)
    buffer.put(value); // Auto-unboxing will let this Double -> double work.

You can't use DoubleBuffer.wrap(...), as nice as it looks, because it takes a double[], and an ArrayList<Double> can only readily be converted to a Double[], not a double[].

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • Thanks for the reply. If I place the returned contents of that function into an arrayList I get an error of "java.lang.OutOfMemoryError: Java heap space" which lead me to use buffers as they can hold more information. So the only way I can call this function is by using a double Buffer, however I then get a casting error. So I am unable to go through the arrayList as it does not have enough memory to contain these items. Any thoughts on what I can or should do? – TomMcG Apr 13 '15 at 11:09
  • @TomMcG The first thing I'd try is to just [increase the JVM's heap size](http://stackoverflow.com/questions/1565388/increase-heap-size-in-java); avoiding the memory issue in the first place. Failing that, your strategy becomes more complicated and nontrivial, e.g. storing parts on disk, dividing your data processing into smaller chunks, etc., really depends on your specific situation. – Jason C Apr 13 '15 at 16:13
0

I believe that you can cast it to array and then cast it to double buffer afterwards

ArrayList.ToArray()

then you can use wrap(), to cat it to the Double Buffer

eboni
  • 883
  • 2
  • 10
  • 25
  • 1
    `wrap` takes a `double[]`, which is not a `Double[]`. – Jason C Apr 13 '15 at 10:56
  • Thanks for the reply. If I place the returned contents of that function into an arrayList I get an error of "java.lang.OutOfMemoryError: Java heap space" which lead me to use buffers as they can hold more information. So the only way I can call this function is by using a double Buffer, however I then get a casting error. So I am unable to go through the arrayList as it does not have enough memory to contain these items. Any thoughts on what I can or should do? – TomMcG Apr 13 '15 at 11:47
  • When you say you do not have enough memory to return the contents of the list, are you defining a min and max java heap size? You can do this by using `java -Xms -Xmx`, or are you saying that you have already done this? – eboni Apr 13 '15 at 13:01
  • No I have not already done this , but I was aware that this can be done, but this generally only works as a short term solution and is not the best way to solve the problem, any ideas as to what else I might do, thanks :) – TomMcG Apr 13 '15 at 14:05