0

Can someone help me convert this C# function to java function?

Could i make some simple for loop instead of all these foreach?

 static Queue RadixSort(Queue Items, int Digits)
    {

        int Digit = Digits - 1;
        while (Digit >= 0)
        {
            Queue Zero = new Queue();
            Queue One = new Queue();
            Queue Two = new Queue();
            Queue Three = new Queue();
            Queue Four = new Queue();
            Queue Five = new Queue();
            Queue Six = new Queue();
            Queue Seven = new Queue();
            Queue Eight = new Queue();
            Queue Nine = new Queue();
            int UpperLimit = Items.Count;
            int counter = 1;
            while (counter <= UpperLimit)
            {
                int i = Convert.ToInt32(Items.Dequeue());
                counter++;

                switch (i.ToString().PadLeft(Digits, '0')[Digit])
                {
                    case '0':
                        Zero.Enqueue(i);
                        continue;
                    case '1':
                        One.Enqueue(i);
                        continue;
                    case '2':
                        Two.Enqueue(i);
                        continue;
                    case '3':
                        Three.Enqueue(i);
                        continue;
                    case '4':
                        Four.Enqueue(i);
                        continue;
                    case '5':
                        Five.Enqueue(i);
                        continue;
                    case '6':
                        Six.Enqueue(i);
                        continue;
                    case '7':
                        Seven.Enqueue(i);
                        continue;
                    case '8':
                        Eight.Enqueue(i);
                        continue;
                    case '9':
                        Nine.Enqueue(i);
                        continue;
                }
            }
            Items = new Queue();
            foreach (int i in Zero)
            {
                Items.Enqueue(i);
            }
            foreach (int i in One)
            {
                Items.Enqueue(i);
            }
            foreach (int i in Two)
            {
                Items.Enqueue(i);
            }
            foreach (int i in Three)
            {
                Items.Enqueue(i);
            }
            foreach (int i in Four)
            {
                Items.Enqueue(i);
            }
            foreach (int i in Five)
            {
                Items.Enqueue(i);
            }
            foreach (int i in Six)
            {
                Items.Enqueue(i);
            }
            foreach (int i in Seven)
            {
                Items.Enqueue(i);
            }
            foreach (int i in Eight)
            {
                Items.Enqueue(i);
            }
            foreach (int i in Nine)
            {
                Items.Enqueue(i);
            }
            Digit--;
        }
        return Items;
    }
6
  • 4
    Why aren't you using an array of queues? Commented Feb 18, 2011 at 2:14
  • What exactly are you trying to do? Commented Feb 18, 2011 at 2:15
  • Not my code. But need that function in my java code. Commented Feb 18, 2011 at 2:29
  • 1
    Is it just me or does this scream thedailywtf.com? Seriously though, I would not even consider porting this code. Without spending the time to really disassemble it, it looks like a totally bad design. Look into Java's collection classes and comparators. Using a bunch of queues like this to sort the contents of another queue .... crazy! Commented Feb 18, 2011 at 2:48
  • 1
    Indeed. "Wtf?" :-) Not only can you port it, but it can be done in 1/4th (or less) the lines. Commented Feb 18, 2011 at 3:08

2 Answers 2

5

In Java, you can write

for (int i : <QueueName>)
{
}

(As for cleaning up and refactoring the rest of the code, I leave that up to you :D)

Good luck, and I hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

All the rest should be pretty similar.
1
for(int Digit = Digits-1; Digit >= 0; Digit--) {
    // Your code here
}

or

int Digit = Digits-1;
while(Digit >= 0) {
    // Your code here
    Digit--;
}

When compiled, they are the same :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.