Generic array creation error on ArrayList [duplicate] - java

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.

Related

Question about declaration and initialization [duplicate]

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

How to create an array of Object of some type? [duplicate]

This question already has answers here:
Initialize Java Generic Array of Type Generic
(2 answers)
Closed 7 years ago.
What's syntax for creating an array of Objects of some class type?
Object<SomeClassType<T>>[] array?
Creating an array of a generic type is not allowed, but what you can do, is creating one with wildcard and casting it. The cast will give you a warning, but as the array only contains nulls by default, it can be done safely.
#SuppressWarnings("unchecked")
Object<SomeClassType<T>>[] array = (Object<SomeClassType<T>>[]) new Object<?>[length]
A number of ways to declare an array of objects of some class type. For example,
1. MyClass[] myArray = new MyClass[# of elements];
Then initialize each as, myArray[0] = new MyClass();, myArray[1] = new MyClass(), etc..
or via a for-loop
2. MyClass[] myArray = {new MyClass(), new MyClass(), ...};
I would say something like this would allow you to create an array.
String[] testArray = new String[3];
testArray[0] = "one";
testArray[1] = "two";
testArray[2] = "three";
String[] testArray = {"one","two","three"};

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

Changing an Array to an ArrayList [duplicate]

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

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

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

Categories

Resources