This question already has answers here:
Convert ArrayList<String> to String[] array [duplicate]
(6 answers)
Closed 7 years ago.
private ArrayList<Integer> list;
...
...
...
for (int i=0; i < list.size(); i++) {
Log.e("downloadTask","resource ID is " + list.get(i));
}
Integer[] resourceId = resourceId= (Integer[])list.toArray();
before for() statement, list had already been initialized. It has three elements;
The Log message is right, but when run
Integer[] resourceId = (Integer[]) list.toArray();
it will throw Exceptions. I don't know how to solve this problem.
list.toArray() returns an array of objects (Object[]), which can not be cast to an integer array (because Object[] can contain anything, like String, Boolean, SpiderMan, not only Integer).
Try:
list.toArray(new Integer[list.size()]); //will fill and return passed array
//with all elements from list
In the future, please provide the exact exceptions being thrown (and include the stack trace) so it's easier for people to spot the problem.
Related
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:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 7 years ago.
I know I have done this in the past. I have an ArrayList that has been populated with integers. I need to iterate over it and find the maximum value. However, when I iterate over an array with something like this:
for (int i = 0; i < list.size(); i++)
{
if (list.get(i) > max)
{
max = list.get(i);
}
}
I get an error that says java.lang.Object cannot be converted to int or that > is a bad operand type. I have never encountered this before, and I have used arraylists multiple times for this same purpose. What am I doing wrong here?
max is declared as an int but is not initialized.
Most likely you declared the ArrayList just like this:
ArrayList list = new ArrayList<>();
instead of :
ArrayList<Integer> list = new ArrayList<>();
This question already has answers here:
Convert ArrayList<String> to String[] array [duplicate]
(6 answers)
Convert list to array in Java [duplicate]
(11 answers)
Closed 9 years ago.
List<String> list = getNames();//this returns a list of names(String).
String[] names = (String[]) list.toArray(); // throws class cast exception.
I don't understand why ? Any solution, explanation is appreciated.
This is because the parameterless toArray produces an array of Objects. You need to call the overload which takes the output array as the parameter, and pass an array of Strings, like this:
String[] names = (String[]) list.toArray(new String[list.size()]);
In Java 5 or newer you can drop the cast.
String[] names = list.toArray(new String[list.size()]);
You are attempting to cast from a class of Object[]. The class itself is an array of type Object. You would have to cast individually, one-by-one, adding the elements to a new array.
Or you could use the method already implemented for that, by doing this:
list.toArray(new String[list.size()]);
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 );
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Removing an element from an Array (Java)
Is there a way I can get rid of some elements in an array.
for instance, if i have this array
int testArray[] = {0,2,0,3,0,4,5,6}
Is there a "fast" way to get rid of the elements that equal 0
int resultArray[] = {2,3,4,5,6}
I tried this function but I got lost using Lists
public int[] getRidOfZero(int []s){
List<> result=new ArrayList<>();
for(int i=0; i<s.length; i++){
if(s[i]<0){
int temp = s[i];
result.add(temp);
}
}
return result.toArray(new int[]);
}
Java arrays can't be resized. You need to create a new array.
Count the non-zero elements in the array. Create a new array that size. Copy the elements from the old to the new array, skipping over zero elements.
You can do this with lists. Your best bet is to create a list of Integers; add non-zero elements to it; then use toArray to create an array from the list.
You're quite close, but:
Your generics were messed up (List<> is syntactically invalid)
Your comparison was only adding the element if it was less than zero (rather than adding it if it was not equal to zero)
You were calling the wrong toArray() method.Because ints are primitives, you have to turn the list back into an array yourself.
public int[] getRidOfZero(int[] s) {
List<Integer> result = new ArrayList<Integer> ();
for (int i : s) {
if (i != 0) {
result.add(i);
}
}
int[] toReturn = new int[result.size()];
for (int i=0; i<result.size(); i++) {
toReturn[i] = result.get(i);
}
return toReturn;
}
Since you're starting with an array, you'll need to create a new array and copy the items from the old array, excluding the zeros.
If you remove a bunch of zeros at once, this is going to be fast.
Otherwise, you'll need to begin with the doubly-linked LinkedList.
LinkedList has an iterator that allows you to move along the list and remove elements, which will be a constant-time operation.