14 października 2012

This confusing Java...

Today evening I was playing around with Java and collections. After a while I finished with a funny code.
public class Tests {

 public static void test1() {

  Collection<Integer> list = new ArrayList<Integer>();

  for (int i = 1; i < 5; i++) {
   list.add(i);
  }

  for (int i = 1; i < 3; i++) {
   list.remove(i);
  }

  System.out.println("Test 1: " + list);
 }

 
 public static void test2() {

  Collection<Integer> list = new ArrayList<Integer>();

  for (int i = 1; i < 5; i++) {
   list.add(i);
  }

  for (int i = 1; i < 3; i++) {
   list.remove(Integer.valueOf(i));
  }

  System.out.println("Test 2: " + list);
 }


 public static void test3() {

  List<Integer> list = new ArrayList<Integer>();

  for (int i = 1; i < 5; i++) {
   list.add(i);
  }

  for (int i = 1; i < 3; i++) {
   list.remove(i);
  }

  System.out.println("Test 3: " + list);
 }


 public static void test4() {

  List<Integer> list = new ArrayList<Integer>();

  for (int i = 1; i < 5; i++) {
   list.add(i);
  }

  for (int i = 1; i < 3; i++) {
   list.remove(Integer.valueOf(i));
  }

  System.out.println("Test 4: " + list);
 }


 public static void main(String[] args) {

  test1();
  test2();
  test3();
  test4();
 }
}
Why this code is so funny ? Well, it is pretty simple. However an output of this code is not obvious. I think that everyone can use this code to check a knowledge about Java. I suggest to read it carefully, write somewhere expected answers and then run the code to check how well you know Java collections framework. Have fun ! :)

Brak komentarzy:

Prześlij komentarz