Why do I get an UnsupportedOperationException when trying to remove an element from a List?

I have this code: public static String SelectRandomFromTemplate(String template,int count) { String[] split = template.split(“|”); List<String> list=Arrays.asList(split); Random r = new Random(); while( list.size() > count ) { list.remove(r.nextInt(list.size())); } return StringUtils.join(list, “, “); } I get this: 06-03 15:05:29.614: ERROR/AndroidRuntime(7737): java.lang.UnsupportedOperationException 06-03 15:05:29.614: ERROR/AndroidRuntime(7737): at java.util.AbstractList.remove(AbstractList.java:645) How would be this the correct way? Java.15 … Read more

Convert list to array in Java [duplicate]

This question already has answers here: Converting ‘ArrayList<String> to ‘String[]’ in Java (17 answers) Closed 3 years ago. How can I convert a List to an Array in Java? Check the code below: ArrayList<Tienda> tiendas; List<Tienda> tiendasList; tiendas = new ArrayList<Tienda>(); Resources res = this.getBaseContext().getResources(); XMLParser saxparser = new XMLParser(marca,res); tiendasList = saxparser.parse(marca,res); tiendas = … Read more

Convert ArrayList to String[] array [duplicate]

This question already has answers here: Converting ‘ArrayList<String> to ‘String[]’ in Java (17 answers) Closed 7 years ago. I’m working in the android environment and have tried the following code, but it doesn’t seem to be working. String [] stockArr = (String[]) stock_list.toArray(); If I define as follows: String [] stockArr = {“hello”, “world”}; it … Read more

Initialization of an ArrayList in one line

I wanted to create a list of options for testing purposes. At first, I did this: ArrayList<String> places = new ArrayList<String>(); places.add(“Buenos Aires”); places.add(“Córdoba”); places.add(“La Plata”); Then, I refactored the code as follows: ArrayList<String> places = new ArrayList<String>( Arrays.asList(“Buenos Aires”, “Córdoba”, “La Plata”)); Is there a better way to do this? 3 33