Java 我的array.sort使我';我在netbeans中使用,isn';t排序
我对Java很陌生,最近我想多练习。所以我在这件事上绊倒了。我想使用array.sort打印出数组中的所有值,但得到的结果是:Java 我的array.sort使我';我在netbeans中使用,isn';t排序,java,arrays,sorting,Java,Arrays,Sorting,我对Java很陌生,最近我想多练习。所以我在这件事上绊倒了。我想使用array.sort打印出数组中的所有值,但得到的结果是:1,2,3,4,5,6而不是22,51,67,12,98,34。 代码如下: public static void main(String[] args) { int[] array; array =new int[6]; array[0]=22; array[1]=51;
1,2,3,4,5,6
而不是<代码>22,51,67,12,98,34。
代码如下:
public static void main(String[] args) {
int[] array;
array =new int[6];
array[0]=22;
array[1]=51;
array[2]=67;
array[3]=12;
array[4]=98;
array[5]=34;
Arrays.sort(array);
int i;
for (i=0; i < array.length; i++){
array[i]= i+1;
System.out.println("num is"+array[i]);
}
publicstaticvoidmain(字符串[]args){
int[]数组;
数组=新整数[6];
数组[0]=22;
数组[1]=51;
数组[2]=67;
数组[3]=12;
数组[4]=98;
数组[5]=34;
数组。排序(数组);
int i;
对于(i=0;i
您正在为循环的内重新填充数组[i]
中的元素。只需打印数组的内容:
for (i=0; i < array.length; i++){
//remove this line since it's setting the value of (i+1) to array[i]
//array[i]= i+1;
//leave this line only
System.out.println("num is"+array[i]);
}
您可以始终使用自己的代码,例如:
private void butgoActionPerformed(java.awt.event.ActionEvent evt) {
int nums[] = {13,6, 1, 25,18,12};
//Array is called "nums", holds random numbers and such
int place = 0;
int check = 0;
while (place < nums.length - 1) {
while (check < nums.length) {
// "Check" loops inside place and checks to see where larger
if (nums[place] > nums[check]) {
//Change To < For Descending
int temp = nums[place];
nums[place] = nums[check];
//"Check" swaps the values around
nums[check] = temp;
}
check++;
}
//"Place" tells loop when to stop and holds a value to be compared
place++;
check = place + 1;
}
place = 0;
//"Place" acts as a counter
while (place < nums.length) {
//Output Loop
txtout.append("\n" + nums[place] + "");
//"txtoutput" is the textarea (.append means to add to the text already there
place++;
//"\n" means new line
}
}
private void butgoActionPerformed(java.awt.event.ActionEvent evt){
int nums[]={13,6,1,25,18,12};
//数组称为“nums”,包含随机数等
int place=0;
整数检查=0;
同时(位置nums[check]){
//更改为<以进行下降
int temp=nums[地点];
nums[地点]=nums[检查];
//“检查”交换周围的值
nums[检查]=温度;
}
check++;
}
//“Place”告诉循环何时停止并保存要比较的值
place++;
检查=位置+1;
}
地点=0;
//“地点”充当计数器
而(位置
只是想让您知道,这与netbeans无关。这……并不能真正回答问题。
private void butgoActionPerformed(java.awt.event.ActionEvent evt) {
int nums[] = {13,6, 1, 25,18,12};
//Array is called "nums", holds random numbers and such
int place = 0;
int check = 0;
while (place < nums.length - 1) {
while (check < nums.length) {
// "Check" loops inside place and checks to see where larger
if (nums[place] > nums[check]) {
//Change To < For Descending
int temp = nums[place];
nums[place] = nums[check];
//"Check" swaps the values around
nums[check] = temp;
}
check++;
}
//"Place" tells loop when to stop and holds a value to be compared
place++;
check = place + 1;
}
place = 0;
//"Place" acts as a counter
while (place < nums.length) {
//Output Loop
txtout.append("\n" + nums[place] + "");
//"txtoutput" is the textarea (.append means to add to the text already there
place++;
//"\n" means new line
}
}