2016-04-26 10 views
0

Ich habe ein Array von zufälligen ganzen Zahlen, die ich durch den Knopfdruck produziere. Ich versuche, die Zahlen Zeile für Zeile in ein Textfeld auszugeben, aber im Moment hat mein Code sie nur durch ein Leerzeichen getrennt. Ich weiß, dass "\ n" den Text in die nächste Zeile verschiebt, obwohl ich mir nicht sicher bin, wie ich dies in ein Array einbauen kann. Wie Sie im folgenden Code sehen können, ist es der SnOutput und der OnOutput, für den ich dieses Format verwenden muss.Wie listet man ein Array zeilenweise auf einer JTextArea auf?

Wenn jemand mir ein Feedback geben kann, würde es sehr geschätzt werden!

Dank

Hier ist mein Code (Beachten Sie bitte, dass es in vielen anderen Bereichen unvollendet ist):

ArrayList<Integer> list = new ArrayList<Integer>(); 

public void selectionSort(Integer[] a) { 
    for(int i = 0; i < a.length; i++) { 
     int smallestValue = a[i]; 
     int smallestIndex = i; 
     if(ascButton.isSelected()){ 
      for(int j = i+1; j < a.length; j++) { 
       if (smallestValue > a[j]) { 
        smallestValue = a[j]; 
        smallestIndex = j; 
       } 
      } 
     a[smallestIndex] = a[i]; 
     a[i] = smallestValue; 
     } else if(desButton.isSelected()){ 
      for(int j = i+1; j < a.length; j++) { 
       if (smallestValue < a[j]) { 
        smallestValue = a[j]; 
        smallestIndex = j; 
       } 
     } 
     a[smallestIndex] = a[i]; 
     a[i] = smallestValue; 
     } 
    } 
} 


public void bubbleSort(Integer[] a) { 

    int temp; 

    for (int i = a.length - 1; i > 0; i--) { 
     if(ascButton.isSelected()) { 
      for(int j = 0; j < i; j++) { 
      if(a[j] > a[j + 1]) { 
       temp = a[j]; 
       a[j] = a[j + 1]; 
       a[j + 1] = temp; 
      } 
     } 
     } else if(desButton.isSelected()) { 
      for(int j = 0; j < i; j++) { 
      if(a[j] < a[j + 1]) { 
       temp = a[j]; 
       a[j] = a[j + 1]; 
       a[j + 1] = temp; 
      } 
     } 
     } 

    } 
} 

public void insertionSort(Integer[] a) { 
    for(int i = 1; i < a.length; i++) { 
     int temp = a[i]; 
     int j = i - 1; 

     if(ascButton.isSelected()) { 
      while(j >= 0 && a[j] > temp) { 
      a[j + 1] = a[j]; 
      j--; 
     } 
     a[j + 1] = temp; 
     } else if(desButton.isSelected()) { 
      while(j >= 0 && a[j] < temp) { 
      a[j + 1] = a[j]; 
      j--; 
     } 
     a[j + 1] = temp; 
     } 

    } 
} 

public void quickSort(Integer[] a, int left, int right) { 
    int i = left; 
    int j = right; 
    int temp; 
    int pivot = a[(left + right)/2]; 
    while(i <= j) { 
    if(ascButton.isSelected()) { 

     while(a[i] < pivot) 
      i++; 
     while(a[j] > pivot) 
      j--; 
    } else if(desButton.isSelected()) { 
     while(a[i] > pivot) 
      i++; 
     while(a[j] < pivot) 
      j--; 
    } 
     if(i <= j) { 
      temp = a[i]; 
      a[i] = a[j]; 
      a[j] = temp; 
      i++; 
      j--; 
     } 
    } 
    if(left < j) { 
     quickSort(a,left,j); 
    } 
    if(i < right) { 
     quickSort(a, i, right); 
    }    
} 

    public void start() { 
     if(tenButton.isSelected()) { 
      int times = 10; 
      for (int i = 0; i < times; i++) { 
       list.add(new Random().nextInt(20000) - 10000); 
     }  
    } else if(hundButton.isSelected()) { 
      int times = 100; 
      for (int i = 0; i < times; i++) { 
       list.add(new Random().nextInt(20000) - 10000); 
     } 
    } else if(thouButton.isSelected()) { 
      int times = 1000; 
      for (int i = 0; i < times; i++) { 
       list.add(new Random().nextInt(20000) - 10000); 
     } 
    } else if(fiveButton.isSelected()) { 
      int times = 5000; 
      for (int i = 0; i < times; i++) { 
       list.add(new Random().nextInt(20000) - 10000); 
     } 
    } 
} 

public static String arrayToString(Integer[] a) { 
    String result = ""; 
    for (int v : a) { 
     result += v + " "; 
    } 
    return result; 
} 

private void sortButtonActionPerformed(java.awt.event.ActionEvent evt) {           

    start(); 
    Integer[] array = list.toArray(new Integer[0]); 
    onOutput.setText(arrayToString(array)); 

    String loop = "Number of times the loop was executed: "; 
    String comp = "Number of times a comparison was made: "; 
    String value = "Number of times a value was shifted: "; 
    String milli = "Number of milliseconds to complete the sort: "; 

    String results = "Selection Sort:" + "\n" + loop + "\n" + comp + "\n" + value + "\n" + milli + "\n" + "\n" 
      + "Bubble Sort: " + "\n" + loop + "\n" + comp + "\n" + value + "\n" + milli + "\n" + "\n" 
      + "Insertion Sort: " + "\n" + loop + "\n" + comp + "\n" + value + "\n" + milli + "\n" + "\n" 
      + "Quick Sort: " + "\n" + loop + "\n" + comp + "\n" + value + "\n" + milli + "\n" + "\n"; 

    if(tenButton.isSelected()) { 
     if(ascButton.isSelected()) { 
      selectionSort(array); 
      bubbleSort(array); 
      insertionSort(array); 
      quickSort(array, 0, list.size()-1); 
      snOutput.setText(arrayToString(array)); 
      resultsOutput.setText(results); 
     } else if (desButton.isSelected()) { 
      selectionSort(array); 
      bubbleSort(array); 
      insertionSort(array); 
      quickSort(array, 0, list.size()-1); 
      snOutput.setText(arrayToString(array)); 
      resultsOutput.setText(results); 
     } else { 
      infoOutput.setText("Please selected a Sort Order!"); 
     } 
    } else if(hundButton.isSelected()) { 
     if(ascButton.isSelected()) { 
      selectionSort(array); 
      bubbleSort(array); 
      insertionSort(array); 
      quickSort(array, 0, list.size()-1); 
      snOutput.setText(arrayToString(array)); 
      resultsOutput.setText(results); 
     } else if (desButton.isSelected()) { 
      selectionSort(array); 
      bubbleSort(array); 
      insertionSort(array); 
      quickSort(array, 0, list.size()-1); 
      snOutput.setText(arrayToString(array)); 
      resultsOutput.setText(results); 
     } else { 
      infoOutput.setText("Please selected a Sort Order!"); 
     } 
    } else if(thouButton.isSelected()) { 
     if(ascButton.isSelected()) { 
      selectionSort(array); 
      bubbleSort(array); 
      insertionSort(array); 
      quickSort(array, 0, list.size()-1); 
      snOutput.setText(arrayToString(array)); 
      resultsOutput.setText(results); 
     } else if (desButton.isSelected()) { 
      selectionSort(array); 
      bubbleSort(array); 
      insertionSort(array); 
      quickSort(array, 0, list.size()-1); 
      snOutput.setText(arrayToString(array)); 
      resultsOutput.setText(results); 
     } else { 
      infoOutput.setText("Please selected a Sort Order!"); 
     } 
    } else if(fiveButton.isSelected()) { 
     if(ascButton.isSelected()) { 
      selectionSort(array); 
      bubbleSort(array); 
      insertionSort(array); 
      quickSort(array, 0, list.size()-1); 
      snOutput.setText(arrayToString(array)); 
      resultsOutput.setText(results); 
     } else if (desButton.isSelected()) { 
      selectionSort(array); 
      bubbleSort(array); 
      insertionSort(array); 
      quickSort(array, 0, list.size()-1); 
      snOutput.setText(arrayToString(array)); 
      resultsOutput.setText(results); 
     } else { 
      infoOutput.setText("Please selected a Sort Order!"); 
     } 
    } 

} 
+0

Sie können den Begrenzer "\ n" verwenden, um Ihrer Integer-Liste beizutreten (oder .toString() hinzuzufügen). http://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array-elements-with-a-separator-the-opposite-of-sp- – Tin

Antwort

0

Ändern Sie den Code der Methode arrayToString() wie folgt:

public static String arrayToString(Integer[] a) { 
    String result = ""; 
    for (int v : a) { 
     result += v + "\n"; 
    } 
    return result; 
} 

Dadurch wird die Escape-Sequenz "newline" zu allen Ihren Ints hinzugefügt, wodurch sie in separaten Zeilen erscheinen.

Verwandte Themen