So, I have a bit of a problem. I'm trying to convert an array of integers (called list
) to an ArrayList
(called arrList
). The code shown below works fine:
java.util.ArrayList arrList = new java.util.ArrayList(Arrays.asList(list));
However, when compiled there is one warning: the line above is reported as using "unchecked or unsafe operations."
Unfortunately, I cannot seem to dispose of this warning. Since this is a homework assignment, part of the criteria is to be warning-free. Is there any way I could convert the array to an ArrayList without warnings/errors?
We can convert an array to arraylist using following ways. Using Arrays. asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.
Answer: One of the basic methods to convert an array to a list in Java is to use the asList () method of the Arrays class. List<String> aList = Arrays. asList (myarray); Apart from this, there are more methods that convert an array to a list as discussed earlier in this tutorial.
Ensure you are using the correct "Arrays" import. By default, my Eclipse will try to import "edu.emory.mathcs.backport.java.util". Make sure it is just the standard "java.util" version and you should do something like:
String[] arr = new String[]{"one", "two", "three"};
List<String> list = Arrays.asList(arr);
Collections.addAll(targetList, sourceArray);
Try the above solution. It should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With