developer

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

Question: ❓❓❓ 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 Read more…

By admin, ago
developer

Convert list to array in Java

Question: ❓❓❓ 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 = tiendasList.toArray(); this.adaptador = new adaptadorMarca(this, R.layout.filamarca, tiendas); setListAdapter(this.adaptador); I need to Read more…

By admin, ago
developer

Convert ArrayList to String[] array [duplicate]

Question: ❓❓❓ This question already has an answer here: Converting ‘ArrayList<String> to ‘String[]’ in Java 11 answers 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 Read more…

By admin, ago
developer

Initialization of an ArrayList in one line

Question: ❓❓❓ I want 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 Read more…

By admin, ago
developer

When to use LinkedList over ArrayList?

Question: ❓❓❓ I’ve always been one to simply use: List<String> names = new ArrayList<String>(); I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code. When should LinkedList be used over ArrayList and vice-versa? Good Answer: 👌👌👌 Read more…

By admin, ago
developer

Create ArrayList from array

Question: ❓❓❓ I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList<Element> arraylist = ???; Good Answer: 👌👌👌 new ArrayList<>(Arrays.asList(array)) Read More: 👉 https://stackoverflow.com/questions/157944/create-arraylist-from-array

By admin, ago