The WeakReference class, monitoring memory leak and garbage collection in a Java application

Image
 Below is a Stack implementation that uses an internal resizeable array structure.  public class MyStack< T > implements Stack< T > { private static final int CAPACITY = 100 ; private Object[] array ; private int pos = 0 ; public MyStack () { this . array = new Object[ CAPACITY ] ; } @Override public void push ( T item) { if ( pos >= array . length / 2 ) { Object[] newArray = new Object[ pos * 2 ] ; System. arraycopy ( array , 0 , newArray , 0 , array . length ) ; array = newArray ; } array [ pos ++] = item ; } @Override public T pop () { if (isEmpty()) { throw new RuntimeException( "empty stack" ) ; } @SuppressWarnings ( "unchecked" ) T item = ( T ) array [ pos - 1 ] ; pos -= 1 ; return item ; } @Override @SuppressWarnings ( "unchecked" ) public T peek...

More fun with left shift operator

Lets say we want to have sum of two integers. But we are not allowed to use + and - operators. Below is a way to do that:

public class Solution {

    public static int getSum(int a, int b) {

        if (a > 0 && b > 0) {
            return positiveSum(a, b);
        }

        if (a < 0 && b < 0) {
            return negate(positiveSum(Math.abs(a), Math.abs(b)));
        }

        if (a > 0 && b < 0) {
            return minus(a, Math.abs(b));
        } else {
            return minus(b, Math.abs(a));
        }
    }

    private static int minus(int a, int b) {
        boolean[] result = new boolean[32];

        boolean carry = false;

        for (int i = 0; i < 32; ++i) {

            boolean ba = ((a & (1 << i))) != 0;
            boolean bb = (b & (1 << i)) != 0;

            result[i] = ba ^ bb;

            if (carry) {
                result[i] = !result[i];
            }

            if (ba == true) {
                carry = false;
            }

            if (ba == false && bb == true) {
                carry = true;
            }

        }

        int intResult = getIntResult(result);

        return intResult;


    }

    private static int positiveSum(int a, int b) {
        boolean[] result = new boolean[32];

        boolean carry = false;

        for (int i = 0; i < 32; ++i) {

            boolean ba = ((a & (1 << i))) != 0;
            boolean bb = (b & (1 << i)) != 0;

            result[i] = ba ^ bb ^ carry;

            if (carry == true) {
                if (result[i] == true) {
                    carry = false;
                }
            }

            if ((a & (1 << i)) != 0) {
                if ((b & (1 << i)) != 0) {
                    carry = true;
                }
            }
        }

        int intResult = getIntResult(result);

        return intResult;
    }

    private static int getIntResult(boolean[] result) {
        int intResult = 0;
        for (int i = 0; i < result.length; ++i) {

            if (result[i] == true)
                intResult |= (1 << i);

        }
        return intResult;
    }



    // negate a positive value
    private static int negate(int value) {
        if (value < 0) {
            throw new RuntimeException();
        }

        return minus(0, value);
    }

 
    public static void main(String[] args) {

        System.out.println(getSum(-17, -10));
        System.out.println(getSum(17, -10));
        System.out.println(getSum(-10, 17));
        System.out.println(getSum(-15, 32));
        System.out.println(getSum(15, 32));
    }
}

Comments

Popular posts from this blog

My Crappy Looking Solution to "Binary Tree Common Ancestor" Problem

Trie Data Structure and Finding Patterns in a Collection of Words

A Graph Application in Java: Using WordNet to Find Outcast Words