?

Log in

No account? Create an account
A true gentleman leaves no puzzle unsolved - 'Twas brillig, and the slithy toves did gyre and gimble in the wabe [entries|archive|friends|userinfo]
Thomas

[ website | Beware the Jabberwock... ]
[ deviantArt | the-boggyb ]
[ FanFiction | Torkell ]
[ Tumblr | torkellr ]

Links
[Random links| BBC news | Vulture Central | Slashdot | Dangerous Prototypes | LWN | Raspberry Pi]
[Fellow blogs| a Half Empty Glass | the Broken Cube | The Music Jungle | Please remove your feet | A letter from home]
[Other haunts| Un4seen Developments | Jazz 2 Online | EmuTalk.net | Feng's shui]

A true gentleman leaves no puzzle unsolved [Monday 7th November 2011 at 10:32 pm]
Thomas

boggyb
[Tags|, , , ]
[Feeling |curiouscurious]
[Playing |The Music of the Night ~ Andrew Lloyd Webber/The Premiere Collection]

Okay, so it turns out that talismancer's post yesterday was filler. The moral balance has swung back!

As tempting as it is to fulfill the entirety of NaBloPoMo with us commenting on each other's posts, I feel like I should actually write something. So in the interests of increasing audience participation, I shall set a Java puzzler.

The puzzler consists of the following class:

import java.util.ArrayList;

public class Foo {
    private ArrayList<Object> barList = new ArrayList<Object>();

    public void fillBarList() {
        for (int i = 0; i < 10; i++) {
            Object o = new Object();
            System.out.println("Adding " + o + " at index " + i);
            barList.add(o);
        }
    }

    public void emptyBarList() {
        for (Object o : barList) {
            System.out.println("Removing " + o);
            barList.remove(o);
        }
    }

    public static void main(String[] args) {
        Foo f = new Foo();
        f.fillBarList();
        f.emptyBarList();
    }
}

Without running it (because that would be cheating and you will be mocked for it), what do you think will happen when Foo.main() is called?

Link | Previous Entry | Share | Next Entry[ 3 pennies | Penny for your thoughts? ]

Comments:
[User Picture]From: olego
Tuesday 8th November 2011 at 8:45 am (UTC)
Though I'm allergic to Java, every language should result in undefined behaviour when looping over a changing object. Possible outcomes are: (1) exception, (2) only the odd-numbered elements are removed, or (3) somehow, everything is removed.

I'm leaning towards #1, though. :)
(Reply) (Thread)
[User Picture]From: pteppic
Tuesday 8th November 2011 at 9:31 am (UTC)
Not 100% sure about Java, but in c# you would get an exception for
modifying the collection while iterating.
(Reply) (Thread)
[User Picture]From: talismancer
Tuesday 8th November 2011 at 3:32 pm (UTC)
It'll fill the bar with 10 objects, then empty them out again.

Except it won't, otherwise you wouldn't have posted it.
(Reply) (Thread)