我有一个随机整数数组,我通过点击一个按钮生成。我试图逐行输出数字到textarea,但目前我的代码只是用空格分隔它们。我知道“\ n”将文本移动到下一行,虽然我不确定如何将其合并到数组中。正如您将在下面的代码中看到的那样,我需要使用此格式的snOutput和onOutput。
如果有人能够给我一些反馈,我们将不胜感激!
由于
这是我的代码(请记住,在许多其他方面尚未完成):
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 :(得分:0)
修改arrayToString()
方法的代码,如下所示:
public static String arrayToString(Integer[] a) {
String result = "";
for (int v : a) {
result += v + "\n";
}
return result;
}
这会添加转义序列&#34;换行符&#34;对所有使它们出现在不同行中的整数。