developer

How to convert an ArrayList containing Integers to primitive int array?

Question: โ“โ“โ“ I’m trying to convert an ArrayList containing Integer objects to primitive int[] with the following piece of code, but it is throwing compile time error. Is it possible to convert in Java? List<Integer> x = new ArrayList<Integer>(); int[] n = (int[])x.toArray(int[x.size()]); Good Answer: ๐Ÿ‘Œ๐Ÿ‘Œ๐Ÿ‘Œ You can convert, but Read more…

By admin, ago
developer

What is the difference between ArrayList.clear() and ArrayList.removeAll()?

Question: โ“โ“โ“ Assuming that arraylist is defined as ArrayList<String> arraylist, is arraylist.removeAll(arraylist) equivalent to arraylist.clear()? If so, can I assume that the clear() method is more efficient for emptying the array list? Are there any caveats in using arraylist.removeAll(arraylist) instead of arraylist.clear()? Good Answer: ๐Ÿ‘Œ๐Ÿ‘Œ๐Ÿ‘Œ The source code for clear(): Read more…

By admin, ago
developer

How to sort an ArrayList?

Question: โ“โ“โ“ I have a List of doubles in java and I want sorted ArrayList in descending order Input ArrayList is like- List<Double> testList=new ArrayList(); testList.add(0.5); testList.add(0.2); testList.add(0.9); testList.add(0.1); testList.add(0.1); testList.add(0.1); testList.add(0.54); testList.add(0.71); testList.add(0.71); testList.add(0.71); testList.add(0.92); testList.add(0.12); testList.add(0.65); testList.add(0.34); testList.add(0.62); The out put should be like this 0.92 0.9 0.71 Read more…

By admin, ago
developer

Save ArrayList to SharedPreferences

Question: โ“โ“โ“ I have an ArrayList with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back at a later time, however I don’t need the array available Read more…

By admin, ago
developer

How to avoid โ€œConcurrentModificationExceptionโ€ while removing elements from `ArrayList` while iterating it? [duplicate]

Question: โ“โ“โ“ This question already has an answer here: Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop 17 answers I’m trying to remove some elements from an ArrayList while iterating it like this: for (String str : myArrayList) { if (someCondition) { myArrayList.remove(str); } } Of course, I Read more…

By admin, ago