Math类和Arrays类的常用方法和习题

Math类常用方法
package math;

public class Math01 {
    public static void main(String[] args) {
        int a = 4;
        double d = -4.4d;
        float f = 3.4f;
        System.out.println(Math.abs(d));
        System.out.println(Math.max(2,34));
        System.out.println(Math.ceil(f));//向上取整 4.0
        System.out.println(Math.floor(f));//向下取整 3.0
        System.out.println(Math.round(f));//四舍五入: 先 + 0.5 在向下取整
        System.out.println(Math.pow(2,3));//求幂
        System.out.println(Math.sqrt(9));//求开方
        //random 求随机数
        // random 返回的是 0 <= x < 1 之间的一个随机小数
       //请写出获取 a-b 之间的一个随机整数,a,b 均为整数 ,比如 a = 2, b=7
        int x = 2;
        int y = 7;
        for (int i = 0; i < 10; i++) {
            System.out.println((int)(2 + Math.random() * (7 - 2 + 1)));
        }
    }
}
Arrays类常用方法
package arrays;

import java.util.Arrays;
import java.util.Comparator;

public class Arrays01 {
    public static void main(String[] args) {
        Integer[] integer = {14,3,21,2,2367,5};
        System.out.println(Arrays.toString(integer));
        Arrays.sort(integer);//默认排序
        System.out.println(Arrays.toString(integer));
       //定制排序
        Arrays.sort(integer, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                Integer i1 = (Integer) o1;
                Integer i2 = (Integer) o2;
                return i2 -i1;
            }
        });
        int[] arr = {1,23,4432,213,4};
//        System.out.println(Arrays.toString(integer));
        bubble02(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
               Integer i1 = (Integer) o1;
               Integer i2 = (Integer) o2;
               
                return i2 - i1;
            }
        });
        System.out.println(Arrays.toString(arr));
    }
    //冒泡 + 定制
    public static void bubble02(int[] arr, Comparator c) {
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //数组排序由 c.compare(arr[j], arr[j + 1])返回的值决定
                if (c.compare(arr[j], arr[j + 1]) > 0) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
习题

案例:自定义Book类,里面包含name和price,按price排序(从大到小)。要两种方式排序,有一个 Book[] books = 4本书对象
使用前面学习过的传递实现Comparator接口匿名内部类也称为定制排序。

package arrays;

import extends_exercise01.Person;

import java.text.CollationKey;
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;

public class ArraysTest01 {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦",100);
        books[1] = new Book("金瓶梅新",90);
        books[2] = new Book("青年文摘20年",5);
        books[3] = new Book("java从入门到放弃",300);

        Book.bubule01(books);
        for (int i = 0; i < books.length; i++) {
            System.out.println("name" +books[i].getName() + "price: " + books[i].getPrice());
        }
        System.out.println();
        Book.dinzhibuBule01(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book book1 = (Book) o1;
                Book book2 = (Book) o2;//向下转型
                return book2.getPrice() - book1.getPrice();
            }
        });
        for (int i = 0; i < books.length; i++) {
            System.out.println("name" +books[i].getName() + "price: " + books[i].getPrice());
        }
    }
}

class Book {
    private String name;
    private int price;

    //冒泡排序
    public static void bubule01(Book[] books) {
        for (int i = 0; i < books.length - 1; i++) {
            for (int j = 0; j < books.length - 1 - i; j++) {
                Book temp;
                if (books[j ].price > books[j + 1].price) {
                    temp = books[j];
                    books[j] = books[j + 1];
                    books[j + 1] = temp;
                }
            }
        }
    }

    //定制 + 冒泡
    public static void dinzhibuBule01(Book[] books, Comparator c) {
        for (int i = 0; i < books.length - 1; i++) {
            for (int j = 0; j < books.length - 1 - i; j++) {
                Book temp;
                if (c.compare(books[j], books[j + 1]) > 0) {
                    temp = books[j];
                    books[j] = books[j + 1];
                    books[j + 1] = temp;
                }
            }
        }
    }

    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 super.toString();
    }
}

编译结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值