issue creating a double array list - java

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>

Related

How to convert java ArrayList to an array?

I have an ArrayList created like this:
public class Main {
public static void main(String[] args) {
// write your code here
ArrayList list1 = new ArrayList();
list1.add(0, 5);
list1.add(1, 3.5);
list1.add(2, 10);
}
}
I am trying to create an array from it, using the toArray method:
public class Main {
public static void main(String[] args) {
// write your code here
ArrayList list1 = new ArrayList();
list1.add(0, 5);
list1.add(1, 3.5);
list1.add(2, 10);
Double[] list2 = list1.toArray(new Double[list1.size()]);
}
}
However, I am getting an error:
(Error:(16, 39) java: incompatible types: java.lang.Object[] ).
So I tried to cast the right side to double:
Double[] list2 = (Double[]) list1.toArray(new Double[list1.size()])
This time i am getting Exception in thread "main". I also tried to declare my ArrayList as double from beginning:
ArrayList<double> list1 = new ArrayList()<double>
With no success. How to do it properly? I know that my problem is probably something very basic.
The problem is that you are doing a number of things wrong:
ArrayList list1 = new ArrayList(); is incorrect because you are using a raw type. You should have gotten a compiler warning for that.
Given the previous list1.add(0, 5) is incorrect. The 5 will be boxed as an Integer because that compiler doesn't know that the list is only supposed to contain Double values.
You were getting this:
(Error:(16, 39) java: incompatible types: java.lang.Object[] ).
because you must have done something like this:
Double[] list2 = list1.toArray();
You appear to have corrected that in the code that you posted. But the no-args toArray method returns an Object[] containing the list content.
ArrayList<double> list1 = new ArrayList()<double> is incorrect because you cannot use a primitive type as a generic type parameter, and because the syntax on the RHS is wrong.
The correct version is ArrayList<Double> list1 = new ArrayList<>(); with an empty diamond.
Surprisingly, the best (most efficient) way to code the toArray is:
Double[] list2 = list1.toArray(new Double[0]);
Apparently, the implementation is able to initialize the array faster if it allocates it itself. (Or so I have heard ...)

Java object array declaration [duplicate]

I am trying do something like this:-
public static ArrayList<myObject>[] a = new ArrayList<myObject>[2];
myObject is a class. I am getting this error:- Generic array creation (arrow is pointing to new.)
You can't have arrays of generic classes. Java simply doesn't support it.
You should consider using a collection instead of an array. For instance,
public static ArrayList<List<MyObject>> a = new ArrayList<List<MyObject>();
Another "workaround" is to create an auxilliary class like this
class MyObjectArrayList extends ArrayList<MyObject> { }
and then create an array of MyObjectArrayList.
Here is a good article on why this is not allowed in the language. The article gives the following example of what could happen if it was allowed:
List<String>[] lsa = new List<String>[10]; // illegal
Object[] oa = lsa; // OK because List<String> is a subtype of Object
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[0] = li;
String s = lsa[0].get(0);
There is a easier way to create generic arrays than using List.
First, let
public static ArrayList<myObject>[] a = new ArrayList[2];
Then initialize
for(int i = 0; i < a.length; i++) {
a[i] = new ArrayList<myObject>();
}
You can do
public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList<?>[2];
or
public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList[2];
(The former is probably better.) Both will cause unchecked warnings, which you can pretty much ignore or suppress by using: #SuppressWarnings("unchecked")
if you are trying to declare an arraylist of your generic class you can try:
public static ArrayList<MyObject> a = new ArrayList<MyObject>();
this will give you an arraylist of myobject (size 10), or if u only need an arraylist of size 2 you can do:
public static ArrayList<MyObject> a = new ArrayList<MyObject>(2);
or you may be trying to make an arraylist of arraylists:
public static ArrayList<ArrayList<MyObject>> a = new ArrayList<ArrayList<MyObject>>();
although im not sure if the last this i said is correct...
It seems to me that you use the wrong type of parenthesis. The reason why you can't define an array of generic is type erasure.
Plus, declaration of you variable "a" is fragile, it should look this way:
List<myObject>[] a;
Do not use a concrete class when you can use an interface.

How to initialize an Array of Sets of HashMaps [duplicate]

I am trying do something like this:-
public static ArrayList<myObject>[] a = new ArrayList<myObject>[2];
myObject is a class. I am getting this error:- Generic array creation (arrow is pointing to new.)
You can't have arrays of generic classes. Java simply doesn't support it.
You should consider using a collection instead of an array. For instance,
public static ArrayList<List<MyObject>> a = new ArrayList<List<MyObject>();
Another "workaround" is to create an auxilliary class like this
class MyObjectArrayList extends ArrayList<MyObject> { }
and then create an array of MyObjectArrayList.
Here is a good article on why this is not allowed in the language. The article gives the following example of what could happen if it was allowed:
List<String>[] lsa = new List<String>[10]; // illegal
Object[] oa = lsa; // OK because List<String> is a subtype of Object
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[0] = li;
String s = lsa[0].get(0);
There is a easier way to create generic arrays than using List.
First, let
public static ArrayList<myObject>[] a = new ArrayList[2];
Then initialize
for(int i = 0; i < a.length; i++) {
a[i] = new ArrayList<myObject>();
}
You can do
public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList<?>[2];
or
public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList[2];
(The former is probably better.) Both will cause unchecked warnings, which you can pretty much ignore or suppress by using: #SuppressWarnings("unchecked")
if you are trying to declare an arraylist of your generic class you can try:
public static ArrayList<MyObject> a = new ArrayList<MyObject>();
this will give you an arraylist of myobject (size 10), or if u only need an arraylist of size 2 you can do:
public static ArrayList<MyObject> a = new ArrayList<MyObject>(2);
or you may be trying to make an arraylist of arraylists:
public static ArrayList<ArrayList<MyObject>> a = new ArrayList<ArrayList<MyObject>>();
although im not sure if the last this i said is correct...
It seems to me that you use the wrong type of parenthesis. The reason why you can't define an array of generic is type erasure.
Plus, declaration of you variable "a" is fragile, it should look this way:
List<myObject>[] a;
Do not use a concrete class when you can use an interface.

How to create a list of doubles out of an array of doubles

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.

Initialize ArrayList with doubles

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

Categories

Resources