在 android studio 中显示每个 for 循环迭代输出
Display each for loop iteration output in android studio
我正在尝试演示通过单击按钮触发的插入排序。在输出中显示排序的所有步骤。
输入整数个位数0-9。
谁能帮我改进我的代码!
将不胜感激。
O/p 显示大括号和逗号,因为它是一个数组。
我可以不带牙套和逗号得到 o/p 吗!?
而不是 o/p [3,5,7,9] 它应该是 - 3 5 7 9
public void btnClickMe(View v) {
Button button = (Button) findViewById(R.id.button);
EditText et = (EditText) findViewById(R.id.editText);
TextView tv = (TextView) findViewById(R.id.textView2);
insertionSort();
}
public void insertionSort(){
EditText et = (EditText) findViewById(R.id.editText);
String text = et.getText().toString();
String txt1 = text.replaceAll(","," ");
String txt= txt1.replaceAll(" ","");
int[] array = new int[txt.length()];
for (int i = 0; i < txt.length(); i++){
array[i] = Character.getNumericValue(txt.charAt(i));
}
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText("Output:");
for (int j = 1; j < array.length; j++){
int key = array[j];
int i = j - 1;
while ((i > -1) && (array[i] > key)){
array[i + 1] = array[i];
i--;
}
}
array[i + 1] = key;
tv.append(Arrays.toString(array).replaceAll(",","")+"\n");
}
我可能问得太多了,但我正在努力学习 android,你的帮助会教会我很多新东西。提前致谢。
首先,您的插入算法存在缺陷。
其次,您需要在每次追加之前重置 TextView
的文本。
int[] array = {6, 4, 3, 2};
// TextView tv = (TextView) findViewById(R.id.textView2);
// Resets the text to be blank.
// tv.setText("");
for (int i = 1; i < array.length; ++i) {
int j = i;
while (j > 0 && array[j - 1] > array[j]) {
int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
--j;
}
System.out.println(Arrays.toString(array));
// i.e. [4, 6, 3, 2]
// tv.append(Arrays.toString(array) + "\n");
}
我正在尝试演示通过单击按钮触发的插入排序。在输出中显示排序的所有步骤。
输入整数个位数0-9。
谁能帮我改进我的代码! 将不胜感激。
O/p 显示大括号和逗号,因为它是一个数组。 我可以不带牙套和逗号得到 o/p 吗!? 而不是 o/p [3,5,7,9] 它应该是 - 3 5 7 9
public void btnClickMe(View v) {
Button button = (Button) findViewById(R.id.button);
EditText et = (EditText) findViewById(R.id.editText);
TextView tv = (TextView) findViewById(R.id.textView2);
insertionSort();
}
public void insertionSort(){
EditText et = (EditText) findViewById(R.id.editText);
String text = et.getText().toString();
String txt1 = text.replaceAll(","," ");
String txt= txt1.replaceAll(" ","");
int[] array = new int[txt.length()];
for (int i = 0; i < txt.length(); i++){
array[i] = Character.getNumericValue(txt.charAt(i));
}
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText("Output:");
for (int j = 1; j < array.length; j++){
int key = array[j];
int i = j - 1;
while ((i > -1) && (array[i] > key)){
array[i + 1] = array[i];
i--;
}
}
array[i + 1] = key;
tv.append(Arrays.toString(array).replaceAll(",","")+"\n");
}
我可能问得太多了,但我正在努力学习 android,你的帮助会教会我很多新东西。提前致谢。
首先,您的插入算法存在缺陷。
其次,您需要在每次追加之前重置 TextView
的文本。
int[] array = {6, 4, 3, 2};
// TextView tv = (TextView) findViewById(R.id.textView2);
// Resets the text to be blank.
// tv.setText("");
for (int i = 1; i < array.length; ++i) {
int j = i;
while (j > 0 && array[j - 1] > array[j]) {
int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
--j;
}
System.out.println(Arrays.toString(array));
// i.e. [4, 6, 3, 2]
// tv.append(Arrays.toString(array) + "\n");
}