This question already has answers here:
make arrayList.toArray() return more specific types
(6 answers)
Closed 7 years ago.
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?
This code should do what you need.
Number[] result = new Number[myV.size()];
myV.toArray(result);
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]);
}
Related
This question already has answers here:
casting Object[] to a reference type array in java
(4 answers)
Closed 2 years ago.
At first, excuse me if this question seems stupid. Actually, I am new to Java programing.
I have the following code:
public class Test{
public static void main(String[] args){
Object[] ob = new Object[2];
ob[0] = new Integer("1");
ob[1] = new Integer("2");
Integer[] o = (Integer)ob;
System.out.println(o.length());
}
}
when compiling this code a classCastException exception is thrown. Why?. I know that Object type can not be cast to Integer type. But, in fact, each element of the array ob is an instance of Integer class which means the casting is logically true. Am I mistaken?.
Thanks in advance.
No, it is not ok.
Consider if this would be valid:
Object[] ob = new Object[2];
ob[0] = new Integer("1");
ob[1] = new Integer("2");
Integer[] o = (Integer)ob;
ob[0] = "Hello :)";
What do you expect o[0] to return?
An Integer?
Or the String "Hello"?
This would work if you created the array with new Integer[2] - then you would get a ClassCastException when you try to put the String into it.
This question already has answers here:
Difference between List and Array
(1 answer)
How to initialize an array in Java?
(11 answers)
Closed 3 years ago.
I have a question about initialization. When we initialize an array with { }, we must do it right after declaration to show compiler which type to use. Why does compiler allow diamond operator do it with 2 statements?
Integer[] array = {2,4,5};
//Integer[] array; array = {2,4,5}; - error
List<Integer> list = new ArrayList<>();
//List<Integer> list; list = new ArrayList<>(); - no error
You can do the two line way with arrays too, you just need to create a new object (and of course explicitly specify the type) when initializing.
Integer[] array;
array = new Integer[]{1, 2, 3};
This question already has answers here:
Generic array creation error
(5 answers)
How to create a generic array in Java?
(32 answers)
Closed 7 years ago.
I get the following error in my IDE "generic array creation"
I googled it but found very long explanations and didn't quite understand what the best solution is to this problem.
If anyone could suggest the best solution to this to get my code to compile...
public ArrayList<String>[] getClosedTicketIDs(Account account) {
ArrayList<String> closedSourceTickets = new ArrayList<>();
ArrayList<String> closedAccountTickets = new ArrayList<>();
// ...some unimportant to this example code...
// return
ArrayList<String>[] a = new ArrayList<String>[2]; // <-- generic array creation error
a[0] = closedSourceTickets;
a[1] = closedAccountTickets;
return a;
}
My objective is to return an array consisting of 2 ArrayList<String> (no more, no less).
You can only create raw array types. You need to do this: a = new ArrayList[2];
You cant do that but you can do
List<List<String>> a=new ArrayList<ArrayList<String>>();
but the better would be
ArrayList[] a=new ArrayList[n];
as you can fix the size in this.
This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 9 years ago.
I am attempting to change the following array line into an ArrayList that will function the same way:
private String[] books = new String[5];
I changed it to this but it is not functioning properly:
private ArrayList<String>(Arrays.asList(books))
I thought this was how an ArrayList was created
You need to create it like this:
private ArrayList<String> booksList = new ArrayList<String>(Arrays.asList(books));
new ArrayList<String>(Arrays.asList(books)) is the part which is turning your array into an ArrayList.
You could also do:
private List<String> booksList = Arrays.asList(books);
If the fact that it is an ArrayList doesn't matter.
Answered here
new ArrayList<Element>(Arrays.asList(array))
*** new
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert a generic list to an array
So I am trying to convert the contents of my arraylist into an array. However I keep getting the error
Type mismatch: cannot convert from Object[] to String
or the error
Type mismatch: cannot convert from String[] to String
any ideas how to solve this, I'm drawing up blanks. Thanks
Here is one way:
String[] listArr= new String[yourList.size()];
Iterator<String> listIter = yourList.iterator();
while (listIter .hasNext()) {
listArr[count] = listIter .next();
}
Note: There may be syntax errors, I just typed code here.
Try this:
String[] array = arrayList.toArray(new String[0]);
That is, assuming that the ArrayList was declared with type ArrayList<String>. Replace with the appropriate types if necessary.
Another way:
String[] result = new String[arrayList.size()];
arrayList.ToArray( result );