书名长度排序
- package com.hspedu;
-
- import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
-
- import java.text.SimpleDateFormat;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Comparator;
- import java.util.Date;
-
- /**
- * @author: guorui fu
- * @versiion: 1.0
- */
- public class ArrayExercise {
- public static void main(String[] args) {
- Book[] books = new Book[4];
- books[0] = new Book("红楼梦",1000);
- books[1] = new Book("金瓶梅新",90);
- books[2] = new Book("青年文摘20年",5);
- books[3] = new Book("java从入门到放弃",300);
- Arrays.copyOf(books,3);
- //按照名字长度大小进行数组排序
- Arrays.sort(books, new Comparator() {
- @Override
- public int compare(Object o1, Object o2) {
- Book i1 = (Book) o1;
- Book i2 = (Book) o2;
- int minus = i1.getName().length() - i2.getName().length();
- if (minus > 0){
- return -1;
- } else if (minus < 0){
- return 1;
- } else {
- return 0;
- }
- }
- });
- System.out.println(Arrays.toString(books));
-
- //Date类的时间运用
- Date date = new Date();
- System.out.println(date);
-
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
- String format = sdf.format(date);
- System.out.println(format);
-
- }
- }
- class Book{
- private String name;
- private int price;
-
- public Book(String name, int price) {
- this.name = name;
- this.price = price;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getPrice() {
- return price;
- }
-
- public void setPrice(int price) {
- this.price = price;
- }
-
- @Override
- public String toString() {
- return '{' + name + '\'' + price + "元}";
- }
- @Override
- protected void finalize() throws Throwable {
- super.finalize();
- }
- }