title: Printing elements of a 2D array with columns and rows
body: <p>Im new to java and I am trying to print elements I got from a selection sort algorithm both by columns and by rows.
  The expected output for an unsorted matrix can be by rows
  486                             123                  147
  927                     to      456 (rows)     or    258 (columns)
  135 (unsorted)                  789                  369
  Thanks in advance! Please no professional solutions since I am a student. </p>
  
  <pre><code>public void sort(boolean byColumn)
  {
      double[] temp = null;
      double placeHolder = 0.0;
      int total = 0;
      int index = 0;
  
      /* Incrementing the total to use for the length of the temporary array. */
      for (int rows = 0; rows &lt; mdArray.length; rows++) 
      {
          for (int columns = 0; columns &lt; mdArray[rows].length; columns++)
          {
              total++;
          }
      }
  
      temp = new double[total];
  
      /* This nested for loop converts the indices of mdArray to one long temp array. */
      for (int rows = 0; rows &lt; mdArray.length; rows++) 
      {
          for (int columns = 0; columns &lt; mdArray[rows].length; columns++)
          {
              temp[index] = mdArray[rows][columns];
              index++;
          }
      }
  
      for (int i = 0; i &lt; temp.length; i++) // Sorting the temporary array using selection sort. 
      {
          for (int j = i + 1; j &lt; temp.length; j++)
          {
              if (temp[j] &lt; temp[i])
              {
                  placeHolder = temp[i];
                  temp[i] = temp[j];
                  temp[j] = placeHolder;
              }
          }
      }
      index = 0;
  
      if (byColumn) // Sorting the invoking array by column.
      {
          for (int rows = 0; rows &lt; mdArray.length; rows++) // Puts sorted elements back into mdArray.
          {
              for (int columns = 0; columns &lt; mdArray[rows].length; columns++)
              {
                  mdArray[rows][columns] = temp[index];
                  index++;
              }
          }
  
          for (int rows = 0; rows &lt; mdArray.length; rows++)
          {
              System.out.println();
              for (int columns = 0; columns &lt; mdArray[rows].length; columns++)
              {
                  System.out.print(mdArray[rows][columns] + " ");
              }
          }
      }
  
      else if (!byColumn) // Sorting the invoking by row.
      {
          for (int columns = 0; columns &lt; mdArray[columns].length; columns++)
          {
              for (int rows = 0; rows &lt; mdArray.length; rows++)
              {
                  mdArray[columns][rows] = temp[index];
                  index++;
              }
          }
  
          for (int rows = 0; rows &lt; mdArray.length; rows++)
          {
              System.out.println();
              for (int columns = 0; columns &lt; mdArray[rows].length; columns++)
              {
                  System.out.print(mdArray[rows][columns] + " ");
              }
          }
      }
  
      else if (byColumn &amp;&amp; isRagged()) // 
          System.out.println("Cannot sort ragged arrays by column.");
  
  
  
  }
  </code></pre>