title: How to list an array line by line on a JTextArea?
body: <p>I have an array of random integers which I am producing through the click of a button. I am trying to output the numbers to a textarea line by line but at the moment my code just has them separate by a space. I know that "\n" moves the text to the next line although I am not sure as to how I can incorporate this into an array. As you will be able to see in the code below, it is the snOutput and onOutput which I need to use this format for.</p>
  
  <p>If anyone is able to give me some feedback it would be greatly appreciated!</p>
  
  <p>Thanks</p>
  
  <p>Here is my code(Please keep in mind that it is unfinished in many other areas):</p>
  
  <pre><code>ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  
  public void selectionSort(Integer[] a) {
      for(int i = 0; i &lt; a.length; i++) {
          int smallestValue = a[i];
          int smallestIndex = i;
          if(ascButton.isSelected()){
              for(int j = i+1; j &lt; a.length; j++) {
                  if (smallestValue &gt; a[j]) {
                      smallestValue = a[j];
                      smallestIndex = j;
                  }
              }
          a[smallestIndex] = a[i];
          a[i] = smallestValue;
          } else if(desButton.isSelected()){
              for(int j = i+1; j &lt; a.length; j++) {
                  if (smallestValue &lt; 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 &gt; 0; i--) {
          if(ascButton.isSelected()) {
              for(int j = 0; j &lt; i; j++) {
              if(a[j] &gt; a[j + 1]) {
                  temp = a[j];
                  a[j] = a[j + 1];
                  a[j + 1] = temp;
              }
          }
          } else if(desButton.isSelected()) {
              for(int j = 0; j &lt; i; j++) {
              if(a[j] &lt; 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 &lt; a.length; i++) {
          int temp = a[i];
          int j = i - 1;
  
          if(ascButton.isSelected()) {
              while(j &gt;= 0 &amp;&amp; a[j] &gt; temp) {
              a[j + 1] = a[j];
              j--;
          }
          a[j + 1] = temp;
          } else if(desButton.isSelected()) {
              while(j &gt;= 0 &amp;&amp; a[j] &lt; 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 &lt;= j) {
      if(ascButton.isSelected()) {
  
          while(a[i] &lt; pivot)
              i++;
          while(a[j] &gt; pivot)
              j--;
      } else if(desButton.isSelected()) {
          while(a[i] &gt; pivot)
              i++;
          while(a[j] &lt; pivot)
              j--;
      }
          if(i &lt;= j) {
              temp = a[i];
              a[i] = a[j];
              a[j] = temp;
              i++;
              j--;
          }
      }
      if(left &lt; j) {
          quickSort(a,left,j);
      }  
      if(i &lt; right) {
          quickSort(a, i, right);
      }            
  }
  
      public void start() {
          if(tenButton.isSelected()) {
              int times = 10;
              for (int i = 0; i &lt; times; i++) {
                  list.add(new Random().nextInt(20000) - 10000);
          }     
      } else if(hundButton.isSelected()) {
              int times = 100;
              for (int i = 0; i &lt; times; i++) {
                  list.add(new Random().nextInt(20000) - 10000);
          }
      } else if(thouButton.isSelected()) {
              int times = 1000;
              for (int i = 0; i &lt; times; i++) {
                  list.add(new Random().nextInt(20000) - 10000);
          }
      } else if(fiveButton.isSelected()) {
              int times = 5000;
              for (int i = 0; i &lt; 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!");
          }
      }
  
  }
  </code></pre>