Arrays常见方法案例与解析
toString源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static String toString (int [] a) { 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(", " ); } }
使用tostring与不使用的区别
1 2 3 4 5 6 7 8 9 10 11 package Learn;import java.util.Arrays;public class Test { public static void main (String[] args) { int []array={1 ,2 ,3 ,4 ,5 }; System.out.println(array); System.out.println(Arrays.toString(array)); } }
首先我们排序的对象为数组,数组在堆中存储,我们排序之后就会影响到其中的实参,位置不变,储存的顺序改变
1 2 3 4 5 6 7 8 9 10 11 12 13 package Learn;import java.util.Arrays;public class Test { public static void main (String[] args) { int []array={1 ,2 ,3 ,5 ,6 ,9 ,2 ,5 }; System.out.println(array); Arrays.sort(array); System.out.println(Arrays.toString(array)); System.out.println(array); } }
sort方法是重载的,也可以传入一个接口Comparadetor,要求实现compare方法,从而改变排序的规则,在这里debug中,我们可以发现其中会进入到二分排序方法中(如下),而二分排序中0与1会影响到最终的排序结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 private static <T> void binarySort (T[] a, int lo, int hi, int start, Comparator<? super T> c) { assert lo <= start && start <= hi; if (start == lo) start++; for ( ; start < hi; start++) { T pivot = a[start]; int left = lo; int right = start; assert left <= right; while (left < right) { int mid = (left + right) >>> 1 ; if (c.compare(pivot, a[mid]) < 0 ) right = mid; else left = mid + 1 ; } assert left = = right;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package Learn;import java.util.Arrays;import java.util.Comparator;public class Test { public static void main (String[] args) { Integer []array={5 ,2 ,3 ,5 ,6 ,9 ,2 ,5 }; Arrays.sort(array, new Comparator () { @Override public int compare (Object o1, Object o2) { Integer n1=(Integer) o1; Integer n2=(Integer) o2; return n2-n1; } }); System.out.println(Arrays.toString(array)); } }
通过二分搜索查找,要求必须排好序(而且是正序),如果没有找到,则返回-(low+1),low表示查找的目标应该在的位置。注意有时候二分查找法不一定找出第一个数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package Learn;import java.util.Arrays;import java.util.Comparator;public class Test { public static void main (String[] args) { Integer []array={5 ,2 ,3 ,5 ,6 ,9 ,2 ,5 }; Arrays.sort(array); System.out.println(Arrays.toString(array)); System.out.println(Arrays.binarySearch(array,2 )); System.out.println(Arrays.binarySearch(array,7 )); } }
当范围不超出时,进行正常的赋值,当大于时,则用null来代替
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package Learn;import java.util.Arrays;public class Test { public static void main (String[] args) { Integer []array={5 ,2 ,3 ,5 ,6 ,9 ,2 ,5 }; Integer []array2=Arrays.copyOf(array,5 ); System.out.println(Arrays.toString(array2)); Integer []array3=Arrays.copyOf(array,11 ); System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(array3)); } }
1 2 3 4 5 6 7 8 9 10 11 package Learn;import java.util.Arrays;public class Test { public static void main (String[] args) { Integer []array={5 ,2 ,3 ,5 ,6 ,9 ,2 ,5 }; Arrays.fill(array,99 ); System.out.println(Arrays.toString(array)); } }
也是包含顺序的
1 2 3 4 5 6 7 8 9 10 11 package Learn;public class Test { public static void main (String[] args) { Integer []array={5 ,2 ,3 ,5 ,6 ,9 ,2 ,5 }; Integer []array1={5 ,2 ,3 ,5 ,6 ,9 ,5 ,2 }; System.out.println(array.equals(array1)); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package Learn;import java.util.Arrays;import java.util.List;public class Test { public static void main (String[] args) { Integer []array={5 ,2 ,3 ,5 ,6 ,9 ,2 ,5 }; List list= Arrays.asList(array); System.out.println(list); System.out.println(list.getClass()); } }
练习:按照书的价格大小顺序输出 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package Learn;import java.util.Arrays;import java.util.Comparator;public class Exercise { public static void main (String[] args) { Book []books=new Book [4 ]; books[0 ]=new Book ("ho" ,45 ); books[1 ]=new Book ("h7" ,450 ); books[2 ]=new Book ("h4737" ,272 ); books[3 ]=new Book ("737" ,753 ); Arrays.sort(books, new Comparator () { @Override public int compare (Object o1, Object o2) { Book book1=(Book) o1; Book book2=(Book) o2; return book1.price-book2.price; } }); for (int i=0 ;i< books.length;i++){ System.out.println(books[i].name+books[i].price); } System.out.println(Arrays.toString(books)); } } class Book { String name; int price; public Book (String name,int price) { this .name=name; this .price=price; } @Override public String toString () { return "Book{" + "name='" + name + '\'' + ", price=" + price + '}' ; } }