need some help understanding this code
I was just looking over this code some more (I posted about it previously; it's from a Berkeley data structures class). The code is designed to generate all permutations from 0 to n-1. I've never taken a Java class; I'm teaching myself. I have two questions about the code provided.
1. My first question has to do with this snippet:
2. My next question is about the usage of clone() in this snippet:
Thanks for any help!
1. My first question has to do with this snippet:
private void setAllPerms () {
final ArrayList<int[]> perms = new ArrayList<int[]> ();
map (new Procedure<int[]> () {
public void apply (int[] p) {
perms.add (p);
}
});
allPerms = perms.iterator ();
}First, the final keyword is used on the perms ArrayList. Next, it appears that map modifies perms. I thought that the final keyword made its variable immutable, therefore I'm confused why perms.add(p) is allowed? There is a note that says: "The perms list in setAllPerms is 'final' in order to be used inside this anonymous subtype." But I still don't understand it because in my mind the final keyword should always mean immutable.2. My next question is about the usage of clone() in this snippet:
public int[] next () {
if (! hasNext ())
throw new NoSuchElementException ();
getNext ();
return last.clone ();
}Why not just return last instead of returning a clone of last?Thanks for any help!
