java util Aarrys 类

toString方法  产生一维数组的二维版本

public static String toString(type[] a) {  //type 是传入数据类型
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            b.append(a[i]);
            if (i == iMax)
                return b.append(']').toString();
            b.append(", ");
        }
    }

 sort对数组进行排序

String[] s = {"dfs","fef","sdf","cadf","adfs"};
 Arrays.sort(s);

sort添加Comparator接口可以实现反序排列

//public static <T> void sort(T[] a,int fromIndex, int toIndex,  Comparator<? super T> c)
//有待改进

package object;

import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Text implements Comparator {
public int compare(String arg0,String arg1)
{

return (int)arg0.charAt(0)>(int)arg1.charAt(0)?-1:1;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Text compare = new Text();
String[] s = {"dfs","zef","sdf","cadf","adfs"};
System.out.println();

Arrays.sort(s,compare);
//System.out.println(s);
for(String s1: s)
System.out.print(s1 + " ");

}
@Override
public int compare(Object o1, Object o2) {
// TODO 自动生成的方法存根
String s1 =o1.toString();
String s2 = o2.toString();
char c1 = s1.charAt(0);
char c2 = s2.charAt(0);
return (int)c1>(int)c2?-1:1;
}

}

 

posted @ 2018-12-07 20:05  江期玉  阅读(441)  评论(0编辑  收藏  举报