gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:23:12 32 4
gpt4 key购买 nike

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

shuffle() 函数代码在 c++ 和 Java 中都在正下方!

这是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);
}
}

这是 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);
*/

}

最佳答案

试试这个方法,它会在 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);

关于java - 如何将C++中的反向函数代码转换成Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30029060/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com