开发者

From C# to Java

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;
    }


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!


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜