Solved
Swapping 0 and 1s in an array
Posted on 2004-09-28
Hello,
For this assignment i'm doing for class(an interative towers of hanoi), I have an array that will contain 0 and 1s that correspond to the number of the disk that is to be moved. And my question is this:
That array, will initially be intitialized to all 1s, and then it needs to swap all 0s to 1s, and all 1s to 0s, but I can't figure out how to do it. I wrote a method but it just undoes work that it's already done, thus giving me what i start with.
here it is:
public static void bitSwap(int[] swapArray)
{
for (k=0; k < swapArray.length; k++)
{
if (swapArray[k] == 0)
{
swapArray[k] = 1;
break;
}
}
for (j=0; j < swapArray.length; j++)
{
if (swapArray[j] == 1)
{
swapArray[j] = 0;
break;
}
}
}
As you can see, that does undoes itself. I know it's EASY but i can't figure it out, any help would be appreciated.
-NeedlessKane