Friday, October 06, 2006

Inserting into array – part 3

First look at this: First look at arrays
Then look at this: Inserting into arrays – part 2
The second method of inserting that we discussed in First look at arrays was the two handed “fastest draw in the west” approach where we start with the locker where we want to keep our stuff, remove its contents with our free hand and replace it with the contents of the other hand. We continue down the row until both hands are free [that is, we find an empty locker].
Let us look at the code for that.
We will start with a class called Exchanger. Like so:

public class Exchanger {
    private String leftHand = "empty";
    private String rightHand = "empty";

This class contains one left hand and one right hand. Initially both are empty.

Then, let us write a function to make us hold our stuff in our, say, left hand.

void initialize(String myStuff)
{
    leftHand = myStuff;
}

Now, we will come to the function for the exchange of contents in locker with the contents of our hand. You may want to practice this in front of the mirror.

void moveRightByOneLocker(int pos)
{
     if (leftHand.equalsIgnoreCase("empty") == true)
     {
          leftHand = arr[pos].content;
          arr[pos].content = rightHand;
          rightHand="empty";
     }
     else // rightHand is empty
     {
          rightHand = arr[pos].content;
          arr[pos].content = leftHand;
          leftHand = "empty";
     }
}

Basically, what we are doing is as follow:
If our left hand is empty, we use that hand and pick up the contents of the locker in front of us and replace the contents with what we are holding in our right hand and if our right hand is empty, we use that hand and pick up the contents of the locker in front of us and replace the contents with what we are holding in our left hand.

Finally one more function to check if we are done, that is to check if both hands are empty:

boolean areBothHandsEmpty()
{
    if ((leftHand.equalsIgnoreCase("empty")== true) && (rightHand.equalsIgnoreCase("empty")== true))
         return true;
    else
         return false;
    }
}

Our exchanger class is ready.
Now we can call it as follows:

public void insert(int pos, String myStuff)
{
     Exchanger fastDraw = new Exchanger();
     fastDraw.initialize(myStuff);
     int startPos = pos;
     while (fastDraw.areBothHandsEmpty() == false)
     {
          fastDraw.moveRightByOneLocker(startPos);
         startPos++;
    }
}

A word of caution: I am using funny variable names in an attempt to be, um..., funny. Please use variable and function names that reflect their purpose. So, basically, what I am saying is that when it comes to variable naming, “Do as I say, not as I do”. Also, I am leaving out a lot of error checking in order to concentrate on the core functionality. As we progress, we will see how we should take care of the various possible error conditions.

In the next article, we shall see how we can move the empty locker to where we want it to be. Sheer magic.

No comments: