Processing code is in fact Java so the ArrayList in P5 is the Java ArrayList.
Using iterators to iterate over an entire ArrayList is slower than accessing the elements directy with
ArrayList_variable.get(index), but unless you have very large ArrayLists or are perforimng a very large number of iterations then you will probably not notice the difference.
The reason for using iterators is because it is the easiest way to
safely remove elements from an ArrayList
while iterating over the list.
The following example deletes all 'Jack' elements (there are 2) from the list.
- List<String> aList = new ArrayList<String>();
- aList.add("Peter");
- aList.add("Jack");
- aList.add("Jill");
- aList.add("Jack");
- aList.add("Sarah");
- aList.add("Maggie");
- Iterator<String> itr = aList.iterator();
- while (itr.hasNext()) {
- String element = itr.next();
- if ("Jack".equals(element)) {
- itr.remove();
- }
- }
- println(aList);
BTW HashMap does not have itearators