I’ve determined that a Java ArrayList.add is similar to a JavaScript Array.push I’m stuck on finding ArrayList functions similar to the following Array.pop Array.shift Array.unshift I’m leaning toward ArrayList.remove[At] Answer ArrayList is unique in its naming standards. Here are the equivalencies: Note that unshift does not remove an element, but instead adds one to the list. Also note that corner-case
Tag: arraylist
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 of the code. My code
Sort ArrayList of strings by length
I want to order an ArrayList of strings by length, but not just in numeric order. Say for example, the list contains these words: They need to be ordered by their difference in length to a special string, for example: So the final list would look like this (difference in brackets): Answer Use a custom comparator: Then sort the list
Remove multiple elements from ArrayList
I have a bunch of indexes and I want to remove elements at these indexes from an ArrayList. I can’t do a simple sequence of remove()s because the elements are shifted after each removal. How do I solve this? Answer Sort the indices in descending order and then remove them one by one. If you do that, there’s no way
Moving items around in an ArrayList
I’ve been playing around with ArrayLists. What I’m trying to achieve is a method to do something like this: I’m trying to be able to move items up in the list, unless it is already at the top in which case it will stay the same. For example, if item 3 was moved the list would be: From my small
Using contains on an ArrayList with integer arrays
I have an ArrayList<int[]>, and I add an array to it. Suppose I want to know if j contains an array that has {1,2} in it without using w, since I will be calling it from another class. So, I create a new array with {1,2} in it… …but this would return false even though w was added to the
Java convert Arraylist to float[]
How I can do that? I have an arraylist, with float elements. (Arraylist <Float>) it is not working. cannot cast from Object[] to float[] Answer Loop over it yourself. The nullcheck is mandatory to avoid NullPointerException because a Float (an object) can be null while a float (a primitive) cannot be null at all. In case you’re on Java 8
How to create a Multidimensional ArrayList in Java?
I’m fairly new to ArrayLists anyway but I need them for this project I’m doing so if you guys could help me I would be more than grateful! Basically, I need to create a multidemensional ArrayList to hold String values. I know how to do this with a standard array, like so public static String[][] array = {{}} but this
What are the differences between ArrayList and Vector?
What are the differences between the two data structures ArrayList and Vector, and where should you use each of them? Answer Differences Vectors are synchronized, ArrayLists are not. Data Growth Methods Use ArrayLists if there is no specific requirement to use Vectors. Synchronization If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which
How to set arrayList size as null?
Why the size of the arrayList choice is 5 when all the elements have been set to null Answer Because the ArrayList still contains 5 Objects. They are null, but they are objects, thus present in the list! You can empty the ArrayList, by calling choice.clear();