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.
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]);
I have a simple array that I declare below but getting incompatible types error, when trying to pass values from another class. Also, is there a better way of declaring the array below instead of saying initialize to 10 positions?
float[] myarray = new float[10];
for(StudentClass score:total){
myarray = ((float)score.getRiskValue()); //incompatible types here
}
Instead of foreach use for loop
ArrayList<StudentClass> score = new ArrayList<>();//initialize the list with data here
float[] myarray = new float[score.size()];
for(int i=0;i<score.size();i++){
myarray[i] = ((float)score.getRiskValue());
}
If I can declare an Array of FloatLists: FloatList [][] values = new FloatList[3][3];
Why doesn’t it work to declare an Array of ArrayLists holding FloatLists like this: ArrayList<FloatList> [][] values = new ArrayList<FloatList>() [3][3];? OR EVEN: ArrayList<FloatList> [][] values = new ArrayList<FloatList> [3][3]();
How can this be achieved? Will it be hard to refer to the floats buried deep under its crusty texture?
Work from the inner-most type to the outer-most type. You start with FloatList:
FloatList
Then wrap that in an ArrayList:
ArrayList<FloatList>
Then you want an array of that:
ArrayList<FloatList>[]
Or a 2D array:
ArrayList<FloatList>[][]
That gives you the type for the declaration, but then you have to initialize the variable by giving it a value. Start with the array by giving it a size:
ArrayList<FloatList>[] array = new ArrayList[10];
This gives you an array of ArrayList<FloatList> objects, but they start out as null. To give them a value, you'd loop over every index in the array and use the new keyword to set the value of the index to an instance of ArrayList<FloatList>:
for(int i = 0; i < array.length; i++){
array[i] = new ArrayList<FloatList>();
}
For a 2D array, you'd use the same logic, just in a nested for loop.
Then to add a FloatList to an ArrayList at a specific index of the array, you'd do this:
array[i].add(new FloatList());
Finally, to add a float to a FloatList in an ArrayList at an index in the array, you'd do this:
array[x].get(y).append(0.5);
And to get a float out of an index in the FloatList in an ArrayList at an index in the array, you'd do this:
float f = array[x].get(y).get(z);
Putting it all together, it looks like this:
ArrayList<FloatList>[] array = new ArrayList[10];
for(int i = 0; i < array.length; i++){
array[i] = new ArrayList<FloatList>();
}
array[1].add(new FloatList());
array[1].get(0).append(0.25);
array[1].get(0).append(0.5);
array[1].get(0).append(0.75);
float f = array[1].get(0).get(2);
println(f);
ArrayList<FloatList> [][] values = new ArrayList[3][3];
Basically, you're declaring that you want an object that is a 3D array of ArrayLists, and not generating actual ArrayList objects.
Afterwards, you have to instantiate each of them, so for example:
values[0][0] = new ArrayList<>();
And so on.
It doesn't work because of the way the JVM provides Generics. Generics in Java is a front-end compiler feature that becomes raw type usages and casts at execution time.
What is the compiler doing when I use generics?
Here's a terribly-contrived example. Let's say I want to create a List<String> to store command line arguments that I will later use to kick off a new process with, like so:
List<String> cmd = new ArrayList<>();
cmd.add("java");
cmd.add("-jar");
cmd.add("path/to/my.jar");
...
String args = cmd.get(0)+" "+cmd.get(1)+" "+cmd.get(2);
At compile time, the compiler will check to make sure that I am using the String type every time I use a generic List method via cmd and throw an error if I try to use instances of an incompatible type. However, there's a little thing called erasure that happens during compilation, before execution. Effectively, under the hood, the compiler converts the code above into something like this:
List cmd = new ArrayList();
cmd.add("java");
cmd.add("-jar");
cmd.add("path/to/my.jar");
...
String args = (String)cmd.get(0)+" "+(String)cmd.get(1)+" "+(String)cmd.get(2);
So why doesn't my generic array code compile?
In your example, you wanted to create an array of a generic type, like so:
ArrayList<FloatList>[][] array = new ArrayList<FloatList>[n][m]; //Doesn't compile? What gives?
The problem is, because of type erasure, the ArrayList<FloatList> class type doesn't really exist, and now you've asked the JVM to create a 2-dimensional array of that non-existent type.
Okay, so what's the alternative?
Well ... it isn't pretty, but you could do something like this:
class ArrayListOfFloatList extends ArrayList<FloatList>{
...
}
//Somewhere else in your code:
ArrayListOfFloatList[][] myArray = new ArrayListOfFloatList[n][m];
//This will compile because ArrayListOfFloatList is a real class.
The only other way around this would be to not use arrays. Ugly, perhaps, but it's unfortunately a limitation of how Java is currently implemented.
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);
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.