Convert contents of an array list to an array [duplicate] - java

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

Related

Java Array Element Type [duplicate]

This question already has answers here:
How to create a generic array in Java?
(32 answers)
Closed 5 years ago.
I want to know what kinds of reference can be array elements.
I know that there're primitive types like:
String[] strs = new String[5];
But there is no
List<String>[] stringList;
However, when I new a class, there is
Class Student{
String name;
List<String> courses;
}
Student[] students = new Student[5];
It says "The element type of an array may be any type, whether primitive or reference."
I think Student is reference and List<> is also reference. What's the difference between them?
Thanks.
Anything can go in an array. Primitives, other arrays, or lists.
Any of the following are legitimate declarations:
int[] intArray;
int[][] arrayOfIntArrays;
List <String> stringList;
List <String[]> stringArrayList;
List <List<String[]>> badIdea; //list of a list of string arrays
List<String>[] array of a list of strings
etc.
An array is a subclass of Object. There is nothing special about it except that java gave it some unique syntax. Otherwise, it's just like anything else you run into in java.

Convert array-like String to ArrayList in Java [duplicate]

This question already has answers here:
Convert String to int array in java
(11 answers)
Closed 5 years ago.
I've been trying to convert a String like this: "[1,2,3]" to an ArrayList in Java.
What I tried so far is to use GSon library to convert the String mentioned above to List using:
Gson - convert from Json to a typed ArrayList<T>
Convert Gson Array to Arraylist
Parsing JSON array into java.util.List with Gson
but it ends with an exception (I can provide more details if needed). The first question should be actually if it's a good approach to achieve such a transformation?
A non-regex method would be to remove the brackets from the string and then split on the commas. Then convert to an ArrayList.
String s = "[1, 2, 3]";
String[] splits = s.replace("[","").replace("]","").split(",");
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(splits));
I suggest you use regular expression to convert the string from "[1,2,3,...]" to "1,2,3,..." and then ue asList() method of Arrays class to convert it to a List.
Refer the following snippet
import java.util.regex.*;
...
...
String str = "[1,2,3,4,5,6,7,8,9,10]";
str = str.replaceAll("[(*)])","$1");
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));

Generic array creation error on ArrayList [duplicate]

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.

Java:: Converting ArrayList Primitive into Array of Number class? [duplicate]

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

Casting Object array into String array throws ClassCastException [duplicate]

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

Categories

Resources