-3

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?

9
  • What do mean by safe? Commented Apr 7, 2015 at 7:58
  • in terms of memory usage. Because Android device is a mobile (small) device instead of laptop computer. @kocko Commented Apr 7, 2015 at 7:59
  • Your question isn't clear. Please make some edits. Commented Apr 7, 2015 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 Commented Apr 7, 2015 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... Commented Apr 7, 2015 at 8:04

2 Answers 2

2

This code should do what you need.

Number[] result = new Number[myV.size()];
myV.toArray(result);
0

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]);
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.