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.
If anyone is able to give me some feedback it would be greatly appreciated!
Thanks
Here is my code(Please keep in mind that it is unfinished in many other areas):
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!");
}
}
}