14.Java-Arrays(类)、基本类型包装类、Integer(类)

14.Java-Arrays(类)、基本类型包装类、Integer(类)

一、Arrays类的概述和方法使用

A:Arrays类概述
	针对数组进行操作的工具类。
	提供了排序,查找等功能。
B:成员方法
	public static String toString(int[] a)
	public static void sort(int[] a)
	public static int binarySearch(int[] a,int key)
	static boolean equals(int[] a, int[] a2) 比较两个数组中的元素,是否一样
    static int[] copyOf(int[] original, int newLength)  复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
    static int[] copyOfRange(int[] original, int from, int to) 复制旧数组中的指定范围间的几个元素到新数组中
C:案例演示	
​		通过Arrays类的功能来进排序和查找
package org.westos.java16;

import java.util.Arrays;

/*A:Arrays类概述
        针对数组进行操作的工具类。
        提供了排序,查找等功能。
        B:成员方法
public static String toString(int[] a)
public static void sort(int[] a)
public static int binarySearch(int[] a,int key)
static boolean equals(int[] a, int[] a2) 比较两个数组中的元素,是否一样
static int[] copyOf(int[] original, int newLength)  复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
static int[] copyOfRange(int[] original, int from, int to) 复制旧数组中的指定范围间的几个元素到新数组中
        C:案例演示
        ​		通过Arrays类的功能来进排序和查找*/
public class Test {
    public static void main(String[] args) {
        int[] arr={1,5,2,6,8};
        System.out.println(Arrays.toString(arr));//[1,5,2,6,8] 将数组返回这个形式的字符串
        Arrays.sort(arr);//Quicksort 快速排序 从小到大排序数组
        System.out.println(Arrays.toString(arr));//[1,2,5,6,8]
        int[] arr2={1,2,2,2,5,8,9,10};
        System.out.println(Arrays.binarySearch(arr2, 2));//二分查找 数组顺序有序 找到的不一定是第一个出现元素的索引值   // 3
        System.out.println(Arrays.binarySearch(arr2, 2, 7, 10)); //索引含头不含尾的范围内查找10   // -8 负数代表没找到
        System.out.println(Arrays.equals(arr,arr2));//false
        int[] copy1 = Arrays.copyOf(arr, 10); //不足的长度用0补全
        int[] copy2 = Arrays.copyOfRange(arr, 2, 6); //索引含头不含尾
        System.out.println(Arrays.toString(copy1));//[1, 2, 5, 6, 8, 0, 0, 0, 0, 0]
        System.out.println(Arrays.toString(copy2));//[5, 6, 8, 0]
        boolean[] arr3={true};
        boolean[] booleans = Arrays.copyOf(arr3, 5); //不足的长度用false补全
        System.out.println(Arrays.toString(booleans)); //[true, false, false, false, false]
        char[] arr4={'a'};
        String[] arr5={"a",""};
        System.out.println(Arrays.toString(Arrays.copyOf(arr4, 5))); //[a,  ,  ,  ,  ] //' ' 空格补全
        System.out.println(Arrays.toString(Arrays.copyOf(arr5, 5))); //[a, , null, null, null] //null补全
    }
}

二、Arrays类的源码解析

A:源码解析
	public static String toString(int[] a)
B:源码解析
	public static int binarySearch(int[] a,int key)
package org.westos.java16;

import java.util.Arrays;

/*A:源码解析
public static String toString(int[] a)
  B:源码解析
public static int binarySearch(int[] a,int key)*/
public class Test12 {
    public static void main(String[] args) {
        int[] arr={1,1,5,2};
        System.out.println(arrString(arr)); //[1,1,5,2]
        
        System.out.println(Arrays.toString(arr)); //[1, 1, 5, 2]
        System.out.println(Arrays.binarySearch(arr, 5)); //2
    }
    public static String arrString(int[] arr){
        if(arr==null){
            return null;
        }
        int max=arr.length-1;
        if(max==-1){
            return "[]";
        }
        StringBuffer stringBuffer = new StringBuffer("[");
        for (int i = 0; ; i++) {
            stringBuffer.append(arr[i]);
            if(i==max){
                return stringBuffer.append("]").toString();
            }
            stringBuffer.append(",");
        }
    }
    public static String toString(int[] a) {
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            b.append(a[i]);
            if (i == iMax)
                return b.append(']').toString();
            b.append(", ");
        }
    }
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
                                     int key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found.
    }
}

三、基本类型包装类的概述

A: 需求:
	a:将100转换成二进制 , 八进制 , 十六进制
	b:判断一个数是否在int的范围内
B:为什么会有基本类型包装类
	为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型.
C:常用操作:	常用的操作之一:用于基本数据类型与字符串之间的转换。
D:基本类型和包装类的对应
	byte 			Byte
	short			Short
	int			    Integer
	long			Long
	float			Float
	double		    Double
	char			Character
	boolean		    Boolean

四、Integer类的概述和构造方法

A:Integer类概述
	通过JDK提供的API,查看Integer类的说明

	Integer 类在对象中包装了一个基本类型 int 的值,
	该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
	还提供了处理 int 类型时非常有用的其他一些常量和方法
B:构造方法
	public Integer(int value)
	public Integer(String s)  //要个一个字面上是数字的字符串,如果不是就会报错
C:案例演示
	使用构造方法创建对象
package org.westos.java16;
/*A:Integer类概述
        通过JDK提供的API,查看Integer类的说明

        Integer 类在对象中包装了一个基本类型 int 的值,
        该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
        还提供了处理 int 类型时非常有用的其他一些常量和方法
        B:构造方法
public Integer(int value)
public Integer(String s)  //要个一个字面上是数字的字符串,如果不是就会报错
        C:案例演示
        使用构造方法创建对象*/
public class Test2 {
    public static void main(String[] args) {
        //Integer integer = new Integer(); //没有提供无参构造方法,只提供了有参构造方法
        Integer integer = new Integer(5);
        int i = integer.intValue();
        System.out.println(i); //5
        Integer integer1 = new Integer('a');
        System.out.println(integer1); //97
/*        Integer integer2 = new Integer("a"); //NumberFormatException
        System.out.println(integer2);*/
        Integer integer2 = new Integer("123");
        System.out.println(integer2); //123
    }
}

五、String和int类型的相互转换

A:int -- String
	a:和""进行拼接
	b:public static String valueOf(int i)
	c:int -- Integer -- String
	d:public static String toString(int i)
B:String -- int
	a:String -- Integer -- intValue();
	b:public static int parseInt(String s)
C:案例演示	
	String和int类型的相互转换
package org.westos.java16;

import java.util.Arrays;

/*A:int -- String
        a:和""进行拼接
        b:public static String valueOf(int i)
        c:int -- Integer -- String
        d:public static String toString(int i)
  B:String -- int
        a:String -- Integer -- intValue();
        b:public static int parseInt(String s)
        C:案例演示
        String和int类型的相互转换*/
public class Test3 {
    public static void main(String[] args) {
        int a=100;
        System.out.println(Integer.toBinaryString(a)); //public static String toBinaryString(int i)  返回类型是字符串,转换为2进制
        System.out.println(Integer.toOctalString(a)); //public static String toOctalString(int i)  返回类型是字符串,转换为8进制
        System.out.println(Integer.toHexString(a)); //public static String toHexString(int i)  返回类型是字符串,转换为16进制
        System.out.println(Integer.MAX_VALUE); //public static final int   MAX_VALUE = 0x7fffffff; int类型范围的最大值
        System.out.println(Integer.MIN_VALUE);//public static final int   MIN_VALUE = 0x80000000; int类型范围的最小值
        System.out.println("=========================");
        String s="" + a; //第一种方式
        String s1 = String.valueOf(a); //第二种方式
        Integer integer = new Integer(a);
        String s2 = integer.toString(); //第三种方式
        System.out.println(Integer.toString(a)); //第四种方式
        System.out.println("=========================");
        String s3="123";
        System.out.println(Arrays.toString(s3.getBytes())); //[49, 50, 51]
        Integer integer1 = new Integer(s3);
        int i = integer1.intValue(); //第一种方式
        int i1 = Integer.parseInt(s3); //第二种方式
        System.out.println(i); //123
        System.out.println(i1); //123
    }
}

六、JDK5的新特性自动装箱和拆箱

A:JDK5的新特性
	自动装箱:把基本类型转换为包装类类型
	自动拆箱:把包装类类型转换为基本类型
B:案例演示
	JDK5的新特性自动装箱和拆箱
	
	Integer ii = 100;
	ii += 200;
C:注意事项
	在使用时,Integer  x = null;代码就会出现NullPointerException。
	建议先判断是否为null,然后再使用。
package org.westos.java16;

/*A:JDK5的新特性
        自动装箱:把基本类型转换为包装类类型
        自动拆箱:把包装类类型转换为基本类型
        B:案例演示
        JDK5的新特性自动装箱和拆箱

        Integer ii = 100;
        ii += 200;
        C:注意事项
        在使用时,Integer  x = null;代码就会出现NullPointerException。
        建议先判断是否为null,然后再使用。*/
public class Test4 {
    public static void main(String[] args) {
        Integer a=123;//自动装箱
       // Integer a = Integer.valueOf(123); //手动装箱 valueOf(num)
        int b=a+123; //自动拆箱
        //int b=a.intValue()+123 //手动拆箱 intValue()
        System.out.println(b);//246
        Integer c=a+123; //经历了自动拆箱和自动装箱的过程
        //等价于下面两步操作
/*        int i = a.intValue() + 123;  //手动拆箱
        Integer c = Integer.valueOf(i);//手动装箱
*/
        Integer ii = 100; //自动装箱
        ii += 200; //自动拆箱+自动装箱
        System.out.println("=====================");
        Integer integer = Integer.valueOf("123");
        Integer integer1 = new Integer("123");
        Integer integer2 = Integer.valueOf(1); //自动装箱
        Integer integer3 = new Integer(1);
    }
}

七、Integer的面试题

package org.westos.java16;

public class Test5 {
    public static void main(String[] args) {
/*        A:Integer的面试题
                看程序写结果*/
                
//Integer这个类在继承Object类中的equals()方法时,重写了该方法
//比较的不再是地址值及引用是否相同,而是比较int这个基本类型数值是否相同

        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2); //false
        System.out.println(i1.equals(i2)); //true  
        System.out.println("-----------");

        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4); //false
        System.out.println(i3.equals(i4)); //true
        System.out.println("-----------");

        Integer i5 = 128;
        Integer i6 = 128;
        System.out.println(i5 == i6); //false
        System.out.println(i5.equals(i6)); //true
        System.out.println("-----------");

        Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8); //ture
        System.out.println(i7.equals(i8)); //true
    }
}
package org.westos.java16;

public class Test6 {
    public static void main(String[] args) {
        Integer integer = Integer.valueOf(127);
        //自动装箱 底层调用的是valueOf()来进行装箱的
        Integer i5 = 128;
        Integer i6 = 128;
        System.out.println(i5 == i6); // false
        //自动装箱
        Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8);//true
        System.out.println(integer==i7); //true

        /*
         *   //当我们采用自动装箱 Integer i7 = 127 这种方式 底层调用的是valueOf()来进行装箱的
         *
         *   在 valueOf()方法里面有这个判断,当我们装箱的值大于127 小于 -128 就会创建一个新的Integer对象返回。
         *
         * 如果说我们包装的这个值   在 -128 <=值<=127 之间
         * 他会从 IntegerCache 这个内部类中的cache[] 数组中取一个Integer对象返回给你
         *
         * IntegerCache 他已经提前帮你创建好了 256个Integer对象到这个cache[]中的。
         *
         *
         *
         * */

    }
}
package org.westos.java16;

//小结

public class Test7 {
    public static void main(String[] args) {
        Integer x=null;
/*        int a=x+1;
        System.out.println(a); //NullPointerException*/
        x=123; //相当于 x = Integer.valueOf(123);
/*        Integer xx = Integer.valueOf(123);
        System.out.println(x==xx); //true*/
        System.out.println(x.toString()); //重写过 toString()方法
        System.out.println(x);
        Integer integer = new Integer(123);
        Integer integer1 = new Integer(123);
        Integer integer2 = Integer.valueOf(123);
        Integer integer3 = Integer.valueOf(123);
        System.out.println(integer==integer1);// false
        System.out.println(integer2==integer3);// true
        //采用valueOf()方法自动装箱的原因是:-128 - 127 的数字装箱的时候使用的都是同一个地址值对象,
        //而使用new Integer()装箱时,每次都要new一个新的对象
        //所以valueOf()方法自动装箱的时候更加节省内存空间
    }
}

八、其他常用基本类型包装类

package org.westos.java16;
//Double Boolean Long与Integer(类)用法一致
public class Test26 {
    public static void main(String[] args) {
        double num=3.14;
        String s="3.14";
        Double aDouble = new Double(num);
        Double aDouble2 = Double.valueOf(3.14);
        Double aDouble3 = Double.valueOf(3.14);
        Double doub=3.14;//自动装箱
        doub=aDouble+1;//自动拆箱+自动装箱
/*        public static Double valueOf(double d) {
            return new Double(d);
        }*/
        System.out.println(aDouble2==aDouble3);//false
        Double aDouble1 = new Double(s);
        //Double s2="3.14";报错
        Double aDouble4 = Double.valueOf(s);
        String s1 = aDouble.toString();
        double v = aDouble.doubleValue();
        double v1 = Double.parseDouble(s);
        System.out.println("================================");
        System.out.println(Long.MAX_VALUE);
        System.out.println(Long.MIN_VALUE);
        String s2="11111111111111";
        Long aLong = new Long(s2);
        //long l=111111111111; 报错 Error:(22, 16) java: 过大的整数: 111111111111
        Long aLong1 = Long.valueOf(s2);
        String s3 = aLong.toString();
        long l = aLong.longValue();
        long l1 = Long.parseLong(s2);
        long lll=251616213;
        Long lon=lll;//自动装箱
        lon=lll+1;//自动拆箱+自动装箱
        System.out.println("================================");
        boolean b=false;
        String bb="TrUe";
        String bbb="sdf";
        Boolean aBoolean = new Boolean(b);
        Boolean aBoolean1 = new Boolean(bb);
        Boolean aBoolean2 = new Boolean(bbb);
/*        public static boolean parseBoolean(String s) {
            return ((s != null) && s.equalsIgnoreCase("true"));
        }*/
        System.out.println(aBoolean1);//true
        System.out.println(aBoolean2);//false
        System.out.println(bb.equalsIgnoreCase("true"));//true
        String s4 = aBoolean.toString();
        boolean b1 = aBoolean.booleanValue();
        boolean b2 = Boolean.parseBoolean(bbb);
        System.out.println(b2); //false
        Boolean bool=true;//自动装箱
        boolean bbbb=bool;//自动拆箱
    }
}

package org.westos.java16;
//Character(类)常用静态方法
public class Test28 {
    public static void main(String[] args) {
        boolean a = Character.isDigit(65);//判断ASC码是否是数字
        boolean a1 = Character.isDigit('a');//判断char字符是否是数字
        System.out.println(a); //false
        System.out.println(a1); //false
        System.out.println("================================");
        boolean a2 = Character.isLetter('a'); //判断char字符是否是字母
        boolean letter = Character.isLetter(66);//判断ASC码是否是字母
        System.out.println(a2); //true
        System.out.println(letter); //true
        System.out.println("================================");
        System.out.println(Character.isLowerCase('a')); //判断char字符是否是小写字母  true
        System.out.println(Character.isLowerCase(68)); //判断ASC码是否是小写字母 false
        System.out.println("================================");
        System.out.println(Character.isSpaceChar(32)); //判断ASC码是否是空白字符  true
        System.out.println(Character.isSpaceChar(' ')); //判断char字符是否是空白字符 true
        System.out.println(Character.isWhitespace(' ')); //判断char字符是否是空白字符 true
        System.out.println(Character.isWhitespace(99)); //判断ASC码是否是空白字符  false
        System.out.println(' '+1);//33

    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值