user3013480
user3013480

Reputation: 33

Using a list in java

So I had a question, which I won't go into too much detail because I managed to correctly code it into an array. Or at least, code it into an array that worked correctly; as follows:

static double getSolution (int n)
{
   double [] w = new double [n];
   w[0] = -1;
   for (int i = 0; i < n; i++)
   {
       w[i + 1] = w[i] + (( 2 / 3*n ) * Math.cos (w[i]) );           
    }
    double x = w[n];
    return x;
}

I then found out I was not meant to have coded it using an array, but instead using a list. I have attempted this, and the following is the code I have come up with:

static double getSolution1 (int n)
{
    List <double> w = new ArrayList <double>();
    w[0] = -1;
    for (int i = 0; i < n; i++)
    {
        w[i+1] = w[i] + ((2 / 3*n ) * Math.cos (w[i]) );
    }
    return w[n];
}

Now I know this is wrong from the start off, but could anybody tell me what I have done wrong and where? Also; when attempted to compile I get the error message "cannot find symbol - class List".

EDIT 1: I have tried changing the line to Double instead of double has many people have suggested, I am still getting the exact same compiling error.

EDIT 2: So if I was to transcribe the array into a list, what would the correct code be? Because I'm really confused as to how I'm supposed to write it out.

Upvotes: 1

Views: 542

Answers (6)

Ashish Aggarwal
Ashish Aggarwal

Reputation: 3026

Use

List <Double> w = new ArrayList <Double>();

instead of

List <double> w = new ArrayList <double>();

double is a primitive type but list will accept only object so thats use Double. Double is a wrapper over primitive type double.

Edit:

w[0] this is not list operation so if you are using this type of operation to access element you should use array. else mperform add remove operations 

Check List operations: http://docs.oracle.com/javase/6/docs/api/java/util/List.html

you must declare import statement for packages

import java.util.List;
import java.util.ArrayList;


so Your method become
Using array

static double getSolution1 (int n)
{
    Double[] w = new Double[n+1];
    w[0] = -1d;
    for (int i = 0; i < n; i++)
    {
        w[i+1] = w[i] + ((2 / 3*n ) * Math.cos (w[i]) );
    }
    return w[n];
}

Using List

static double getSolution1(int n) {
    List<Double> w = new ArrayList<Double>();
    w.add(-1d);
    for (int i = 0; i < n; i++) {
        double d = w.get(i) + ((2 / 3 * n) * Math.cos(w.get(i)));
        w.add(d);
    }
    return w.get(n);
}

Upvotes: 1

Justin
Justin

Reputation: 25277

You get that error because you have not imported the List interface and the ArrayList class. Insert this code at the beginning of the file:

import java.util.List;
import java.util.ArrayList;

Alternatively, you can do this (no imports needed):

static double getSolution1 (int n)
{
    //If you do the import, remove the java.util. that follows.
    //This also fixes the other errors.
    java.util.List <Double> w = new java.util.ArrayList<Double>();
    //w[0] = -1; //this does not work; a list is not an array.
    w.add(-1.0);
    for (int i = 0; i < n; i++)
    {
        //w[i+1] = w[i] + ((2 / 3*n ) * Math.cos (w[i]) ); this does not work either;
        //                                                 just add the result to the list.
        w.add(w.get(i) + ((2 / 3*n ) * Math.cos (w.get(i) );
    }
    return w.get(n);
}

Here is an explanation of the changes I made (other than java.util.List or the imports):

For the generic type, double does not work. You need to use a Double (the wrapper class for a double), thus the code changes from List<double> to List<Double>. This is necessary to Java because of Type Erasure. At compile time, the generic type is removed and the List is treated as if it were a list of Objects. Since primitives are not Objects, we need the wrapper class.

Rather than using w[0]=-1;, we use w.add(-1);. w is an object, not an array, thus cannot be accessed with [number]. Luckily, List has some useful methods; namely List#add() and List#get(). Calling w.add(-1) appends -1 to the end of the list.

For the w[i+1] part in the loop, we simply add the new element, using w.get(i) rather than w[i].

Upvotes: 0

martyj
martyj

Reputation: 14

You should only use a List where the data is not always a fixed size. They add extra overhead compared to a regular array.

To create a List object, use an implementation like ArrayList.

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

You also don't access a list via [].

w.get(index) // Gets value. Throws exception if index is greater than or equal to size() or less than 0.
w.add(index, value);  // Adds value
w.set(index, value);  // Replaces value

Upvotes: 0

Weibo Li
Weibo Li

Reputation: 3605

In java each primary data type has its Object type. e.g.

int --> Integer
double --> Double
byte --> Byte

If you want to put your primary data into a collections, you need to declare the collection with the number's corresponding Object type.

So,

List <Double> w = new ArrayList <Double>();

will work.

And

double d = 12.34;
w.add(d);

will also work. Java will convert d into Double type automatically. The mechanism is called auto-boxing.

Upvotes: 0

Bhavik Ambani
Bhavik Ambani

Reputation: 6657

You shoule have

List <Double> w = new ArrayList <Double>();

instead of

List <double> w = new ArrayList <double>();

Because collection framework does not allow primitive data type, it only accepts Wrapper classes of the primitive data type.

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Generics are meant to be used for classes, not for primitive types.

Just change the code to use Double instead of double:

List <Double> w = new ArrayList <Double>();

Upvotes: 1

Related Questions