2015-05-04 192 views
0

我已經在C++中完成了處理器仿真器分配,現在我正嘗試轉換Java中的整個代碼。這裏出現的問題是我們通過在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); 
    */ 

} 
+2

在Java中發佈您迄今爲止所做的工作。 – ChristofferPass

+0

我很難跟隨你想要做的事情。我知道你正在將一些C++代碼移植到Java中,但是這並不完全清楚你停滯不前。 –

+0

@AhmedAdnan:這裏我們用代碼刪除註釋,並將其置於編輯的問題。 – Qiu

回答

1

嘗試這種方法,將扭轉X在3索引範圍的順序:

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); 
+0

讓我看看它是否成功。謝謝:) –

+0

它是如何工作的? – gaRos