Code Pretty Print Script

Thursday, December 12, 2013

Palindromes

How can you efficiently tell if a word is a palindrome? You could always reverse your input, and then test that reversed String against your initial input String (because they'll be equal if the input is a palindrome, by definition). Or, you could use my Stack and do this.

Thursday, November 21, 2013

A Nicer Way to Sort

When you're dealing with production code, it's nice to have a convenient sort utility method that works with Arrays and Collections.  I think you might find this class useful, especially if you can use import static!

Tuesday, November 19, 2013

Converting Strings to Wrapper Types

Are you dealing with a lot of numeric data stored in strings? Don't you wish there were some simple utility wrappers to use? Try these:

Monday, November 18, 2013

Sunday, November 17, 2013

Even-Odd Optimization

When you want to test whether something is even or odd, don't use the expensive modulo operation like this -
      
    for (int i = 0; i < 100; ++i) {
      if (i % 2 == 0) {
        System.out.println("i = " + i + " is even.");
      } else {
        System.out.println("i = " + i + " is odd.");
      }
    }
You should probably use this instead -

Saturday, November 16, 2013

Stack (Unbound)

Implementing an abstract data type like an unbounded Stack allows its' use with a two stack arbitrary precision machine. This Stack provides peek(), pop() and push().
Inspired by Dan's question on Stack Overflow.
Given this "program", the Stack will have one number at the top (i.e. 12).
( 3 * 4 ) .

Friday, November 15, 2013

Reflections

Are you working with Java Reflection? Having trouble invoking your methods through reflection? Wish there was a simple way to call an arbitrary method on an object?
Swans Reflecting Elephants - Dali
Swans Reflecting Elephants (1937) - Dali