下面程序实现冒泡排序,最后输出3 8 8 12 37 59 132 352 1236 3400
我想最后不仅输出排序好的数据,还想输出数据在数组中的本来的位置,比如3400在原数组中位置为7,应该怎么改??
public class SortDemo {
public SortDemo() {
}
public static void main(String[] args) {
SortDemo sortDemo1 = new SortDemo();
//初始化变量
int[] arrayofInts={12,8,3,59,132,1236,3400,8,352,37};
//循环整个数组
for(int i=arrayofInts.length; --i>=0;){
//循环每个数字
for(int j=0;j<i;j++){
if(arrayofInts[j]>arrayofInts[j+1]){
int temp=arrayofInts[j];
arrayofInts[j]=arrayofInts[j+1];
arrayofInts[j+1]=temp;
}
}
}
//循环整个数组
for(int i=0;i<arrayofInts.length;i++){
System.out.println("arrayofInts["+i+"]="+arrayofInts[i]); } }}
我想最后不仅输出排序好的数据,还想输出数据在数组中的本来的位置,比如3400在原数组中位置为7,应该怎么改??
public class SortDemo {
public SortDemo() {
}
public static void main(String[] args) {
SortDemo sortDemo1 = new SortDemo();
//初始化变量
int[] arrayofInts={12,8,3,59,132,1236,3400,8,352,37};
//循环整个数组
for(int i=arrayofInts.length; --i>=0;){
//循环每个数字
for(int j=0;j<i;j++){
if(arrayofInts[j]>arrayofInts[j+1]){
int temp=arrayofInts[j];
arrayofInts[j]=arrayofInts[j+1];
arrayofInts[j+1]=temp;
}
}
}
//循环整个数组
for(int i=0;i<arrayofInts.length;i++){
System.out.println("arrayofInts["+i+"]="+arrayofInts[i]); } }}
解决方案 »
- JTextPane指定换行?
- 求计算器的思路以及简短有解释带吗
- 存储stack在中是什么意思
- 双重for中的break有疑问了!
- FTP客户端中progressbar的问题
- 一个关于JSpinner显示小数位数的问题
- 为什么我的j2sdk-1_4_2_04-windows-i586-p.exe一直装不进去?
- 我的程序为什么出现这样的错误:Exception in thread "main" java.lang.NoClassDefFoundError: SimpleFrameTest
- 如何避免抛出invalid column index错误
- MySQL的JDBC问题
- 運行jar文件
- 马上就要找工作了,求几本JAVA面试教材?
// 初始化变量
int[] arrayofInts = { 12, 8, 3, 59, 132, 1236, 3400, 8, 352, 37 };
Map<Integer, Integer> position = new HashMap<Integer, Integer>();
for (int i = 0; i < arrayofInts.length; i++) {
position.put(arrayofInts[i], i);
}
// 循环整个数组
for (int i = arrayofInts.length; --i >= 0;) {
// 循环每个数字
for (int j = 0; j < i; j++) {
if (arrayofInts[j] > arrayofInts[j + 1]) {
int temp = arrayofInts[j];
arrayofInts[j] = arrayofInts[j + 1];
arrayofInts[j + 1] = temp;
}
}
}
// 循环整个数组
for (int i = 0; i < arrayofInts.length; i++) {
System.out.println("arrayofInts[" + i + "]=" + arrayofInts[i] + " at " + position.get(arrayofInts[i]));
}
}
public class SortDemo {
public SortDemo() {
}
public static void main(String[] args) {
SortDemo sortDemo1 = new SortDemo();
//初始化变量
int[] arrayofInts = {12,8,3,59,132,1236,3400,8,352,37};
int[] backUpArray = {12,8,3,59,132,1236,3400,8,352,37}; //循环整个数组
for(int i=arrayofInts.length; --i>=0;){
//循环每个数字
for(int j=0;j <i;j++){
if(arrayofInts[j]>arrayofInts[j+1]){
int temp=arrayofInts[j];
arrayofInts[j]=arrayofInts[j+1];
arrayofInts[j+1]=temp;
}
}
}
//循环整个数组
for(int i=0;i <arrayofInts.length;i++){
System.out.print("arrayofInts["+i+"]="+arrayofInts[i]);
for (int j=0; j<backUpArray.length; j++){
if (arrayofInts[i] == backUpArray[j]){
System.out.print("In former array, the index is: "+ j +"\n");
}
}
}
}
}
arrayofInts[0]=3In former array, the index is: 2
arrayofInts[1]=8In former array, the index is: 1
In former array, the index is: 7
arrayofInts[2]=8In former array, the index is: 1
In former array, the index is: 7
arrayofInts[3]=12In former array, the index is: 0
arrayofInts[4]=37In former array, the index is: 9
arrayofInts[5]=59In former array, the index is: 3
arrayofInts[6]=132In former array, the index is: 4
arrayofInts[7]=352In former array, the index is: 8
arrayofInts[8]=1236In former array, the index is: 5
arrayofInts[9]=3400In former array, the index is: 6
Map <Integer, Integer> position = new HashMap <Integer, Integer>();
---------not a statement
还是我不太会用,但是也谢谢你:)