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

 

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

 

posted @ 2023-11-17 15:35  ascertain  阅读(3)  评论(0编辑  收藏  举报