一:Arrays类常见方法应用举例

1:toString( ) 返回数组的字符串形式

  1. public class Test00 {
  2. public static void main(String[] args) {
  3. Integer[] integers = {1,5,7,8};
  4. //遍历数组 ,返回的是字符串形式
  5. System.out.println(Arrays.toString(integers));
  6. }
  7. }

2:sort( ) 排序(自然排序和定制排序)

<1>自然排序

  1. public class Test00 {
  2. public static void main(String[] args) {
  3. Integer[] Arr2 = {5,9,1,4,7};
  4. Arrays.sort(Arr2);
  5. System.out.println("--------排序完成----------");
  6. System.out.println(Arrays.toString(Arr2));
  7. }
  8. }

<2>定制排序

  1. public class Test00 {
  2. public static void main(String[] args) {
  3. Integer[] Arr2 = {5,9,1,4,7};
  4. Arrays.sort(Arr2);
  5. Arrays.sort(Arr2, new Comparator<Integer>() {
  6. @Override
  7. public int compare(Integer o1, Integer o2) {
  8. return o2 - o1;
  9. }
  10. });
  11. System.out.println(Arrays.toString(Arr2));
  12. }
  13. }

<3>模拟排序

  1. public class Test00 {
  2. public static void main(String[] args) {
  3. int[] Arr2 = {5,9,1,4,7};
  4. bubble02(Arr2, new Comparator() {
  5. @Override
  6. public int compare(Object o1, Object o2) {
  7. int n1 = (Integer)o1;
  8. int n2 = (Integer)o2;
  9. return n2 - n1;
  10. }
  11. });
  12. Systen.out.println("-------排序结果---------")
  13. System.out.println(Arrays.toString(Arr2));
  14. }
  15. public static void bubble02(int[] arr, Comparator c) {
  16. int temp = 0;
  17. for (int i = 0; i < arr.length - 1; i++) {
  18. for (int j = 0; j < arr.length - 1 - i; j++) {
  19. //数组排序由 c.compare(arr[j], arr[j + 1])返回的值决定
  20. if (c.compare(arr[j], arr[j + 1] ) > 0) {
  21. temp = arr[j];
  22. arr[j] = arr[j + 1];
  23. arr[j + 1] = temp;
  24. }
  25. }
  26. }
  27. }
  28. }

3:binarySearch( ) 通过二分搜索法进行查找

  1. public class Test00 {
  2. public static void main(String[] args) {
  3. Integer[] integers = {1,5,7,8};
  4. //1:要求该数组是有序的. 如果该数组是无序的,不能使用 binarySearch
  5. //2:如果数组中不存在该元素
  6. // 就返回 return -(low + 1); // key not found.
  7. // (它应该在的位置的负数)
  8. System.out.println(Arrays.binarySearch(integers,8)); //3
  9. System.out.println(Arrays.binarySearch(integers,9)); //-5
  10. }
  11. }

4:copyOf 数组元素的复制 数组元素的复制

  1. public class Test00 {
  2. public static void main(String[] args) {
  3. Integer[] Arr1 = {1,5,7,8};
  4. //1. 从 arr 数组中,拷贝 arr.length 个元素到 newArr 数组中
  5. //2. 如果拷贝的长度 > arr.length 就在新数组的后面 增加 null
  6. //3. 如果拷贝长度 < 0 就抛出异常NegativeArraySizeException
  7. //4. 该方法的底层使用的是 System.arraycopy()
  8. Integer[] Arr2 = Arrays.copyOf(Arr1,4);
  9. System.out.println("-----拷贝执行完毕-------");
  10. System.out.println(Arrays.toString(Arr2));
  11. }
  12. }

5:fill( ) 数组元素的填充

  1. public class Test00 {
  2. public static void main(String[] args) {
  3. Integer[] Arr2 = {1,5,7,8};
  4. ///1. 使用 99 去填充 num数组,可以理解成是替换原理的元素
  5. Arrays.fill(Arr2,10);
  6. System.out.println(Arrays.toString(Arr2));
  7. }
  8. }

6:alist( ) 将一组值转换为list

  1. public class Test00 {
  2. public static void main(String[] args) {
  3. Integer[] Arr2 = {1,5,7,8};
  4. //1. asList 方法,会将 (2,3,4,5,6,1)数据转成一个 List 集合
  5. //2. 返回的 asList 编译类型 List(接口)
  6. //3. asList 运行类型 java.util.Arrays#ArrayList,
  7. // 是Arrays 类的静态内部类
  8. List asList = Arrays.asList(2,3,4,5,6,1);
  9. System.out.println("asList=" + asList);
  10. }
  11. }

二:Arrays类课堂练习

image.png

  1. package Date0901.Test01;
  2. import java.util.Comparator;
  3. /**
  4. * 作者:sakura
  5. * 日期:2022年09月02日 00:53
  6. */
  7. public class Test00 {
  8. public static void main(String[] args) {
  9. Book[] books = new Book[4];
  10. books[0] = new Book("红楼梦", 100);
  11. books[1] = new Book("金瓶梅新", 90);
  12. books[2] = new Book("青年文摘20年", 5);
  13. books[3] = new Book("java从入门到放弃~",300);
  14. Pricesort(books, new Comparator() {
  15. @Override
  16. public int compare(Object o1, Object o2) {
  17. int n1 = (int)o1;
  18. int n2 = (int)o2;
  19. return n2 -n1;
  20. }
  21. });
  22. Namesort(books, new Comparator() {
  23. @Override
  24. public int compare(Object o1, Object o2) {
  25. int n1 = (int)o1;
  26. int n2 = (int)o2;
  27. return n2 -n1;
  28. }
  29. });
  30. System.out.println("--------排序结果--------");
  31. for (int i = 0; i < books.length; i++) {
  32. System.out.println(books[i].toString());;
  33. }
  34. }
  35. public static void Pricesort(Book[] book, Comparator C) {
  36. Book temp ;
  37. for (int i = 0; i < book.length; i++) {
  38. for (int j = 0; j < book.length -i-1 ; j++) {
  39. if ( (C.compare(book[j].getPrice(),book[j+1].getPrice())) >0){
  40. temp = book[j];
  41. book[j] = book[j + 1];
  42. book[j+1] = temp;
  43. }
  44. }
  45. }
  46. }
  47. public static void Namesort(Book[] books,Comparator C){
  48. Book temp;
  49. for (int i = 0; i < books.length; i++) {
  50. for (int j = 0; j < books.length-i-1; j++) {
  51. if ( (C.compare(books[j].getName().length(),books[j+1].getName().length())) >0){
  52. temp = books[j];
  53. books[j] = books[j + 1];
  54. books[j+1] = temp;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. class Book {
  61. private String name;
  62. private int price;
  63. public Book(String name, int price) {
  64. this.name = name;
  65. this.price = price;
  66. }
  67. public String getName() {
  68. return name;
  69. }
  70. public void setName(String name) {
  71. this.name = name;
  72. }
  73. public int getPrice() {
  74. return price;
  75. }
  76. public void setPrice(int price) {
  77. this.price = price;
  78. }
  79. @Override
  80. public String toString() {
  81. return "Book{" +
  82. "name='" + name + '\'' +
  83. ", price=" + price +
  84. '}';
  85. }
  86. }