0

Given 3 variables (homeNumber,mobileNumber and workNumber), which can be null, but atleast one of those will be a String, I need to return a String array so I can use it later on an Android Dialog. I'm having troubles doing this. I tried doing it in an ArrayList and removing all null elements, which leaves an ArrayList with only Strings, like I want, but when trying to change it to an Array I get a ClassCast exception on the last line.

ArrayList numberList = new ArrayList();
numberList.add(homeNumber);
numberList.add(mobileNumber);
numberList.add(workNumber);
numberList.removeAll(Collections.singleton(null)); 
final String[] items= (String[]) numberList.toArray();

Any ideas how to fix this?

True Soft
8,7976 gold badges58 silver badges86 bronze badges
asked Dec 27, 2011 at 21:12
2
  • 2
    You should check if the data you add in your arraylist are null before inserting them. Why do you want to transform your ArrayList in String Array? Why you don't build an ArrayList<String>? Commented Dec 27, 2011 at 21:15
  • You cannot cast non generic ArrayList to an array of a given object, since it can contain objects of different classes. Given the fact that this is Android code why not use generics as Jeremy stated above? Commented Dec 27, 2011 at 21:20

5 Answers 5

8
String[] items = new String[numberList.size()];
numberList.toArray(items);
answered Dec 27, 2011 at 21:18
Sign up to request clarification or add additional context in comments.

1 Comment

or you need to loop through the list and populate String[] by incrementing array index. You can't directly do final String[] items= (String[]) numberList.toArray(); because return type of toArray is object[]
1

You can do one of two things:

Pass in the type of array you want to get (there's no need to instantiate a full length array, performance is the same regardless):

final String[] items= (String[]) numberList.toArray(new String[0]);

However, the better solution is to use generics:

List<String> numberList = new ArrayList<String>();
final String[] items= (String[]) numberList.toArray();
answered Dec 27, 2011 at 21:21

Comments

0

The return type of ArrayList.toArray() is Object[], unless you pass an array as the first argument. In that case the return type has the same type as the passed array, and if the array is large enough it is used. Do this:

final String[] items= (String[])numberList.toArray(new String[3])
answered Dec 27, 2011 at 21:17

1 Comment

The list may not have exactly 3 items, because he removes nulls from it.
0

Use the other method toArray() of List class:

numberList.toArray(new String[numberList.size()]);
answered Dec 27, 2011 at 21:19

2 Comments

This would work if you were using it as an argument for something like an adapter (its what I do) but if you want the array to persist past this line of code, you have to instantiate it separately.
The return of the method is exactly the array created. He can assign it to String[] items
0

Change

ArrayList numberList = new ArrayList();

to

List<String> numberList = new ArrayList<String>();
answered Dec 27, 2011 at 21:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.