working with arrays

ArrayList and Iterator Example

This example shows how to use an iterator with ArrayList. Iterator can be used with other collections.

import java.util.*;

public class ArrayListIterator {
    public static void main(String[] args) {
        ArrayList al = new ArrayList();
        al.add(new Integer(11));
        al.add(new Integer(13));
        al.add(new Integer(17));

        Iterator itr = al.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}

output

ArrayList NullPointerException

Java's ArrayList is a great Collection that frees us from IndexOutOfBound, one type limitation, and NullPointerException issues while providing several value-added methods. Naturally, you would be shocked if you encounter a NullPointerException on an ArrayList. The only time, you would encounter this problem is when you fail to initialize the ArrayList.

PHP Array functions

Passing arrays by value and reference

The difference between passing by value and passing by reference is that you when pass by value, you are passing a copy of the array. When passing by reference, you are passing a pointer i.e any changes made in the function you pass to can modify the original.

function foo($var) {
  $var[1]++;
}
function bar(&$var) {
  $var[1]++;
}

$a=array(5,6,7);
foo($a);
print "Passed by value:\n";
print_r($a);
bar($a);
print "Passed by reference:\n";
print_r($a);

output