Java: toString Comparator Array equals
toString
package org.example.c; public class A { public static String toString(Object[] array) { if (array == null) return "null"; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append('['); for (Object object : array) { stringBuilder.append(String.valueOf(object)).append(", "); } stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length()); stringBuilder.append(']'); return stringBuilder.toString(); } public static void main(String[] args) { Integer[] integers = {1, 2, 3}; System.out.println(toString(integers)); } }
package org.example.c; public class A { public static String toString(Object[] array) { if (array == null) return "null"; if (array.length == 0) return "[]"; StringBuilder stringBuilder = new StringBuilder("["); for (int b = 0; ; ++b) { stringBuilder.append(String.valueOf(array[b])); if (b == array.length - 1) return stringBuilder.append(']').toString(); stringBuilder.append(", "); } } public static void main(String[] args) { Integer[] integers = {1, 2, 3}; System.out.println(toString(integers)); } }
Comparator
package org.example.c; import java.util.Arrays; import java.util.Comparator; public class Ersatz { public static void bubbleSort(int[] array, Comparator<Integer> comparator) { int tmp; for (int b = 0; b < array.length - 1; ++b) { for (int p = 0; p < array.length - 1 - b; ++p) { if (array[p] > array[p + 1]) { tmp = array[p]; array[p] = array[p + 1]; array[p + 1] = tmp; } } } } public static void main(String[] args) { int[] v = {11, 0, -1, 8}; bubbleSort(v, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1 - o2; } }); System.out.println(Arrays.toString(v)); } }
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | package com.gibe; import java.util.Arrays; import java.util.Comparator; public class Ersatz { public static void main(String[] args) throws Exception { Book[] books = new Book[4]; books[0] = new Book( "b" , 100); books[1] = new Book( "bb" , 90); books[2] = new Book( "bbb" , 5); books[3] = new Book( "bobby" , 300); // price descend Arrays.sort(books, new Comparator() { @Override public int compare(Object b, Object p) { Book b1 = (Book) b; Book b2 = (Book) p; double deviation = b1.getPrice() - b2.getPrice(); if (deviation > 0) { return -1; // positive need to swap } else if (deviation < 0) { return 1; } else { return 0; } } }); System. out .println(Arrays.toString(books)); // price ascend Arrays.sort(books, new Comparator() { @Override public int compare(Object b, Object p) { Book b1 = (Book) b; Book b2 = (Book) p; double deviation = b1.getPrice() - b2.getPrice(); if (deviation > 0) { return 1; } else if (deviation < 0) { return -1; } else { return 0; } } }); System. out .println(Arrays.toString(books)); // name.length descend Arrays.sort(books, new Comparator<Book>() { @Override public int compare(Book o1, Book o2) { return o2.getName().length() - o1.getName().length(); } }); System. out .println(Arrays.toString(books)); } } class Book { private String name; private double price; public Book(String name, double price) { this .name = name; this .price = price; } @Override public String toString() { return "Book{" + "name='" + name + '\ '' + ", price=" + price + '}' ; } public String getName() { return name; } public void setName(String name) { this .name = name; } public double getPrice() { return price; } public void setPrice( double price) { this .price = price; } } |
Array equals
package org.example.c; import java.util.Objects; public class Ersatz { public static boolean equals(Object[] b, Object[] p) { if (b == p) return true; if (b == null || p == null) return false; if (b.length != p.length) return false; for (int v = 0; v < b.length; ++v) { Object o1 = b[v]; Object o2 = p[v]; // if (!(o1 == null ? o2 == null : o1.equals(o2))) { // return false; // } if (!Objects.equals(o1, o2)) return false; } return true; } public static void main(String[] args) { Integer[] b = {null, 1}; Integer[] p = {null, 1}; System.out.println(equals(b, p)); String[] t = {null, "bb"}; String[] u = {null, "bb"}; System.out.println(equals(t, u)); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律