My problem
I have a fixed size ArrayList that contains user variables. Despite the fact that ArrayList has a fixed size, sometimes many of them will be practically null. The thing is, I need to return an ArrayList without null variables inside it. It is important to note: ArrayList will first have all its non-zero elements, and then all zeros under them, for example, the elements will not be mixed. Example: [non-null, non-null, .... null, null, null]
My workaround
I would like to create a for loop that checks (from the last to the first index) each of the elements inside an ArrayList to determine if it is null or not. If it is null, I would call this code:
for (i = size-1; i >=0 ; i--) { groupList = new ArrayList<>(groupList.subList(0, i)); }
My question
If the ArrayList is too large, this method can be especially slow (or not?). I was wondering if there is a better, more efficient solution. AFAIK .subList method is expensive.
java arraylist algorithm time-complexity
Jose Lopez
source share