I’m using Android 2.3.3. I have a mainArrayList<String>, and a subArrayList<String> which has content that is part of mainArrayList<String>. I wish to remove this subArrayList<String> from the mainArrayList<String>.
How do I do it?
I tried using alMainArrayList.remove(alSubArrayList). But it doesn’t work. Can someone help me?
Ex:
alMainArrayList<String> = [1,+,(,2,*,(,3,+,4,),),)]
alSubArrayList<String> = [(,3,+,4,)]
For more information, I am developing a calculator app, where in i have the given expression in a ArrayList<String> alMainArrayList. After performing the evaluation of a sub expression, I wish to remove it from the alMainArrayList and then insert the result into it at the specified index. The code complies.
The problem is, the subexpression, which i have in alSubArrayList doesn’t get deleted, when i use alMainArrayList.remove(alSubArrayList) or even alMainArrayList.removeAll(alSubArrayList).
It’s a loop, and so the result gets added at the specified index (without first deleting the subArrayList) and the loop enters into infinite loop.
To provide the code snippet, it’s a bit hard. They are bits of pieces here and there. So, if someone understood what i wanted to say, please help me.
I have found a method subList() for the ArrayList.
Here is the information as per the help, when you place it on the method.
subList() :::: for an ArrayList
Returns a part of consecutive elements of this list as a view. The returned view will be of zero length if start equals end. Any change that occurs in the returned subList will be reflected to the original list, and vice-versa. All the supported optional operations by the original list will also be supported by this subList.
This method can be used as a handy method to do some operations on a sub range of the original list, for example list.subList(from, to).clear();
If the original list is modified in other ways than through the returned subList, the behavior of the returned subList becomes undefined.
The returned subList is a subclass of AbstractList. The subclass stores offset, size of itself, and modCount of the original list. If the original list implements RandomAccess interface, the returned subList also implements RandomAccess interface.
The subList’s set(int, Object), get(int), add(int, Object), remove(int), addAll(int, Collection) and removeRange(int, int) methods first check the bounds, adjust offsets and then call the corresponding methods of the original AbstractList. addAll(Collection c) method of the returned subList calls the original addAll(offset + size, c).
The listIterator(int) method of the subList wraps the original list iterator. The iterator() method of the subList invokes the original listIterator() method, and the size() method merely returns the size of the subList.
All methods will throw a ConcurrentModificationException if the modCount of the original list is not equal to the expected value.
Parameters
start start index of the subList (inclusive).
end end index of the subList, (exclusive).
Returns
a subList view of this list starting from start (inclusive), and ending with end (exclusive)
Throws
IndexOutOfBoundsException if (start < 0 || end > size())
IllegalArgumentException if (start > end)