Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert linkedlist to array using `toArray()`?

Tags:

java

I do not know how to convert a linked list of doubles to array. Please help me find the error.

import java.util.*;
public class StackCalculator {


  private   LinkedList<Double> values;
  double value1 , value2 ;

  public StackCalculator()
  {
     values = new LinkedList<Double>();
  }


    void push(double x)
    {
         values.addFirst(x);
    }
    double pop()
    {
       return values.removeFirst();
    }
    double add()
    {
        value1=pop();
        value2=pop();
        return (value1 + value2);
    }

    double mult()
    {
        value1=pop();
        value2=pop();
        return (value1 * value2);
    }


    double[] v = new double[10];
    double[] getValues()
    {
        return   values.toArray(v);
    }

}
like image 369
kathy Avatar asked Feb 20 '10 23:02

kathy


People also ask

What is the use of toArray () in Java?

The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.

What does the toArray () method do when called on a list?

toArray() method returns an array containing all the elements in the list in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-based and collection-based APIs.

How do you use the toArray method?

The toArray() method is used to get an array which contains all the elements in ArrayList object in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein.

How do you turn a linked list into a string?

For converting a linked list to a string we need to traverse the linked list and after that, we need to append the element of the linked list to the string variable. We can use String class, StringBuilder, or StringBuffer for converting a LinkedList to string.


2 Answers

The List#toArray() of a List<Double> returns Double[]. The Double[] isn't the same as double[]. As arrays are objects, not primitives, the autoboxing rules doesn't and can't apply here.

Either use Double[] instead:

Double[] array = list.toArray(new Double[list.size()]);

...or create double[] yourself using a simple for loop:

double[] array = new double[list.size()];
for (int i = 0; i < list.size(); i++) {
    array[i] = list.get(i); // Watch out for NullPointerExceptions!
}
like image 72
BalusC Avatar answered Sep 21 '22 20:09

BalusC


The problem is the type of your list, which is Double (object), while you're trying to return an array of type double (primitive type). Your code should compile if you change getValues() to return a Double[].

For your other methods, it's not a problem, because Java automatically converts Doubles to doubles and back (called autoboxing). It cannot do that for array types.

like image 31
Jorn Avatar answered Sep 24 '22 20:09

Jorn