在其他地方你可能经常会看到一些关于java的数组排序,大多数写的都是同一种数据类型的数组排序,例如统一为整数的数组排序、统一为小数的数组排序,但是在实际开发中,这类排序是不能满足于我们的日常需求的。
今天就分享一下我个人采用的数组排序做法:
- 满足整数(int)、小数(double、float)的排序
- 可以满足于一维、二维、多维数组排序
- 封装类,随调随用
先新建一个包含主方法的类:Main.java
package com.ldw.arrayAlgorithm;
public class Main
{
public static void main(String[] args)
{
Object[] arr_one = {10.2,32,21.73,15,66.0,30};
System.out.println("排序前的数组:"+outsortArray(arr_one));
Array_sort arr_sort = new Array_sort();
Object[] reArr = arr_sort.arrSortOne(arr_one);
System.out.println("排序后的数组:"+outsortArray(reArr));
Object[][] rearr2= {{"小明",2},{"tom",0.5},{"Bucky",11},{"小李",8.8},{"老帅",10.12,999},{"Berry",5.0}};
System.out.println("排序前的数组:"+outsortArray_2(rearr2));
System.out.println("排序后的数组:"+outsortArray_2(arr_sort.arrSortTwo(rearr2,1)));
//测试数组(一维)
/*int[] ary = {21,10,23,42,11,35,66};
Array_sort arr_test = new Array_sort(arr_one);
System.out.println(arr_test.arrSortTest(ary));*/
}
/**
* 输出自定义数组文本
* @param arrObj
* @return
*/
public static String outsortArray(Object[] arrObj) {
String arrStr = "[";
for (int i = 0; i < arrObj.length; i++) {
if(i != arrObj.length-1) {
arrStr+=arrObj[i]+", ";
}else {
arrStr+=arrObj[i];
}
}
arrStr+="]";
return arrStr;
}
/**
* 输出自定义数组文本(二维)
* @param arrObj
* @return
*/
public static String outsortArray_2(Object[][] arrObj) {
String arrStr = "[ [";
for(int i = 0;i < arrObj.length; i++) {
for (int j = 0; j < arrObj[i].length; j++) {
if(j != arrObj[i].length-1) {
arrStr+=arrObj[i][j]+", ";
}else {
arrStr+=arrObj[i][j];
}
}
if(i != arrObj.length-1) {
arrStr+="], [";
}else {
arrStr+="] ";
}
}
arrStr+="]";
return arrStr;
}
}
然后新建数组排序(冒泡算法)封装类:Array_sort.java
package com.ldw.arrayAlgorithm;
public class Array_sort
{
//定义一维数组变量
private Object[] one;
//定义临时存放数据类型的一维数组
private String[] one_temp;
//定义二维数组变量
private Object[][] two;
//定义当前类
Array_sort that = this;
/**
* 构造函数
*/
public Array_sort() {
}
public Array_sort(Object[] one){
this.one = one;
//System.out.println(this.one);
}
public Array_sort(Object[] one,Object[][] two){
that.one = one;
that.two = two;
}
/**
* 一维数组排序函数
* @return
*/
public Object[] arrSortOne(){
//动态为临时数组实例长度
that.one_temp = new String[that.one.length];
for(int a=0;a<that.one.length;a++) {
if(that.one[a] instanceof Integer) {
that.one_temp[a] = "int";
}else if(that.one[a] instanceof Byte) {
that.one_temp[a] = "byte";
}else if(that.one[a] instanceof Short) {
that.one_temp[a] = "short";
}else if(that.one[a] instanceof Long) {
that.one_temp[a] = "long";
}else if(that.one[a] instanceof Float) {
that.one_temp[a] = "float";
}else if(that.one[a] instanceof Double) {
that.one_temp[a] = "double";
}
//将所有元素强转为double类型
that.one[a] = Double.parseDouble(that.one[a].toString());
}
//冒泡算法排序
for(int i=0;i<that.one.length;i++){
for(int j=i+1;j<that.one.length;j++){
//System.out.println(that.one[i].getClass());
//System.out.println(Double.parseDouble(that.one[i].toString()));
double front = Double.parseDouble(that.one[i].toString());
double behind = Double.parseDouble(that.one[j].toString());
if (front<behind) {
//交换一维数组位置
double temp = behind;
that.one[j]=front;
that.one[i]=temp;
//交换临时一维数组位置
String temps = that.one_temp[i];
that.one_temp[i] = that.one_temp[j];
that.one_temp[j] = temps;
}
}
}
for(int x=0;x<that.one.length;x++){
switch (that.one_temp[x]) {
case "int":
/*int xx = Integer.parseInt(new java.text.DecimalFormat("0").format(that.one[x]));
that.one[x] = xx;*/
//System.out.println(that.one[x].getClass());
/*double xx = (double) that.one[x];
that.one[x] = (new Double(xx)).intValue();*/
if(that.one[x] instanceof Double) {
double xx = (double) that.one[x];
that.one[x] = (new Double(xx)).intValue();
}else if(that.one[x] instanceof Integer){
that.one[x] = that.one[x];
}
break;
case "byte":
that.one[x] = (byte) that.one[x];
break;
case "short":
that.one[x] = (short) that.one[x];
break;
case "long":
that.one[x] = (long) that.one[x];
break;
case "float":
that.one[x] = (float) that.one[x];
break;
case "double":
if(that.one[x] instanceof Integer) {
that.one[x] = Double.valueOf(that.one[x].toString());
}else if(that.one[x] instanceof Double){
that.one[x] = that.one[x];
}
break;
}
}
return that.one;
}
/**
* 一维数组排序(重载)
* @param array
* @return
*/
public Object[] arrSortOne(Object[] array){
that.one = array;
Object[] resultSort = this.arrSortOne();
return resultSort;
}
/**
* 二维数组排序
* @param index 指定排序索引依据
* @return
*/
public Object[][] arrSortTwo(int index) {
//动态为临时数组实例长度
that.one_temp = new String[that.two.length];
for(int a=0;a<that.two.length;a++) {
if(that.two[a][index] instanceof Integer) {
that.one_temp[a] = "int";
}else if(that.two[a][index] instanceof Byte) {
that.one_temp[a] = "byte";
}else if(that.two[a][index] instanceof Short) {
that.one_temp[a] = "short";
}else if(that.two[a][index] instanceof Long) {
that.one_temp[a] = "long";
}else if(that.two[a][index] instanceof Float) {
that.one_temp[a] = "float";
}else if(that.two[a][index] instanceof Double) {
that.one_temp[a] = "double";
}
//将所有元素强转为double类型
that.two[a][index] = Double.parseDouble(that.two[a][index].toString());
}
//冒泡算法排序
for(int i=0;i<that.two.length;i++){
for(int j=i+1;j<that.two.length;j++){
//System.out.println(that.one[i].getClass());
//System.out.println(Double.parseDouble(that.one[i].toString()));
double front = Double.parseDouble(that.two[i][index].toString());
double behind = Double.parseDouble(that.two[j][index].toString());
if (front<behind) {
//交换二维数组位置
Object[] temp = that.two[j];
that.two[j]=that.two[i];
that.two[i]= temp;
//交换临时一维数组位置
String temps = that.one_temp[i];
that.one_temp[i] = that.one_temp[j];
that.one_temp[j] = temps;
}
}
}
for(int x=0;x<that.two.length;x++){
switch (that.one_temp[x]) {
case "int":
/*int xx = Integer.parseInt(new java.text.DecimalFormat("0").format(that.one[x]));
that.one[x] = xx;*/
/*double xx = (double) that.two[x][index];
that.that.two[x][index] = (new Double(xx)).intValue();*/
if(that.two[x][index] instanceof Double) {
double xx = (double) that.two[x][index];
that.two[x][index] = (new Double(xx)).intValue();
}else if(two[x][index] instanceof Integer){
that.two[x][index] = that.two[x][index];
}
//that.two[x][index] = (int) that.two[x][index];
break;
case "byte":
that.two[x][index] = (byte) that.two[x][index];
break;
case "short":
that.two[x][index] = (short) that.two[x][index];
break;
case "long":
that.two[x][index] = (long) that.two[x][index];
break;
case "float":
that.two[x][index] = (float) that.two[x][index];
break;
case "double":
/*double xx = (double) that.two[x][index];
System.out.println(Double.valueOf(xx));*/
if(that.two[x][index] instanceof Integer) {
that.two[x][index] = Double.valueOf(that.two[x][index].toString());
}else if(that.two[x][index] instanceof Double) {
that.two[x][index] = that.two[x][index];
}
//that.two[x][index] = (double) that.two[x][index];
break;
}
}
return that.two;
}
/**
* 二维数组排序(重载)
* @param array 排序二维数组
* @param index 指定排序索引依据
* @return
*/
public Object[][] arrSortTwo(Object[][] array ,int index) {
this.two = array;
Object[][] resultSort = this.arrSortTwo(index);
return resultSort;
}
//数组排序测试(int类型)
public int[] arrSortTest(int[] array){
for(int i=0;i<array.length-1;i++){
for(int j=i+1;j<array.length-1;j++){
if(array[i] < array[j]){
int temp;
temp = array[j];
array[j]=array[i];
array[j]=temp;
}
}
}
System.out.println(array);
return array;
}
}
通过运行主函数可以在控制台看到类似以下输出:
当前Array_sort.java仅对一维数组和二维数组排序进行封装,有兴趣的朋友可以继续完成多维数组的封装,原理同二维数组排序类似