如何将C ++中的反向函数代码转换为Java

时间:2020-12-26 02:01:03

I have done the processor emulator assignment in c++ , now I am trying to convert the whole code in Java. The problem that is arising here is that we are shuffling processes by using reverse function in C++. I know there is a reverse function in java as well but I don't know how to use it with the following example.

我已经用c ++完成了处理器模拟器分配,现在我正在尝试用Java转换整个代码。这里出现的问题是我们通过在C ++中使用反向函数来改变进程。我知道java中还有一个反向函数,但我不知道如何在下面的例子中使用它。

The shuffle() function code is right below both in c++ and Java!

shuffle()函数代码正好在c ++和Java下面!

This is the C++ code:

这是C ++代码:

void processor::shuffle(){

    int swapPriority;
    int swapTime;
    int swapProcessID;
    string swapProcessName;
    int end = used - 1;
    int length = used - 1;
    for (int counter = length - 1; counter>0; counter--){
        for (int index = 0; index<end; index++){
            if (priority[index]>priority[index + 1]){
                //ist work
                swapPriority = priority[index + 1];
                swapTime = timeReq[index + 1];
                swapProcessID = processId[index + 1];
                swapProcessName = processName[index + 1];
                //2nd
                priority[index + 1] = priority[index];
                timeReq[index + 1] = timeReq[index];
                processId[index + 1] = processId[index];
                processName[index + 1] = processName[index];
                //3rd
                priority[index] = swapPriority;
                timeReq[index] = swapTime;
                processId[index] = swapProcessID;
                processName[index] = swapProcessName;
            }


        }end--;

    }
    **The problem is here !!!!**
        //How to convert this below portion into Java code??
    if (priority[0]<priority[1]){
        reverse(priority, priority + length + 1);
        reverse(timeReq, timeReq + length + 1);
        reverse(processId, processId + length + 1);
        reverse(processName, processName + length + 1);
    }
}

This is the Java code:

这是Java代码:

public void shuffle() {
    int swapPriority;
    int swapTime;
    int swapProcessID;
    String swapProcessName;
    int end = used - 1;
    int length = used - 1;
    for (int counter = length - 1; counter > 0; counter--) {
        for (int index = 0; index < end; index++) {
            if (priority[index] > priority[index + 1]) {
                //1st work
                swapPriority = priority[index + 1];
                swapTime = timeReq[index + 1];
                swapProcessID = processId[index + 1];
                swapProcessName = processName[index + 1];
                //2nd
                priority[index + 1] = priority[index];
                timeReq[index + 1] = timeReq[index];
                processId[index + 1] = processId[index];
                processName[index + 1] = processName[index];
                //3rd
                priority[index] = swapPriority;
                timeReq[index] = swapTime;
                processId[index] = swapProcessID;
                processName[index] = swapProcessName;

            }

        }
        end--;

    }
    if (priority[0] < priority[1]) {



    }
    /* reverse(priority, priority + length + 1);
    reverse(timeReq, timeReq + length + 1);
    reverse(processId, processId + length + 1);
    reverse(processName, processName + length + 1);
    */

}

1 个解决方案

#1


Try this approach, it will reverse the order of X in 3 index range:

尝试这种方法,它将颠倒3个索引范围内的X的顺序:

List<String> x = new ArrayList<>();
        x.add("1");x.add("2");x.add("3");x.add("4");x.add("5");x.add("6");x.add("7");x.add("8");
Comparator<String> ro = Collections.reverseOrder();  
Collections.sort(x.subList(0, 2),ro);
Collections.sort(x.subList(2, 5),ro);
Collections.sort(x.subList(5, 8),ro);

#1


Try this approach, it will reverse the order of X in 3 index range:

尝试这种方法,它将颠倒3个索引范围内的X的顺序:

List<String> x = new ArrayList<>();
        x.add("1");x.add("2");x.add("3");x.add("4");x.add("5");x.add("6");x.add("7");x.add("8");
Comparator<String> ro = Collections.reverseOrder();  
Collections.sort(x.subList(0, 2),ro);
Collections.sort(x.subList(2, 5),ro);
Collections.sort(x.subList(5, 8),ro);