4

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.

1
  • 1
    Did you at least try to search for this issue on the net before asking? Commented Nov 18, 2013 at 2:31

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Primitive types cannot be used as generic type arguments. Use the wrapper type Double (or whichever is appropriate).

Comments

2

Use ArrayList<Double> instead of ArrayList<double>.

2 Comments

In fact, OP should use List<Double> instead of ArrayList<Double>.
@LuiggiMendoza Why should a List be used instead of an ArrayList?
0

cant be primitive type

private ArrayList<double>

use Double

private ArrayList<Double>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.