• Java、检查顺序


     

    1. package sort;
    2. import java.util.Arrays;
    3. import java.util.Comparator;
    4. /**
    5. * @author: xyz
    6. * @create: 2022/8/15
    7. * @Description:
    8. * @FileName: Exercise23_05
    9. * @History:
    10. * @自定义内容:
    11. */
    12. public class Exercise23_05 {
    13. public static void main(String[] args) {
    14. int[] list1 = new int[10];
    15. for (int i = list1.length - 1, j = 0; i >= 0; i--)
    16. list1[j++] = i;
    17. System.out.println(Arrays.toString(list1));
    18. System.out.println(ordered(list1));
    19. double[] list2 = new double[10];
    20. for (int i = 0; i < list2.length; i++)
    21. list2[i] = i;
    22. System.out.println(Arrays.toString(list2));
    23. System.out.println(ordered(list2));
    24. Integer[] list3 = new Integer[10];
    25. for (int i = 0; i < list3.length; i++)
    26. list3[i] = i;
    27. System.out.println(Arrays.toString(list3));
    28. System.out.println(ordered(list3, false));
    29. Double[] list4 = new Double[10];
    30. for (int i = 0; i < list4.length; i++)
    31. list4[i] = i * 1.0;
    32. System.out.println(Arrays.toString(list4));
    33. System.out.println(ordered(list4, new SortComparator<>()));
    34. }
    35. public static boolean ordered(int[] list) {
    36. return ordered(list, true);
    37. }
    38. public static boolean ordered(int[] list, boolean ascending) {
    39. double[] temp = new double[list.length];
    40. for (int i = 0; i < list.length; i++)
    41. temp[i] = list[i];
    42. return ordered(temp, ascending);
    43. }
    44. public static boolean ordered(double[] list) {
    45. return ordered(list, true);
    46. }
    47. public static boolean ordered(double[] list, boolean ascending) {
    48. for (int i = 0; i < list.length - 1; i++) {
    49. if (ascending) {
    50. if (list[i] > list[i + 1])
    51. return false;
    52. } else {
    53. if (list[i] < list[i + 1])
    54. return false;
    55. }
    56. }
    57. return true;
    58. }
    59. public static extends Comparable> boolean ordered(E[] list) {
    60. return ordered(list, true);
    61. }
    62. public static extends Comparable> boolean ordered(E[] list, boolean ascending) {
    63. for (int i = 0; i < list.length - 1; i++) {
    64. if (ascending) {
    65. if (list[i].compareTo(list[i + 1]) > 0)
    66. return false;
    67. } else {
    68. if (list[i].compareTo(list[i + 1]) < 0)
    69. return false;
    70. }
    71. }
    72. return true;
    73. }
    74. public static boolean ordered(E[] list, Comparatorsuper E> comparator) {
    75. return ordered(list, comparator, true);
    76. }
    77. public static boolean ordered(E[] list, Comparatorsuper E> comparator, boolean ascending) {
    78. for (int i = 0; i < list.length - 1; i++) {
    79. if (ascending) {
    80. if (comparator.compare(list[i], list[i + 1]) > 0)
    81. return false;
    82. } else {
    83. if (comparator.compare(list[i], list[i + 1]) < 0)
    84. return false;
    85. }
    86. }
    87. return true;
    88. }
    89. /** 静态内部类-泛型比较器类 */
    90. static class SortComparator implements Comparator {
    91. @Override
    92. public int compare(E o1, E o2) {
    93. if (o1 instanceof Comparable) {
    94. if (((Comparable) o1).compareTo(o2) > 0) return 1;
    95. else if (((Comparable) o1).compareTo(o2) < 0) return -1;
    96. else return 0;
    97. }
    98. //按哈希码排序
    99. if (o1.hashCode() > o2.hashCode())
    100. return 1;
    101. else if (o1.hashCode() < o2.hashCode())
    102. return -1;
    103. else
    104. return 0;
    105. }
    106. }
    107. }

     

  • 相关阅读:
    Spring--基于注解管理bean
    C语言入门,用什么编译器比较好?
    Linux之进程信号
    初级查找算法
    深度学习:如何静悄悄地改变我们的日常生活
    TextAttack配置中遇到的问题(TAADpapers)
    Plsql连接报Initialization Error:Could not initialize oci.dll
    【Java八股文总结】之Spring
    【JavaSE】内部类
    drf从无到有学习Django Rest Framework框架——目录结构最佳实践、REST_FRAMEWORK、数据库配置
  • 原文地址:https://blog.csdn.net/m0_62659797/article/details/126375288