I'm trying to initialize an ArrayListto use later in my code, but it seems like it doesn't accept doubles.
public ArrayList<double> list = new ArrayList<double>();
It gives an error under 'double', sayin "Syntax error on token "double", Dimensions expected after this token"
An ArrayList doesn't take raw data types (ie, double). Use the wrapper class (ie, Double) instead :
public ArrayList<Double> list = new ArrayList<>();
Also, as of Java 7, no need to specify that it's for the Double class, it will figure it out automatically, so you can just specify <> for the ArrayList.
You need to use the Wrapper class for double which is Double. Try
public ArrayList<Double> list = new ArrayList<Double>();
In Java, ArrayLists (and other generic classes) only accept object references as types, not primitive data types. There are wrapper classes that allow you to emulate using primitives, though: Boolean, Byte, Short, Character, Integer, Long, Float and Double;
public ArrayList<Double> list = new ArrayList<Double>();
//or "public ArrayList<Double> list = new ArrayList<>();" in Java 1.7 and beyond
Values inside are "autoboxed" and "autounboxed" so you can treat doubles as Doubles without problems, and vice versa. You may need to explicitly specify whether you want arguments to be treated as int or Integer when dealing with lists of integral types, though, to disambiguate between cases like remove(int index) and remove(Object o).
public ArrayList<Double> doubleList = new ArrayList<>();
From Java 1.7, you don't need to write Double while initializing ArrayList, so it's your choice to write Double in new ArrayList<>(); or new ArrayList<Double>(); or not.. otherwise it's not compulsory.
Also known as a dynamic array.
No need to pre-determine the number of elements up
front, just add to the array as we need it
Related
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.
This question already has answers here:
Create a List of primitive int?
(12 answers)
Closed 8 years ago.
In my Java program, I want to store a list of primitive values. I could do something like this:
int x = 0;
double timestamp = 123.1d;
List<Object> data = new ArrayList<Object>();
data.add(x);
data.add(timestamp);
But then the problem is that I do not know exactly what kind of objects i store in the list. So is there any better way to do that?
Well, Double, Integer, Long all belong to the Number-class. So a
List<Number>
would probably fit. It is exactly what it is - a List of numbers of unspecified subtype. Because of autoboxing you should be able to just add the primitives, but the better practice would be to use the Wrapper-classes.
The Number-class offers methods to get the different representations of the Number, for example doubleValue(). So you could convert all the values in the List<Number> to (as an example) Doubles by using this method. For more reference see the Oracle documentation for Number.
You could use List<Object> and add any kind of object to it.While retrieving the Object back from List<Object> you can get class of object by list.get(0).getClass() or you could check for list.get(i) instacne of Double.
use wrapper class and use instance of operator to check datatype of value.
like
Integer x = 0;
Double timestamp = 123.1d;
List<Object> data = new ArrayList<Object>();
data.add(x);
data.add(timestamp);
if(data.get(0) instance of Integer)
{
...
}
else if(data.get(0) instance of Double )
{
..
}
I am trying to convert an ArrayList to ArrayList. I am having actually a list of labels in Double and I want to create a list of Integers. I am trying to add the one to another but of course I need a casting process.
ArrayList<Integer> lab = new ArrayList<Integer>();
lab.addAll(labels.data); //labels.data is an Arraylist of Doubles.
How can I cast one list to another??
I ve tried this to add one by one:
ArrayList<Integer> lab = new ArrayList<Integer>();
for (int i = 0; i < labels.data.size(); i++) {
lab.set(i, labels.data.get(i).intValue());
}
But I received outOfBoundsError.
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());
}
First, is there any need for this to be an ArrayList particularly, or for these to be the wrapper classes instead of the primitives? If not, working with a simple array will avoid the overhead of boxing and unboxing, and of storing a lot of objects.
That aside, you'd probably want to loop over the list and cast each item to a double (or a Double), then add it to a new array (or ArrayList). There isn't a bulk operation for this.
You are getting outOfBoundsError because you are using set() instead of add(). set() is a replacement command and requires there to already be an object in that position.
Use Arraylist<Number>. A Number is a parent of both Double and Integer, so you would be able to add Doubles to your list and the Number.intValue() will convert (autoboxing) into Integer when required.
ArrayList<Number> list;
list.add(new Double(17.7));
Integer i = list.get(0).intValue(); // 18, rounding.
Is there any reason the fallowing code would give A compile error ?
Import java.util.*;
public class Storageclass
// class used to store the Student data
{
// creates the private array list needed.
private ArrayList<String> nameList = new ArrayList<String>();
private ArrayList<double> GPAList = new ArrayList<double>();
private ArrayList<double> passedList = new ArrayList<double>();
}
this is in a class access by a main file there is more in the class by it not part of this error. when I run this the two double arrayList give me this error.
Storageclass.java:8: error: unexpected type
private ArrayList<double> GPAList = new ArrayList<double>(1);
^
required: reference
found: double
I am not sure why or what that error means any help would be appreciated.
~ Thanks for the help was a embarrassingly novice mistake I made, but hope full this can help some other person.
Since all generic types <T> are erased at runtime to Object every type you put in place of T must also extend Object. So you can't set T to be primitive type like double but you can use its wrapper class Double. Try this way:
private List<Double> passedList = new ArrayList<Double>();
or since Java7 little shorter version
private List<Double> passedList = new ArrayList<>();
Also don't worry if you try to add variable of double type to such array since it will be autoboxed to Double.
Primitive types cannot be used as generic type arguments. Use the wrapper type Double (or whichever is appropriate).
Use ArrayList<Double> instead of ArrayList<double>.
cant be primitive type
private ArrayList<double>
use Double
private ArrayList<Double>
I need to convert my List:
List<Double> listFoo = new LinkedList<Double>();
to a array of double. So I tried:
double[] foo = listFoo.toArray();
But I get the error:
Type mismatch: cannot convert from Object[] to double[]
How can I avoid this? I could Iterate over the list and create the array step by step, but I don't want to do it this way.
You need to make, generic one
Double[] foo = listFoo.toArray(new Double[listFoo.size()]);
That would be the fastest way.
Using new Double[listFoo.size()] (with array size) you will reuse that object (generally it is used to make generic invocation of method toArray).
You can't make the array of primitives with the standard List class's toArray() methods. The best you can do is to make a Double[] with the generic method toArray( T[] ).
Double[] foo = listFoo.toArray( new Double[ listFoo.size() ] );
One easy way to make a double[] is to use Guava's Doubles class.
double[] foo = Doubles.toArray( listFoo );
It should work if you make foo an array of Doubles instead of doubles (one is a class, the other a primitive type).