Monday, May 4, 2009

esoteric programming languages

Wikipedia has an awesome article about esoteric programming languages. Only few of them is designed for being adopted for real world problems. But the majority of them are quite interesting and entertaining. Take a look at Brainfuck for instance. You won't meditate much on the naming of this programming language after seeing the Hello World example.


++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++
..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

Monday, April 27, 2009

recommendation for people with desire to throw money away: burningairlines.com

About half a year ago, I ordered bunch of t-shirts from burningairlines.com. I never got these t-shirts and neither burningairlines, nor usps did care what happened to my order. If you got loads of money to throw away, I highly recommend you to shop from burningairlines; satisfaction guaranteed.

Friday, April 24, 2009

secrets of list class part 2: xAll strikes back

I continue my series of entries about less known features of the List class and today I'm going to explain the uses of addAll(), retainAll(), removeAll() methods.
Assume that you have two Lists and you want to merge them somehow.


/* list1 consists of [1, 2, 3, 4, 5] and list2 consists of [6, 7, 8, 9]*/
/* I want to merge both lists*/
/* I can use addAll and add list2 at the end of list1*/
list1.addAll(list2);
/* if we take a look at list1 we will see*/
System.out.println(list1);
/* output is [1, 2, 3, 4, 5, 6, 7, 8, 9] */


If you take a look at the illustration above you'll see that red, green part of the sets and two blue part of the sets (elements of the intersection are used 2 times each) are obtained after addAll().


If we don't want elements of the second list to be added to the end of the first list then we can use addAll() with an index argument.


/* list1 consists of [1, 2, 3, 4, 5] and list2 consists of [6, 7, 8, 9] (again)*/
/* I want to merge both lists*/
/* but I want that list2's elements come first*/
list1.addAll(0, list2);
System.out.println(list1);
/* output is [6, 7, 8, 9, 1, 2, 3, 4, 5] */


Notice that
 list1.addAll(list2) 
and
 list1.addAll(list1.size(), list2) 
are the same thing and adding the list2 to the list1 does not effect the content of list2.

Now, it's the removeAll()'s turn. You can remove all the elements of a Collection from a List using this method.


/* list1 consists of [1, 2, 3, 4, 5] and list2 consists of [6, 7, 8, 9] (again)*/
/* I want to remove elements of list2 from list1*/
list1.removeAll(list2);
System.out.println(list1);
/* output is [1, 2, 3, 4, 5]. list1 is intact because there are no elements */
/* of list2 in list1*/




/* list1 consists of [1, 2, 3, 4, 5] and list2 consists of [3, 8, 9, 4]*/
/* I want to remove elements of list2 from list1*/
list1.removeAll(list2);
System.out.println(list1);
/* output is [1, 2, 5]. 3 and 4 which are elements of both list1 and list2 */
/* are removed from list1*/



If you look at the picture above you'll see that we took only the red part of list1 and removed the intersection from it.

The last method of my entry will be retainAll(). retainAll() retains only shared elements of list1 and list2.


/* list1 consists of [1, 2, 3, 4, 5] and list2 consists of [3, 8, 9, 4]*/
/* I want to retain only shared elements of both lists */
list1.retainAll(list2);
System.out.println(list1);
/* output is [3, 4] */



If you take a look at the picture above you'll see that we retain only the intersection which is the blue part of list1. That's all for part 2 secrets of List class part 2.

Sunday, April 19, 2009

concurrentmodificationexception or: how I learned to stop worrying and loved for-each loop as it is

Few days ago, I got a ConcurrentModificationException out of nowhere while I was iterating through a list and removing elements from it. I remember doing this thing and not getting any exception. I delved a little bit and realized that the exception is thrown because of my misuse of for-each loop (introduced in jdk 5). Its developers noted:
Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel.


For-each loop creates and hides the iterator, so when you try to remove an element using the list itself you'll get ConcurrentModificationException because you have to do the removing and adding through the iterator. So the code below will throw ConcurrentModificationException.

/*
integerList is an ArrayList
with Integer objects as elements
*/

for(Integer integer : integerList){
if(integer.intValue()%2 == 0) // remove even elements
integerList.remove(integer);
}


You can fix your error by not using for-each loop. For instance, you can define your iterator and work with it or you can iterate through the array list and do the add/remove directly from the list.

First one's code:


Iterator iterator = integerList.iterator(); // create the iterator
while(iterator.hasNext()){
Integer integer = iterator.next(); // take the element
if(integer.intValue()%2 == 0)
iterator.remove(); // remove using the iterator
}


Second approach's code:



for(int i=0 ; i < integerList.size(); i++){
Integer integer = integerList.get(i); // take the element
if(integer.intValue() %2 == 0){
// remove from the list
integerList.remove(i);
/*
decrease the index by one, so
that we won't skip any elements
after the element left-shifting which
occurs after each removal.
*/

i--;
}
}

Saturday, April 18, 2009

secrets of list class part 1: constructor tricks

List classes in Java can be thought as resizable arrays. They are very frequently used as they are handy. In this part of the article, I'll show you two constructor tricks that can become quite useful in special cases. The first trick is for using an existing list content for creating a new list. The second trick is for ensuring the element capacity of a list. You can ask yourself why are we giving a capacity value to a list if it can increase its capacity by itself. The answer is that yes, lists can increase their capacity by themselves but if we know the potential size we can skip waiting for the initial capacity adjustment and save some precious time. Here's the code for constructor tricks:



/* create two Integer objects which are wrappers for integers */

Integer integer1 = new Integer(1);
Integer integer2 = new Integer(2);

/* create an ArrayList and add both objects to list1*/

List list1 = new ArrayList();
list1.add(integer1);
list1.add(integer2);

// constructor tricks begin

/* to create a new list (list2) with elements of a list (list1)
* give list1 to the constructor of list2 while creating list2
*/

List list2 = new ArrayList(list1);

// you can see that their content are the same

System.out.println(list1);
System.out.println(list2);

/*
* we know that a large number of elements will be added to the list
* so the initial capacity of 10 will not be enough and
* the list will have to expand.
*
* if we have an idea about the potential initial size for a list
* we can ensure that the list's initial capacity will be large
* enough to handle the initial size of elements.
* */

final int potentialSize = 10000;

/* to create a list of desired capacity, give the capacity value
to its constructor */

List list3 = new ArrayList(potentialSize);

// add all elements to list3

for(int i=0 ; i < tmpinteger =" new">