0

I'm doing an assignment for class that requires me to create a class that extends ArrayList<Double>. However I cannot make an ArrayList<Double> without getting an error.

I created a new class to try and test it and it still doesn't work. Here's what I have:

import java.util.ArrayList;

public class imSad {

   ArrayList<Double> var = new ArrayList<Double>();

   var.add(double 15.0);

   var.add(15.0);

However this gives me the error : Syntax Error on token "15.0" invalid VariableDeclaratorID and Syntax Error on token "add" Identifier expected after this token.

What am I doing wrong? I've tried searching it but I'm having trouble finding much on arraylists of doubles.

4
  • var.add(new Double(15.0f)); var.add(15.0f); Commented Nov 25, 2013 at 8:29
  • 1
    Why would you expect this line - var.add(double 15.0); to compile? Commented Nov 25, 2013 at 8:29
  • 1
    Just remove the word double from before 15.0. Everything else is perfect. Commented Nov 25, 2013 at 8:29
  • I've tried searching it but I'm having trouble finding much on arraylists of doubles. Typing 'arraylist of doubles' (google) gives you examples when clicking just the second link - took me ~10s to find. Commented Nov 25, 2013 at 8:38

7 Answers 7

3

the cast is wrong.

use

var.add((double) 15.0); 

or

var.add(15.0d);

or better, by default

var.add(15.0);
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't 15.0 a double type by default?
Don't do this. 15.0 is already a double. It's completely redundant either to cast it, or to affix the d.
1

You can assign the value to a double variable and then add it.

double val = 15.0; // create a double variable
var.add(val); // add it to the list
..
var.add(15.0); // add it without creating a new double variable, which you already did

Also, by default 15.0 is double and thus you need not cast it.

Comments

1

You don't need a cast to write double literals in Java; all floating-point literals are double by default (JLS7, 3.10.2). So just writing

var.add(15.0);

is entirely sufficient.

Comments

1

Just add doubles like this, you don't need casting:

var.add(15.0);

Comments

1

if you wanna cast it, you should write

var.add((double) 15.0);

or

var.add(15.0d);

if you write

var.add(double 15.0);

it is a declaration of a new double, which you will name 15.0 (which is not valid, because you can't start a variable name with a number and it can not contain a dot.)

Comments

1

You may try this,

import java.util.ArrayList;

public class YouShouldResearch{

public static void main(String ar[]) {
     ArrayList<Double> var = new ArrayList<Double>();
     var.add(15.0);

}
}

Comments

0
var.add(15.0);

should be OK. Or

var.add((double)15.0);

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.