Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

If let say i have;

ArrayList <Double> myV = new ArrayList <Double>();
myV.add(12.2); 
myV.add(3.2); 
myV.add(5.00); 

// this is error
Number[] youV = myV.toArray();

the below code is error when I compiled it. What should I do then to convert the ArrayList into Number of arrays type?

How to convert them into Number[] ? And lastly, is this code list safe for us to use, if I apply this code inside Android?

share|improve this question

marked as duplicate by Der Golem, alex2410, maveň, 一二三, kolossus Apr 7 at 13:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
What do mean by safe? –  kocko Apr 7 at 7:58
    
in terms of memory usage. Because Android device is a mobile (small) device instead of laptop computer. @kocko –  gumuruh Apr 7 at 7:59
    
Your question isn't clear. Please make some edits. –  Chaker Mallek Apr 7 at 8:01
    
if its a simple list then you can use String numbers[] myV.toArray(new String[myV.size()]); but there are certain limitations also on this new array, need to know the requirement of the array –  Saurabh Jhunjhunwala Apr 7 at 8:04
    
Android Devices generally have RAM in the range of 0.5 to 3 GB and each application typically has a dedicated heap of 64 MB or more. I don't understand in what way you think your code will be limited by running on Android? It's not an embedded device from the 1990s we are talking about... –  JHH Apr 7 at 8:04

2 Answers 2

up vote 1 down vote accepted

This code should do what you need.

Number[] result = new Number[myV.size()];
myV.toArray(result);
share|improve this answer

To answer your first question, You can convert them like this.

public static void main(String[] args) {

    ArrayList<Double> myV = new ArrayList<Double>();
    myV.add(12.2);
    myV.add(3.2);
    myV.add(5.00);
    Number[] target = new Number[myV.size()];
    myV.toArray(target);
    System.out.println(target[0]);
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.