Difference between if (a – b < 0) and if (a < b)

I was reading Java’s ArrayList source code and noticed some comparisons in if-statements. In Java 7, the method grow(int) uses if (newCapacity – minCapacity < 0) newCapacity = minCapacity; In Java 6, grow didn’t exist. The method ensureCapacity(int) however uses if (newCapacity < minCapacity) newCapacity = minCapacity; What was the reason behind the change? Was … Read more

How to randomize two ArrayLists in the same fashion?

I have two arraylist filelist and imgList which related to each other, e.g. “H1.txt” related to “e1.jpg”. How to automatically randomized the list of imgList according to the randomization of fileList? Like in excel, if we sort certain column, the other column will automatically follow? String [] file = {“H1.txt”,”H2.txt”,”H3.txt”,”M4.txt”,”M5.txt”,”M6.txt”}; ArrayList<String> fileList = new ArrayList<String>(Arrays.asList(file)); … Read more

How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList

I have an ArrayList that I want to iterate over. While iterating over it I have to remove elements at the same time. Obviously this throws a java.util.ConcurrentModificationException. What is the best practice to handle this problem? Should I clone the list first? I remove the elements not in the loop itself but another part … Read more

ArrayList initialization equivalent to array initialization [duplicate]

This question already has answers here: Create ArrayList from array (40 answers) Initialization of an ArrayList in one line (33 answers) Closed 5 years ago. I am aware that you can initialize an array during instantiation as follows: String[] names = new String[] {“Ryan”, “Julie”, “Bob”}; Is there a way to do the same thing … Read more

Java List.add() UnsupportedOperationException

I try to add objects to a List<String> instance but it throws an UnsupportedOperationException. Does anyone know why? My Java code: String[] membersArray = request.getParameterValues(‘members’); List<String> membersList = Arrays.asList(membersArray); for (String member : membersList) { Person person = Dao.findByName(member); List<String> seeAlso; seeAlso = person.getSeeAlso(); if (!seeAlso.contains(groupDn)){ seeAlso.add(groupDn); person.setSeeAlso(seeAlso); } } The error message: java.lang.UnsupportedOperationException java.util.AbstractList.add(Unknown … Read more