package core_java;
import java.util.Scanner;
public class ExchangeArray {
/**
* @param args
*/
public static int[] split2array(String str){
String[] m = null;
m = str.split(",");
int[] a = new int[m.length];
for(int i=0;i<m.length;i++){
a[i] = Integer.parseInt(m[i]);
}
return a;
}
public static int[] exchange(int[] a){
int[] b = new int[a.length];
for(int j=0;j<a.length;j++){
b[j]=a[a.length-1-j];
}
return b;
}
public static int[] bubsort(int[] a){//冒泡排序
int[] b = a;
for (int i = 0; i < b.length; i++) {
for(int j=i;j<b.length;j++){
int temp;
if(b[i]<b[j]){
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
return b;
}
public static int[] selsort(int[] a){//选择排序
int[] b = a;
for (int i = 0; i < b.length; i++) {
int minIndex = i;//以最小值为基准
for(int j=i;j<b.length;j++){
int temp1 = b[minIndex];
int temp2 = b[j];
if(temp1>temp2){
minIndex = j;
}
}
if(minIndex!=i){
int temp;
temp = b[minIndex];
b[minIndex] = b[i];
b[i] = temp;
}
}
return b;
}
public static void print(int[] a){
for(int m=0;m<a.length-1;m++){
System.out.print(a[m]+",");
}
System.out.print(a[a.length-1]);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入序列(以逗号隔开回车结束):");
Scanner sc = new Scanner(System.in);
String ss = sc.nextLine();
String[] o = ss.split(",");
System.out.println("原序列:");
for (int i = 0; i < o.length-1; i++) {
System.out.print(o[i]+",");
}
System.out.println(o[o.length-1]);
int[] r = exchange(split2array(ss));
System.out.println("处理后的序列:");
for(int m=0;m<r.length-1;m++){
System.out.print(r[m]+",");
}
System.out.println(r[r.length-1]);
System.out.println("选择排序后的结果:");
print(selsort(r));
/*System.out.println();
System.out.println("冒泡排序后的结果:");
print(bubsort(r));*/
}
}
注意其中的异常:
数组越界异常
ArrayIndexOutOfBoundsException空指针异常
NullPointerException