zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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;
  }
}