零基础学习java------day12------

时间:2019-08-18
本文章向大家介绍零基础学习java------day12------,主要包括零基础学习java------day12------使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

0.数组高级

(1)选择排序 

  它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的起始位置。以此类推,直到全部待排序的数据元素排完

// 选择排序
    /**
     * asc:升序          desc:降序
     * @param arr    要排序的数组
     * @param isAsc  是否升序         true:升序     false:降序
     */
    // 下面是直接交换元素,零一种方法是定义一个最小下角标,如minIndex = i
    public static void selectSort(int[] arr, boolean isAsc) {
        for(int i=0;i<arr.length-1;i++) {
            for(int j=i+1;j<arr.length;j++) {
                if(isAsc) {
                    if(arr[j]<arr[i]) {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;    
                    }
                }else {
                    if(arr[j]>arr[i]) {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;    
                    }
                }    
            }
        }
View Code

(2)冒泡排序

  冒泡排序(Bubble Sort),它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,排序”。所以叫“冒泡排序

// 冒泡排序
    /**
     * 两种思路:第一种:外部循环考虑比较的轮数,内部循环考虑比较的次数(数组长度-比较的轮数)
     *              第二种:外部循环考虑比较的次数,内部循环直接进行比较(如下)
     * @param arr
     * @param isAsc
     */
    public static void bubbleSort(int[] arr, boolean isAsc) {
        for(int i=arr.length-1;i>0;i--) {
            for(int j=0;j<i;j++) {
                if(isAsc) {
                    if(arr[j]>arr[j+1]) {
                        int temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }else {
                    if(arr[j]<arr[j+1]) {
                        int temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
        }
View Code

(3)二分查找

// 二分查找
    public static int binarySearch(int[] arr, int num) {
        // 把升序和降序两种情况都写出来
        int startIndex = 0;
        int endIndex = arr.length-1;
        while(startIndex <= endIndex) {
            int middleIndex = (startIndex+endIndex)/2;
            if(num == arr[middleIndex]) {
                return middleIndex;
            }else if(num < arr[middleIndex]) {
                if(arr[0] < arr[arr.length-1]) {
                    endIndex = middleIndex - 1;
                }else {
                    startIndex = middleIndex + 1;
                }
            }else {
                if(arr[0] < arr[arr.length-1]) {
                    startIndex = middleIndex+1;
                }else {
                    endIndex = middleIndex - 1;
                }
            }
        }
        return -1;
    }
View Code

1. Arrays

  Arrays: java.util 数组的工具类
  public static String toString(int[] a) 得到数组的字符串形式[数组的元素1,2,3,4]
  public static void sort(int[] a) 排序(升序)
  public static int binarySearch(int[] a,int key) 二分查找(只支持升序的数组)

public class ArraysDemo {
    public static void main(String[] args) {
        int[] arr = {12,3,43,545};
        ArrayUtil.printArr(arr);
        String str = Arrays.toString(arr);
        System.out.println(str);
        Arrays.sort(arr);
        ArrayUtil.printArr(arr);
        ArrayUtil.reverse(arr);//降序
        ArrayUtil.printArr(arr);
        //该方法只能对升序数组做查询
        int index = Arrays.binarySearch(arr, 12);//如果数组是降序的,则不能得到正确结果
        System.out.println(index);//1
    }
}

2.包装类

2.1 概述

包装类:(引用数据类型,类)

object可以接收一切参数(除了8中基本数据类型,其没有父类),所以就有包装类的出现(可以通过将基本数据类型转换成对应的包装类,这样object就能间接的接收基本数据类型了)


为每种基本数据类型都提供了对象的引用数据类型
byte         Byte
short        Short
int            Integer
long         Long
float         Float
double          Double
char         Character
boolean      Boolean

2.2 装箱和拆箱

 装箱:把基本数据类型转成包装类

 拆箱:把包装类转为基本数据类型

public class WrapperDemo {
    public static void main() {
        // 装箱:把基本数据类型转成包装类
        int i = 10;
        Integer in = Integer.valueOf(i);
        // 拆箱:把包装类转为基本数据类型
        int a = in.intValue();
        
        // jdk1.5后自动拆装箱
        Integer b = i;
        int c = b;
    }
}

2.3 Integer及构造方法(其它包装类也类似)

 (1)Integer类概述

  Integer类在对象中包装了一个基本数据类型int的值,该类提供了多个方法,能在int类型和String类型之间互相转换,还提供了处理int类型时非常有用的其他一些常量和方法

 (2)构造方法

  public Integer(int value)

  public Integer(String s)

这俩构造方法在jdk1.9后不推荐使用了,更优的方案使用valueOf来创建Integer对象,而parseInt(String)则是将字符串转为int类型

 (3)String和int的转换(int换成其他基本类型也一样)

a.  int 转成String(两种方式)

第一种:通过与字符串的相加

String s = 12+"";

第二种:通过String的valueOf方法

String s2 = String.valueOf(12);

b.  String 转成int

int d = new Integer("123");//右边是Integer类型,自动拆成int型,jdk1.9后不推荐使用

 推荐使用(jdk1.9以后)

int b = Integer.valueOf("234");//右边Integer类型,jdk1.9后创建Integer对象的推荐方法
int c = Integer.parseInt("345");//右边等式将string转为了int

若int换成其他数据类型,如double,结果与上面相似,如

double h = Double.parseDouble("12.88");

(4)Integer成员方法(这里应该是静态方法,但也能叫成员方法,不过很少)

a.   常用的基本进制转换
  public static String toBinaryString(int i) 转成二进制的字符串
  public static String toOctalString(int i) 转成八进制的字符串
  public static String toHexString(int i) 转成十六进制的字符串

b.  十进制到其他进制     

  i: 要转的10 进制      radix: 转成几进制          (进制的范围为2-36,因为数字是10个,字母26个,超过36进制就不知道用什么去表示了)超过36 返回原内容
  public static String toString(int i,int radix)
c. 其他进制到十进制s:

  要转的数radix: 该数是几进制的

   public static int parseInt(String s,int radix)

public class IntegerDemo {
    public static void main(String[] args) {
        int a = 100;
        System.out.println(Integer.toBinaryString(a));//1100100
        System.out.println(Integer.toOctalString(a));//144
        System.out.println(Integer.toHexString(a));//64
        System.out.println(Integer.toString(a,8));//把a转成8进制
        System.out.println(Integer.toString(a,36));//把a转成36进制2s
        System.out.println(Integer.parseInt("2s",36));// 把36进制的2s转成10进制,100
        System.out.println(Integer.MAX_VALUE);//表示Integer的最大值
        System.out.println(Integer.MIN_VALUE);//最小值
    }
}

2.4 Charater

 (1)Character类概述

  Character类在对象中包装一个基本类型char的值,此外,该类提供了几种方法,已确定字符的类别(大写字母,小写字母,数字等等),并将字符从大写转换成小写,反之亦然

(2)构造方法

  public Character(char value)

(3)Character成员方法

  public static boolean isUpperCase(char ch) 是否是大写字母
  public static boolean isLowerCase(char ch) 是否是小写字母
  public static boolean isDigit(char ch) 是否是数字
  public static char toUpperCase(char ch) 转成大写字母
  public static char toLowerCase(char ch) 转成小写字母

练习:

改进练习:
统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑
其他字符)

public class DaXiaoXie {
    public static void main(String[] main) {
        String str = "akfIJDO123";
        int n = str.length();
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        for(int i = 0;i<n;i++) {
            if(Character.isUpperCase(str.charAt(i))) {
                count1++;
            }
            if(Character.isLowerCase(str.charAt(i))) {
                count2++;
            }
            if(Character.isDigit(str.charAt(i))) {
                count3++;
            }
        }
        System.out.println(count1);
        System.out.println(count2);
        System.out.println(count3);
    }
}
View Code

3. BigInteger

由于基本数据类型所能表示的数值的大小有限,当数据量特别大的话就不能表示,所以就提出BigInteger类型,其数值大小为Integer所能表示数值大小的平方

(1)BigInteger类概述:

  可以让超过Integer范围内的数据进行运算

(2)构造方法

  public BigInteger(String val);

(3)成员方法

public BigInteger add(BigInteger, val);
public BigInteger subtract(BigInteger, val);
public BigInteger multiply(BigInteger, val);
public BigInteger divide(BigInteger, val);
public BigInteger[] divideAndRemainder(BigInteger, val);//第一个元素商,第二个余数

案例

import java.math.BigInteger;

public class BigIntegerDemo {
    public static void main(String[] args) {
        BigInteger b1 = new BigInteger("123");
        BigInteger b2 = new BigInteger("2");
        BigInteger b3 = b1.add(b2);
        BigInteger b4 = b1.subtract(b2);
        BigInteger b5 = b1.multiply(b2);
        BigInteger b6 = b1.divide(b2);
        BigInteger[] b7 = b1.divideAndRemainder(b2);//注意,此处得到的结果为数组
        System.out.println(b3);//125,没有打印地址值是因为BigInteger重写了toString方法
        System.out.println(b4);//121
        System.out.println(b5);//246
        System.out.println(b6);//61
        System.out.println(b7[0]);//61
        System.out.println(b7[1]);//1
    }
}

4.BigDecimal

BigDecimal及构造方法

 (1)由于在运算的时候,float类型和double很容易丢失精度(如下),所以,为了能精确的表示、计算浮点数,java提供了BigDecimal

System.out.println(1.0-0.32);//0.679999999999

 (2)BigDecimal类概述

  不可变的,任意精度的有符号十进制数

 (3)构造方法

   public BigDecimal(String val)
   public BigDecimal(int val)

 (4)成员方法

public BigDecimal add(Bigcimal augend)
public BigDecimal subtract(Bigcimal subtrahend)
public BigDecimal multiply(Bigcimal multiplicand)
public BigDecimal divide(Bigcimal divisor)
public BigDecimal divide(Bigcimal divisor,int scale,int roundingMode) // divisor为除数,scale为要保存的小数位数

案例

public class BigDecimalDemo {
    public static void main(String[] args) {
        BigDecimal b1 = new BigDecimal("123");
        BigDecimal b2 = new BigDecimal("2");
        System.out.println(b1.add(b2));//125
        System.out.println(b1.subtract(b2));//121
        System.out.println(b1.multiply(b2));//246
        System.out.println(b1.divide(b2));//61.5
        System.out.println(new BigDecimal("1.0").subtract(new BigDecimal("0.32")));//0.68,无精度损失
    }
}
BigDecimal b3 = new BigDecimal("7");
BigDecimal b4 = new BigDecimal("3");
System.out.println(b3.divide(b4, 2, BigDecimal.ROUND_UP));//The field BigDecimal.ROUND_UP is deprecated since version 9

(5)格式化小鼠

 BigDecimal.setScale() 方法用于格式化小数点

setScale(1)表示保留一位小数,默认用四舍五入的方式

setScale(1,BigDecimal.ROUND_DOWN)直接删除多余的小数位,如2.35会变成2.3

setScale(1,BigDecimal.ROUND_UP) 进位处理,2.35变成2.4

setScale(1,BigDecimal.ROUND_HALF_UP)四舍五入,2.35变成2.4 

 setScale(1,BigDecimal.ROUND_HALF_DOWN)四舍五入,2.35变成2.3 

 5. Math

public static int abs(int a)                           取绝对值
public static double ceil(double a)                    向上取整
public static double floor(double a)                   向下取整
public static double pow(double a,double b)            求a的b次方
public static double random()                          随机数,从0-1之间的随机数
public static int round(float a)                       四舍五入取整
public static double sqrt(double a)            求算数平方根       

案例

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.PI);//3.141592653589793
        System.out.println(Math.abs(-2));//2
        System.out.println(Math.ceil(12.1));//13.0
        System.out.println(Math.floor(12.5));//12.0
        System.out.println(Math.pow(4, 4));//256.0
        System.out.println(Math.random());
        System.out.println(Math.round(12.5));//13
        System.out.println(Math.sqrt(16));//4.0
    }
}

原文地址:https://www.cnblogs.com/jj1106/p/11370048.html