Java Arrays binarySearch equals
JAVA equals Arrays
2023-09-11 14:16:16 时间
package ersatz;
import java.io.FileNotFoundException;
import java.util.Arrays;
public class Ersatz {
public static void main(String[] args) throws FileNotFoundException {
Integer integer[] = {8, 2, 6, 1};
System.out.println(Arrays.toString(integer));
System.out.println(toString(integer));
}
public static String toString(Object[] b) {
if (b == null) {
return "null";
}
int indexMax = b.length - 1;
if (indexMax == -1) return "[]";
StringBuilder p = new StringBuilder();
p.append("[");
for (int i = 0; ; ++i) {
p.append(String.valueOf(b[i]));
if (i == indexMax) return p.append(']').toString();
p.append(", ");
}
}
}
package com.gibe;
import java.util.Arrays;
import java.util.Comparator;
public class Ersatz {
public static void main(String[] args) throws Exception {
int[] v = {11, 0, -1, 8};
BubbleSort(v,new Comparator(){
@Override
public int compare(Object o1, Object o2) {
return (Integer)o1-(Integer)o2;
}
});
System.out.println(Arrays.toString(v));
}
public static void BubbleSort(int[] b, Comparator comparator) {
int temp=0;
for(int i=0;i<b.length-1;++i){
for(int j=0;j<b.length-1-i;++j){
if (comparator.compare(b[j], b[j + 1]) > 0) {
temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
}
}
package com.gibe;
import java.util.Arrays;
public class Ersatz {
public static void main(String[] args) throws Exception {
Integer[] b = {1, 3, 9, 12, 15};
int index = Arrays.binarySearch(b, 18);
System.out.println(index);
System.out.println(binarySearch(b,18));
}
public static int binarySearch(Integer[] b, Integer key) {
int low = 0;
int high = b.length - 1;
int mid;
while (low <= high) {
mid = (low + high) >>> 1;
if (key > b[mid]) {
low = mid + 1;
} else if (key < b[mid]) {
high = mid - 1;
} else {
return mid;
}
}
return -low;
}
}
直接 return 负值 表示 没有找到 , low此时表示此值应在array中的索引,但是 0 是有问题的(无法确定是否找到了), 故 采用源码方式 返回 return -(low+1), 如果是负值 , 表示 没有找到,并且 abs(key) 为 此值应在array中的位置( 1 开始编号)
改进
package com.gibe;
import java.util.Arrays;
import java.util.Comparator;
public class Ersatz {
public static void main(String[] args) throws Exception {
Integer[] b = {1, 3, 9, 12, 15};
int index = Arrays.binarySearch(b, 2);
System.out.println(index);
System.out.println(binarySearch(b, 2, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return (Integer) o1 - (Integer) o2;
}
}));
}
public static int binarySearch(Integer[] b, Integer key, Comparator comparator) {
int low = 0;
int high = b.length - 1;
int mid;
while (low <= high) {
mid = (low + high) >>> 1;
if (comparator.compare(key, b[mid]) > 0) {
low = mid + 1;
} else if (comparator.compare(key, b[mid]) < 0) {
high = mid - 1;
} else {
return mid;
}
}
return -(low+1);
}
}
equals方法实现
package com.gibe;
import java.util.Objects;
public class Ersatz {
public static void main(String[] args) throws Exception {
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));
}
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 i = 0; i < b.length; ++i) {
Object o1 = b[i];
Object o2 = p[i];
// if (!(o1 == null ? o2 == null : o1.equals(o2))) {
// return false;
// }
if (!Objects.equals(o1, o2)) { // 使用Objects.equals()
return false;
}
}
return true;
}
}
asList将参数转成List集合
返回的编译类型为List(接口)
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;
}
}
相关文章
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
- [Java Plasterer] Java Components 3:Java Enum
- Java实现第八届蓝桥杯取数位
- Java实现 蓝桥杯VIP 算法训练 Car的旅行路线
- Java实现 蓝桥杯VIP 算法训练 整数平均值
- 【JAVA】java中的length和length()
- java动态代理的两种方法
- 深入理解JAVA虚拟机--Idea远程执行本地Java代码
- Java NIO
- 【JAVA】MacBook安装Java环境及eclipse
- 【JAVA】增强for循环for(int a : arr)
- java集合框架03——ArrayList和源码分析
- 从零开始学习Java神经网络、自然语言处理和语音识别,附详解和简易版GPT,语音识别完整代码示例解析
- Java:JavaFX项目FXML文件应用实例
- Java 标准编码 StandardCharsets
- Java中的final变量 final修饰变量 final修饰方法 final修饰类 final和String的关系
- Java Logger(java日志)
- Java中static作用及使用方法具体解释
- Java Instrumentation 内存马——主要是利用Instrumentation Java API来做内存注入,会用到反射机制,文中提到检测思路:注入jar包-> dump已加载class字节码->反编译成java代码-> 源码webshell检测
- 【java】Java 抽象类
- 【java】Java 内存模型
- java入门 常见问题0006:字符串的内容比较应当用String的equals方法,而不是用 ==