I wrote my own PriorityQueue class to manage unlimited elements. TO do this I declared an ArrayList and then I used the standard methods to add/swap the elements in it, but I get the error in the title. This is my code:
public class PriorityQueue<E extends Comparable<E>> {
private ArrayList<E> queue;
public PriorityQueue() {
queue= new ArrayList<>();
}
public <T> int size() {
return queue.size();
}
public <T> boolean isEmpty() {
return queue.isEmpty();
}
public <T> void insert(E element) {
queue.add(queue.size(), element);
siftUp(queue.size()-1);
}
public <T> void siftUp(int size) {
E elem = queue.get(size);
for (; size>0 && elem.compareTo(queue.get(size/2))==1; size=size/2)
queue.add(size, queue.get(size/2));
queue.add(size, elem);
}
}
How do I can solve it?
Edit: changed from "T elem" to "E elem" and now it compiles, but inserts two identical elements.
// perhaps this what you meant to do... Maybe? Kinda?
public class PriorityQueue<E> extends Comparable<E>
{
private ArrayList<E> queue = new ArrayList<E>();
public PriorityQueue() { }
public int size() { return queue.size(); }
public boolean isEmpty() { return queue.isEmpty(); }
public void insert(E element) {
queue.add(queue.size(), element);
siftUp(queue.size() - 1);
}
public void siftUp(int size) {
E elem = queue.get(size);
for (; size>0 && elem.compareTo(queue.get(size/2))==1; size=size/2)
queue.add(size, queue.get(size/2));
queue.add(size, elem);
}
}
Related
I'm working in a school project, where I want to implement the Iterator design pattern. I want to use generic arrays.
Container.java
public interface Container {
Iterator getIterator();
}
Iterator.java
public interface Iterator <T> {
boolean hasNext();
T next();
}
TransactionRepository.java
public class TransactionRepository<T> implements Container {
public TransactionRepository(){
userTransactions = new ArrayList<>();
}
public List<T> userTransactions;
#Override
public Iterator <T> getIterator() {
return new UserTransactions();
}
private T t;
public void add(T t) {
this.t = t;
}
public T get() {
return t;
}
private class UserTransactions implements Iterator <T> {
int index;
#Override
public boolean hasNext() {
return index < userTransactions.size();
}
#Override
public T next() {
if(this.hasNext())
return userTransactions.get(index);
return null;
}
}
}
In my other class, I add the elements to the list by first creating the TransactionRepository object like this: TransactionRepository<String> companyName = new TransactionRepository<String>();.
Then I add elements to the array with the add method companyName.add("CompanyName");. After that I want to print the array using Iterator, but It just won't print the elements. I have tried multiple variations, but none of them worked.
Iterator <String> stringIterator = companyName.getIterator();
while (stringIterator.hasNext()) {
System.out.println("Name : " + companyName.get());
}
With the current implementation List<T> userTransactions is never updated.
In this case userTransactions.size() in hasNext() method will always return 0 so the result of method will be false.
Moreover, you should use stringIterator.next() instead of companyName.get(). Since you implement your own iterator you don't want to use get() method at all.
There is also a need to update index counter variable after calling next() method.
#Override
public T next() {
if (this.hasNext())
return userTransactions.get(index++);
return null;
}
Change modifier on userTransactions to private final as it should be referenced just with iterator.
Code with proposed improvements:
public class TransactionRepository<T> implements Container {
public TransactionRepository() {
userTransactions = new ArrayList<>();
}
public List<T> userTransactions;
#Override
public Iterator<T> getIterator() {
return new UserTransactions();
}
public void add(T t) {
userTransactions.add(t);
}
private class UserTransactions implements Iterator<T> {
int index;
#Override
public boolean hasNext() {
return index < userTransactions.size();
}
#Override
public T next() {
if (this.hasNext()) {
return userTransactions.get(index++);
}
return null;
}
}
}
It seems that you are never adding elements to your userTransactions List on the add method
You add() method doesnt add anything to your list , it's just like a setter of the attribute t , you should use it to add elements to the list instead
public void add(T t) {
userTransactions.add(t);
}
There is also another problem , the index , your next() method gets the index element while you didnt initialise your index variable , i recommand you to do it in this way :
int index = 0 ;
...
public T next() {
if(this.hasNext())
int temp = index;
index++;
return userTransactions.get(temp);
return null;
}
I am trying to figure out how to store the values of tst.insert() and tsttxt.insert() into an array. So far the only thing I have been able to do is have the program recognize that they are there. When I try to print the variables I get the last value of tst.insert(). I am assuming that the last value is displayed because the other values are being overridden.
public class genericdrive {
public static void main(String[] args) {
collection<Integer> tst = new collection<>();
collection<String> tsttxt = new collection<>();
//System.out.println("If collection is empty return true: " + tst.isEmpty());
tst.insert(45);
tst.insert(43);
tst.insert(90);
tsttxt.insert("Jeff");
tsttxt.insert("Rey");
}
}
..
public class collection<T> extends genericdrive {
private T element;
private T[]array;
// collection<T> objt = new collection<>();
public void set(T element) {
this.element = element;
}
public T get() {
return element;
}
public <T> void insert(T i) {
i = (T) element;
//array[0]=<T> i;
}
}
considering that array variable holds all the elements the insert function you wrote does not push any value into it.
It is a workaround if the private variable is expected to be an array.
Try the following:
public class MyCollection<T> {
private T element;
private T[] array;
MyCollection(){
array = (T[]) Array.newInstance( Comparable.class , 0);
}
public void set(T element) {
this.element = element;
}
public T get() {
return element;
}
public void insert(T i) {
T[] temp = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length + 1);
temp[array.length] = i;
System.arraycopy(array, 0, temp, 0, array.length);
array = temp;
}
}
To be precise, I am trying to flatten a tree and I am stuck on trying to get the values of private attributes in a generic class using a generic function.
I have attached the classes to show how the tree is structured exactly. But it's looks something like this:
/|\
1 | 6
/|\
5 4 9
I am going to paste my attempt at the end. First, let me introduce the classes:
Triple simply stores three values of the same type.
public class Triple<V> {
private final V l, m, r;
public Triple(V l, V m, V r) {
this.l = l;
this.m = m;
this.r = r;
}
public V left() { return l; }
public V middle() { return m; }
public V right() { return r; }
}
Straightforward interface:
public interface Function<P, R> {
R apply(P p);
}
Now, for a tricky class. This one is simply a type that stores one of EitherOr of two types of value, but not both.
public class EitherOr<A,B> {
// Constructs a left-type EitherOr
public static <A> EitherOr left(A a) {
return new EitherOr(a, null);
}
// Constructs a right-type EitherOr
public static <B> EitherOr right(B b) {
return new EitherOr(null, b);
}
private final A a;
private final B b;
private EitherOr(A a, B b) {
this.a = a; this.b = b;
}
public<T> T ifLeft(Function<A,T> f) {
return f.apply(a);
}
public<T> T ifRight(Function<B,T> f) {
return f.apply(b);
}
public boolean isLeft() {
return b == null;
}
}
I know this is getting long, but bear with me. This class implements the tree structure.
public interface Tree<T> {
EitherOr<T, Triple<Tree<T>>> get();
static final class Leaf<T> implements Tree<T> {
public static <T> Leaf<T> leaf (T value) {
return new Leaf<T>(value);
}
private final T t;
public Leaf(T t) { this.t = t; }
#Override
public EitherOr<T, Triple<Tree<T>>> get() {
return EitherOr.left(t);
}
}
static final class Node<T> implements Tree<T> {
public static <T> Tree<T> tree (T left, T middle, T right) {
return new Node<T>(Leaf.leaf(left), Leaf.leaf(middle), Leaf.leaf(right));
}
private final Triple<Tree<T>> branches;
public Node(Tree<T> left, Tree<T> middle, Tree<T> right) {
this.branches = new Triple<Tree<T>>(left, middle, right);
}
#Override
public EitherOr<T, Triple<Tree<T>>> get() {
return EitherOr.right(branches);
}
}
}
Alright. Here is my idea for flattening:
public class MyFlattenTree<T> implements FlattenTree<T> {
public List<T> flattenInOrder(Tree<T> tree) {
List<T> list = new ArrayList<T>();
EitherOr<T, Triple<Tree<T>>> EitherOr;
EitherOr = tree.get();
// it is a leaf
if (EitherOr.isLeft()) {
// This is where the problem lies
// I don't how to get the value using a function f
list.add((T) EitherOr.ifLeft(f));
return list;
}
else {
// basically recursively go through the tree somehow
}
return null;
}
}
As I said, I am stuck with trying to retreive the value in the EitherOr class using the Function interface. I am thinking of implementing the Function interface and write a function for "apply" that just gets the value, but I am not sure how to do that. Any help would be appreciated. Thanks!
So, here is your flattenInOrder method:
public List<T> flattenInOrder(final Tree<T> tree) {
final EitherOr<T, Triple<Tree<T>>> EitherOr = tree.get();
if (EitherOr.isLeft()) {
return Collections.singletonList(EitherOr.ifLeft(this.ifLeftFunction));
}
return EitherOr.ifRight(this.ifRightFunction);
}
Quite simple, assuming that:
ifLeftFunction yields a single element (since EitherOr<T, Triple<Tree<T>>> has a single T elem' if it s "left")
... and:
ifRightFunction yields a collection of elements (since EitherOr<T, Triple<Tree<T>>> has a list of T elems' if it is "right")
Let's look into these functions now:
ifLeftFunction is... basic. I want to extract a T from... a T.
final Function<T, T> ifLeftFunction = new Function<T, T>() {
#Override
public T apply(final T t) {
return t;
}
};
ifRightFunction is slightly more complex: it has to be recursive and collect all Ts from the Tree it's browsing:
final Function<Triple<Tree<T>>, List<T>> ifRightFunction = new Function<Triple<Tree<T>>, List<T>>() {
#Override
public List<T> apply(final Triple<Tree<T>> t) {
final List<T> res = new ArrayList<>();
res.addAll(MyFlattenTree.this.flattenInOrder(t.left()));
res.addAll(MyFlattenTree.this.flattenInOrder(t.middle()));
res.addAll(MyFlattenTree.this.flattenInOrder(t.right()));
return res;
}
};
And... you're done!
Sample working code:
public class MyFlattenTree<T> {
private final Function<Triple<Tree<T>>, List<T>> ifRightFunction = new Function<Triple<Tree<T>>, List<T>>() {
#Override
public List<T> apply(final Triple<Tree<T>> t) {
final List<T> res = new ArrayList<>();
res.addAll(MyFlattenTree.this.flattenInOrder(t.left()));
res.addAll(MyFlattenTree.this.flattenInOrder(t.middle()));
res.addAll(MyFlattenTree.this.flattenInOrder(t.right()));
return res;
}
};
private final Function<T, T> ifLeftFunction = new Function<T, T>() {
#Override
public T apply(final T t) {
return t;
}
};
public static void main(final String[] args) {
final Tree<String> tree = new Node<>(new Leaf<>("1"), new Node<>(new Leaf<>("5"), new Leaf<>("4"), new Leaf<>("9")), new Leaf<>("6"));
System.out.println(new MyFlattenTree<String>().flattenInOrder(tree));
}
public List<T> flattenInOrder(final Tree<T> tree) {
final EitherOr<T, Triple<Tree<T>>> EitherOr = tree.get();
if (EitherOr.isLeft()) {
return Collections.singletonList(EitherOr.ifLeft(this.ifLeftFunction));
}
return EitherOr.ifRight(this.ifRightFunction);
}
}
Note that I'm creating the exact Tree you're featuring as an example in your question in the main method here:
public static void main(final String[] args) {
final Tree<String> tree = new Node<>(new Leaf<>("1"), new Node<>(new Leaf<>("5"), new Leaf<>("4"), new Leaf<>("9")), new Leaf<>("6"));
System.out.println(new MyFlattenTree<String>().flattenInOrder(tree));
}
Output: [1, 5, 4, 9, 6]
Cheers ;)
Here's a class I wrote that implements Iterable<Integer> for an arithmetic series (from start to stop in steps of step)
package com.example.test;
import java.util.Iterator;
import com.google.common.collect.AbstractIterator;
public class ArithmeticSeries implements Iterable<Integer>
{
final private int start, step, stop;
public int getStart() { return this.start; }
public int getStep() { return this.step; }
public int getStop() { return this.stop; }
public ArithmeticSeries(int start, int step, int stop)
{
this.start = start;
this.step = step;
this.stop = stop;
}
#Override public Iterator<Integer> iterator()
{
return new AbstractIterator<Integer>() {
private Integer n = null;
#Override protected Integer computeNext() {
int next;
if (this.n == null)
{
next = getStart();
}
else
{
next = this.n + getStep();
if ((getStep() > 0 && next > getStop())
|| (getStep() < 0 && next < getStop()))
return endOfData();
}
this.n = next;
return next;
}
};
}
#Override public String toString() {
return getStart()+":"+getStep()+":"+getStop();
}
public static void main(String[] args) {
Iterable<Integer> range = new ArithmeticSeries(100,-1,80);
System.out.println(range);
for (int i : range)
System.out.println(i);
}
}
Is there a way to implement iterator() that's more elegant? I don't like the null check and use of Integer (alternative would be an extra flag boolean firstTime), it just seems wrong.
return new AbstractIterator<Integer>() {
int next = getStart();
#Override protected Integer computeNext() {
if (isBeyondEnd(next)) {
return endOfData();
}
Integer result = next;
next = next + getStep();
return result;
}
};
If you wanted to, you could probably implement this as an immutable List<Integer>. If you extend AbstractList then the Iterator would be taken care of for you. Actually, I think AbstractList would really be the best way to go. The whole class would look like something like this (I haven't checked that it works right in all situations):
public class ArithmeticSeries extends AbstractList<Integer> {
private final int start;
private final int step;
private final int size;
public ArithmeticSeries(int start, int end, int step) {
this.start = start;
this.step = (start < end) ? step : -step;
this.size = (end - start) / this.step + 1;
}
#Override public Integer get(int index) {
return start + step * index;
}
#Override public int size() {
return size;
}
}
You can use a Function to abstract the successive values and a Predicate to control the end of iteration, eventually creating an Unfold implementation:
public final class UnfoldIterator<E> implements Iterator<E> {
public static <E> Iterator<E> unfold(E initial, Function<? super E, ? extends E> next, Predicate<? super E> finished) {
return new UnfoldIterator<E>(initial, next, finished)
}
private final Function<? super E, ? extends E> next;
private final Predicate<? super E> finished;
private E element;
public UnfoldIterator(E initial, Function<? super E, ? extends E> next, Predicate<? super E> finished) {
super();
this.next = next;
this.finished = finished;
this.element = initial;
}
#Override protected Integer computeNext() {
if (finished.apply(element)) {
return endOfData();
}
E result = element;
element = next.apply(element);
return result;
}
}
Then ArithmeticSeries becomes:
public Iterable<Integer> series(final int start, final int step, final int stop) {
return new Iterable<Integer>() {
public Iterator<Integer> iterator() {
return new UnfoldIterator<Integer>(start, new Function<Integer, Integer>() {
public Integer apply(Integer from) {
return from - step;
}
}, new Predicate<Integer>() {
public boolean apply(Integer input) {
return input >= stop;
}
});
}
};
}
Of course the code seems more complex now, but with appropriate base functions for comparison and algebra the call becomes much clearer:
return unfold(start, subtractBy(step), not(lessThan(stop)));
I think the best tool for your problem in guava is the AbstractLinkedIterator. Implementation of your example would look like this:
final Iterator<Integer> series = new AbstractLinkedIterator<Integer>(100) {
#Override protected Integer computeNext(final Integer previous) {
return previous == 80 ? null : previous - 1;
}
};
while (series.hasNext()) {
System.out.println(series.next());
}
You can easily create an Iterable adapter for this iterator, e.g. like this:
package sk.the0retico.guava;
import java.util.Iterator;
import com.google.common.base.Function;
import com.google.common.collect.AbstractLinkedIterator;
public class LinkedIterable<T> implements Iterable<T> {
public static final <T> Iterable<T> from(final T first,
final Function<T, T> computeNext) {
return new LinkedIterable<T>(first, computeNext);
}
public static void main(final String[] args) {
final Iterable<Integer> series = LinkedIterable.from(100,
new Function<Integer, Integer>() {
#Override public Integer apply(final Integer input) {
return input == 80 ? null : input - 1;
}
});
for (final Integer value : series) {
System.out.println(value);
}
}
private final Function<T, T> computeNext;
private final T first;
public LinkedIterable(final T first, final Function<T, T> computeNext) {
this.first = first;
this.computeNext = computeNext;
}
#Override public Iterator<T> iterator() {
return new AbstractLinkedIterator<T>(first) {
#Override protected T computeNext(final T previous) {
return computeNext.apply(previous);
}
};
}
}
However this approach makes special constraints on the provided function returning null.
This question already has answers here:
Why is a ConcurrentModificationException thrown and how to debug it
(8 answers)
Closed 3 years ago.
I encountered ConcurrentModificationException and by looking at it I can't see the reason why it's happening; the area throwing the exception and all the places modifying the collection are surrounded by
synchronized (this.locks.get(id)) {
...
} // locks is a HashMap<String, Object>;
I tried to catch the the pesky thread but all I could nail (by setting a breakpoint in the exception) is that the throwing thread owns the monitor while the other thread (there are two threads in the program) sleeps.
How should I proceed? What do you usually do when you encounter similar threading issues?
It may have nothing to do with the synchronization block. ConcurrentModificationExceptions often occur when you're modifying a collection while you are iterating over its elements.
List<String> messages = ...;
for (String message : messages) {
// Prone to ConcurrentModificationException
messages.add("A COMPLETELY NEW MESSAGE");
}
Similar to a previous post, you can get the same issue if you delete an entry.
e.g.
for(String message : messages) {
if (condition(message))
messages.remove(message);
}
Another common example is cleaning up a Map.
This particular problem can be resolved using an Iterator explicitly.
for(Iterator<String> iter = messages.iterator(); iter.hasNext();) {
String message = iter.next();
if (condition(message))
iter.remove(); // doesn't cause a ConcurrentModificationException
}
Sometime your application may be complex too complex and some functions may have too much side effect. Also, maybe another thread is really doing something wrong with that list and you can't find where easily.
For my own problem, I've write my own list system that delegates another list and, once locked, all other modifications throws ConcurrentModificationException, so the bad modification instruction will get at output with the exception. It can also detect errors described above.
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: francoiscassistat
* Date: 12 juin 2010
* Time: 18:20:18
*
*
* Lockable list, made to debug ConcurrentModificationException on Lists.
* The lock can be switched on/off with setLocked(boolean).
* When locked, all write access to the list or iterators gets ConcurrentModificationException.
* Simple usage case :
*
* list.setLocked(true);
*
* for (Object o : list.iterator()) // now this won't get ConcurrentModificationException, the other instruction that cause this will thrown the exception
* { ... }
*
* list.setLocked(false);
*/
public class LockableList<E> implements List<E> {
protected class LockableListIterator implements Iterator<E> {
protected Iterator<E> iterator;
public LockableListIterator(Iterator<E> iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
return iterator.hasNext();
}
public E next() {
return iterator.next();
}
public void remove() {
checkLock();
iterator.remove();
}
}
protected class LockableListListIterator implements ListIterator<E> {
protected ListIterator<E> listIterator;
public LockableListListIterator(ListIterator<E> listIterator) {
this.listIterator = listIterator;
}
public boolean hasNext() {
return listIterator.hasNext();
}
public E next() {
return listIterator.next();
}
public boolean hasPrevious() {
return listIterator.hasPrevious();
}
public E previous() {
return listIterator.previous();
}
public int nextIndex() {
return listIterator.nextIndex();
}
public int previousIndex() {
return listIterator.previousIndex();
}
public void remove() {
checkLock();
listIterator.remove();
}
public void set(E e) {
checkLock();
listIterator.set(e);
}
public void add(E e) {
checkLock();
listIterator.add(e);
}
}
protected class LockableListSubList implements List<E>
{
protected List<E> list;
public LockableListSubList(List<E> list) {
this.list = list;
}
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean contains(Object o) {
return list.contains(o);
}
public Iterator<E> iterator() {
return new LockableListIterator(list.iterator());
}
public Object[] toArray() {
return list.toArray();
}
public <T> T[] toArray(T[] a) {
return list.toArray(a);
}
public boolean add(E e) {
checkLock();
return list.add(e);
}
public boolean remove(Object o) {
checkLock();
return list.remove(o);
}
public boolean containsAll(Collection<?> c) {
return list.containsAll(c);
}
public boolean addAll(Collection<? extends E> c) {
checkLock();
return list.addAll(c);
}
public boolean addAll(int index, Collection<? extends E> c) {
checkLock();
return list.addAll(index, c);
}
public boolean removeAll(Collection<?> c) {
checkLock();
return list.removeAll(c);
}
public boolean retainAll(Collection<?> c) {
checkLock();
return list.retainAll(c);
}
public void clear() {
checkLock();
list.clear();
}
#Override
public boolean equals(Object o) {
return list.equals(o);
}
#Override
public int hashCode() {
return list.hashCode();
}
public E get(int index) {
return list.get(index);
}
public E set(int index, E element) {
checkLock();
return list.set(index, element);
}
public void add(int index, E element) {
checkLock();
list.add(index, element);
}
public E remove(int index) {
checkLock();
return list.remove(index);
}
public int indexOf(Object o) {
return list.indexOf(o);
}
public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
}
public ListIterator<E> listIterator() {
return new LockableListListIterator(list.listIterator());
}
public ListIterator<E> listIterator(int index) {
return new LockableListListIterator(list.listIterator(index));
}
public List<E> subList(int fromIndex, int toIndex) {
return new LockableListSubList(list.subList(fromIndex, toIndex));
}
}
protected List<E> list;
protected boolean locked;
public LockableList(List<E> list) {
this.list = list;
locked = false;
}
public boolean isLocked() {
return locked;
}
public void setLocked(boolean locked) {
this.locked = locked;
}
protected void checkLock() {
if (locked)
throw new ConcurrentModificationException("Locked");
}
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean contains(Object o) {
return list.contains(o);
}
public Iterator<E> iterator() {
return new LockableListIterator(list.iterator());
}
public Object[] toArray() {
return list.toArray();
}
public <T> T[] toArray(T[] a) {
return list.toArray(a);
}
public boolean add(E e) {
checkLock();
return list.add(e);
}
public boolean remove(Object o) {
checkLock();
return list.remove(o);
}
public boolean containsAll(Collection<?> c) {
return list.containsAll(c);
}
public boolean addAll(Collection<? extends E> c) {
checkLock();
return list.addAll(c);
}
public boolean addAll(int index, Collection<? extends E> c) {
checkLock();
return list.addAll(index, c);
}
public boolean removeAll(Collection<?> c) {
checkLock();
return list.removeAll(c);
}
public boolean retainAll(Collection<?> c) {
checkLock();
return list.retainAll(c);
}
public void clear() {
checkLock();
list.clear();
}
#Override
public boolean equals(Object o) {
return list.equals(o);
}
#Override
public int hashCode() {
return list.hashCode();
}
public E get(int index) {
return list.get(index);
}
public E set(int index, E element) {
checkLock();
return list.set(index, element);
}
public void add(int index, E element) {
checkLock();
list.add(index, element);
}
public E remove(int index) {
checkLock();
return list.remove(index);
}
public int indexOf(Object o) {
return list.indexOf(o);
}
public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
}
public ListIterator<E> listIterator() {
return new LockableListListIterator(list.listIterator());
}
public ListIterator<E> listIterator(int index) {
return new LockableListListIterator(list.listIterator(index));
}
public List<E> subList(int fromIndex, int toIndex) {
return new LockableListSubList(list.subList(fromIndex, toIndex));
}
}
Simply use it like this :
List list = new LockableList(new ArrayList(...));
list.setLocked(true);
for (E e : list.iterator())
{ ... }
list.setLocked(false);
Hope it may help someone else.
if you need to delete few elements from your list. You can maintain another list like elements to be removed. And finally call removeAll(collection). Of course this is not good for huge data.
Having had to deal with similar issues I wrote a small helper to debug concurrent access situations on certain objects (sometimes using a debugger modifies the runtime behavior so much that the issue does not occur). The approach is similar to the one Francois showed, but a bit more generic. Maybe it helps someone: https://github.com/smurf667/kongcurrent
It's common to receive a ConcurrentModificationException when modifying a dynamic list while iterating over it (in a foreach-loop for example). You may want to make sure you're not doing that anywhere.