I wrote below code. It get this message:
Exception in thread "main" java.lang.ClassCastException: [D cannot be cast to java.lang.Double
double[] xyz = {1, 11, 111, 2, 22, 222};
ArrayList array = new ArrayList();
array.add(xyz);
double[] vals = new double[array.size()];
vals[0] = (double) array.get(0);
vals[1] = (double) array.get(1);
vals[2] = (double) array.get(2);
I have also search and see some post on Stack Overflow, but they do not make much me sense. What should I do?
If you want to add an array of double values to an ArrayList, do this:
Double[] xyz = {...};
ArrayList<Double> array = new ArrayList<>(); // note, use a generic list
array.addAll(Arrays.asList(xyz));
A List cannot store primitives, so you must store Double values not double. If you have an existing double[] variable, you can use ArrayUtils.toObject() to convert it.
Actually your problem is that you are trying to cast the type of 'xyz', which does not seems to be a double or the Wrapper associed (Double), into a double.
Since java can't transform the type of 'xyz' into a double a ClassCastException is throw. You should try to add n double to your array like that (or even in a loop) :
ArrayList<Double> myListOfDouble = new ArrayList();
myListOfDouble.add(1.0);
And then using a for loop to populate your double[] vals like this :
for(int i = 0; i < myListOfDouble.size(); i++)
vals[i] = myListOfDouble.get(i);
Related
I have a function in a jar file which accepts two parameters:
An Object array which mentions the type and number of output
An Object array which contains input to the function.
The input I need to pass consists of 7 objects. I have to send two double[] arrays and rest all 5 are double values.
I would like to know how to convert double[] into an Object.
Please help me with this.
If i want to convert a single double value to an object of Double, I can use the following code.
public class JavadoubleToDoubleExample {
public static void main(String[] args) {
double d = 10.56;
/*
Use Double constructor to convert double primitive type to a Double object.
*/
Double dObj = new Double(d);
System.out.println(dObj);
}
}
Output:
10.56
But what if the d={1.0,8.9,4.0,7.9}.
How can I convert the array into an object of Double?
Arrays are already Objects in Java. If you want to convert an array of primitive double into an array of Objects:
double[] nums = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
Object[] objs = new Object[nums.length];
for(int i=0; i<nums.length; i++)
objs[i] = new Double(nums[i]);
First off, I want to say that I know this has been asked before at the following location (among others), but I have not had any success with the answers there:
Create ArrayList from array
What I am trying to do is the following:
double[] FFTMagnitudeArray = processAudio.processFFT(audioData);
List<Double> FFTMagnitudeList = Arrays.asList(FFTMagnitudeArray);
audioData.setProperty("FFTMagnitudeList", FFTMagnitudeList);
However, I get the error:
"Type mismatch: cannot convert from List<double[]> to List<Double>"
This makes no sense to me, as I thought the List was necessary and the Array.asList(double[]) would return a list of Double, not double[]. I have also tried the following, to no avail:
List<Double> FFTMagnitudeList = new ArrayList<Double>();
FFTMagnitudeList.addAll(Arrays.asList(FFTMagnitudeArray));
List<Double> FFTMagnitudeList = new ArrayList<Double>(Arrays.asList(FFTMagnitudeArray));
And I keep getting the same error.
So how do I create the List?
Change your method to return the object wrapper array type.
Double[] FFTMagnitudeArray = processAudio.processFFT(audioData);
List<Double> FFTMagnitudeList = Arrays.asList(FFTMagnitudeArray);
Or you'll have to manually copy from the primitive to the wrapper type (for the List).
double[] FFTMagnitudeArray = processAudio.processFFT(audioData);
List<Double> FFTMagnitudeList = new ArrayList<>(FFTMagnitudeArray.length);
for (double val : FFTMagnitudeArray) {
FFTMagnitudeList.add(val);
}
The double type is a primitive type and not an object. Arrays.asList expects an array of objects. When you pass the array of double elements to the method, and since arrays are considered as objects, the method would read the argument as an array of the double[] object type.
You can have the array element set the Double wrapper type.
Double[] FFTMagnitudeArray = processAudio.processFFT(audioData);
Using Java 8:
List<Double> FFTMagnitudeList = Arrays.stream(FFTMagnitudeArray).mapToObj(Double::valueOf).collect(Collectors.toCollection(ArrayList::new));
This creates a DoubleStream (a stream of the primitive type double) out of the array, uses a mapping which converts each double to Double (using Double.valueOf()), and then collects the resulting stream of Double into an ArrayList.
I would like to be able to declare a java array of double in matlab. In java you would do something like this:
double[] arr = new double[4];
Now I want to do the same thing in Matlab. I have tried the following:
arr = javaArray('D', 100);
arr = javaArray('[D', 100);
It gives me the No class [D can be located on the java class path error.
I know I can create an array of Double with arr = javaArray('com.lang.Double', 100);, but this is not a primitive type and would require further conversion.
I am getting the following error when executing the code below...
I am trying to convert the list of Object arrays to a BigDecimal array using the toArray()
method of the List collection..
When I simply give fileFormatObj.toArray() it is working fine, but when I give like in the code below I am getting the error....
public static void main(String[] args)
{
List<BigDecimal> objList = new ArrayList<BigDecimal>();
List<Object[]> fileFormatObj = new ArrayList<Object[]>();
final Object[] array1 = new Object[1];
array1[0] = new BigDecimal(BigInteger.ONE);
fileFormatObj.add(array1);
if (fileFormatObj != null)
{
//Error here java.lang.System.arraycopy
final BigDecimal[] arr = fileFormatObj
.toArray(new BigDecimal[fileFormatObj.size()]);
objList.addAll(Arrays.asList(arr));
for (final BigDecimal object : arr) {
System.out.println("TEST-->" + object.intValue());
}
}
}
The problem is that the element in fileFormatObj is an Object[], not a BigDecimal. Therefore you can't convert fileFormatObj to a BigDecimal[]. You can't even convert it to a BigDecimal[][] (as an Object[] isn't a BigDecimal[]) but at the moment you're trying to store an array reference in a BigDecimal[]...
It's not clear why you're trying to do it like this, but that's simply not going to work. If you edit your question to explain why you're trying to do this - what the bigger picture is - we can help you more.
If you absolutely have to convert a List<Object[]> to a BigDecimal[] and you want to take the first element of each array, you could use something like:
BigDecimal[] arr = new BigDecimal[fileFormatObj.size()];
for (int i = 0; i < arr.length; i++) {
Object[] fileFormatArray = fileFormatObj.get(i);
Object firstElement = fileFormatArray[0];
arr[i] = (BigDecimal) firstElement;
}
You can do the whole for loop body in a single statement of course - I only split it up here to make it clear exactly where the cast to BigDecimal needs to happen.
I am from a .Net background and do not understand the following snip. Can someone explain the <> and the following code to me as I just dont seem to get it. Sorry for dumb questions but this one I have been trying to understand all evening.
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < 3; i++) {
x.add(new double[] { 1, 2, 3, 4, 5, 6 });
}
They're the equivalent of C# generics. It's creating a list of double arrays, then adding [1,2,3,4,5,6] to it three times.
If you create a List<T> you can add instance of T to the list. In this case, T is double[].
In the Java programming language arrays are objects and may be assigned to variables of type java.lang.Object. Your code can also be written this way
Object numbers =new double[] { 1, 2,
3, 4, 5, 6 };
Your code
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < 3; i++) {
x.add(numbers);
}
Another variation: Here I created "x" as a List that can contain Object types. Since, arrays are subclasses of Object in Java, I can store the arrays in this list "x"
List<Object> x=new ArrayList<Object>();
for (int i = 0; i < 3; i++) {
x.add(numbers);
}
For a list, the type parameter in the <>'s indicates what type of objects should be stored in that list. List<double []> creates a list that stores arrays of doubles.
List<double []> myList = new ArrayList<Double>();
myList.add(new double [] {1,2,3});
myList.add(new double [] {4,5,6});
Would add two double arrays to myList. So: myList.get(0) would return: {1,2,3}
and myList.get(1) would return: {4,5,6}.
If you are trying to just create a list of doubles, and not a list of double arrays, you would do:
List<Double> myList = new ArrayList<Double>();
myList.add(1);
myList.add(2);
myList.add(3);
Now myList.get(0) will return 1 and myList.get(1) will return 2. Notice that to create a list of a primitive type, you need to specify the object version of that primitive type in the type parameter. I.e., you can't do: List<double>
This is because all type parameters just get converted to Object by the compiler.