java常用类---Arrays和Integer

1.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)复制旧数组中的指定范围间的几个元素到新数组中
    案例1:
public class MyTest {
    public static void main(String[] args) {
        //Java针对数组的操作,给我们提供了一个工具类Arrays
        //此类包含用来操作数组(比如排序和搜索)的各种方法。
        int[] arr = {10, 20, 5, 3, 8, 7, 6};
        //排序
        Arrays.sort(arr);
        //打印数组元素
        String string = Arrays.toString(arr);
        System.out.println(string);
        //二分查找:前提数组元素有序
        int index = Arrays.binarySearch(arr, 80);
        System.out.println(index);
    }

}

案例2:

public class MyTest2 {
    public static void main(String[] args) {
        int[] arr = {10, 20, 5, 3, 8, 7, 6};
        int[] arr2 = {10, 20, 5, 3, 8, 7, 6};
        boolean b = Arrays.equals(arr, arr2);
        System.out.println(b);

       /* static int[] copyOf ( int[] original, int newLength)
        复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。*/

        int[] ints = Arrays.copyOf(arr, arr.length);

        System.out.println(Arrays.toString(ints));

        "abc".concat("abc");

       /* static int[] copyOfRange ( int[] original, int from, int to)
        将指定数组的指定范围复制到一个新数组。
*/

        //从指定索引处,拷贝旧数组元素到你指定的终止索引处,含头不含尾

        int[] ints1 = Arrays.copyOfRange(arr, 1, 3);
        System.out.println(Arrays.toString(ints1));
    }
}

2.基本类型包装类的概述

  • 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
public class MyTest {
    public static void main(String[] args) {

        //byte -128  ---127
        //int 4个字节  -2^31-<=num-<=-2^31-1
        //Java为了我们方便的去操作这些基本类型的数据,那么针对每个基本数据类型,都提供了他所对应的包装类(引用类型)

        int num=100;
        String string = Integer.toBinaryString(num);
        String string1 = Integer.toHexString(num);
        String string2 = Integer.toOctalString(num);
        System.out.println(string);
        System.out.println(string2);
        System.out.println(string1);

        if(2000>=Integer.MIN_VALUE&& 2000<=Integer.MAX_VALUE){
            System.out.println("int 类型的值");
        }
    }
}

3.Integer类

  • A:Integer类概述
    Integer 类在对象中包装了一个基本类型 int 的值,
    该类提供了多个方法,能在 int 类型和String 类型之间互相转换还提供了处理 int 类型时非常有用的其他一些常量和方法
  • B:构造方法
    public Integer(int value)
    public Integer(String s)

4.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)
public class MyTest {
    public static void main(String[] args) {
        // String和int类型的相互转换

        //int------String
        int num = 100;//  ----->"100"
        //方式1
        String str = num + "";  //拼接空串
        //方式2 记忆这个
        //String 中 静态方法 valueOf(num) 可以把多种类型转换成字符串
        String s = String.valueOf(num);
      /*  char[]  chs={'a','b','c'};
        String s1 = new String(chs);
        //把字符数组转换成字符串
        String s2 = String.valueOf(chs);*/
        //方式3
        Integer integer = new Integer(num);
        String string = integer.toString();
        System.out.println(string);
        // String------int
        String strNum = "100";//----100
        //方式1
        Integer integer1 = new Integer(strNum);
        int i = integer1.intValue(); //把包装类型转成他对应的基本类型

        //方式2 记这个方法
        int nun = Integer.parseInt(strNum);


    }
}

5.JDK5的新特性自动装箱和拆箱

  • A:JDK5的新特性
    自动装箱:把基本类型转换为包装类类型
    自动拆箱:把包装类类型转换为基本类型
  • B:案例演示
    JDK5的新特性自动装箱和拆箱
public class MyTest2 {
    public static void main(String[] args) {
        //JDK1.5之后有的自动拆装箱
        //自动装箱:将基本类型自动转换成他所对应的包装类型
        //自动拆箱:将包装类型自动转换成他所对应的基本类型
        Integer integer = new Integer(20);

        Integer integer2 = new Integer(200);
       /* int a=20;
        int b=200;*/

       int num2=integer+integer2; //自动拆箱

        //收动拆箱
        int i = integer.intValue();
        int i1 = integer2.intValue();
        int num=i+i1;
        System.out.println(num);


        int aa=200;
        Integer integer1=aa;  //自动装箱

        Integer integer3 = Integer.valueOf(200); //手动装箱


        //都完成了哪些操作?
        Integer ii = 100; //自动装箱
        ii += 200;//自动拆箱,自动装箱


    }
}
  • C:注意事项
    在使用时,Integer x = null;代码就会出现NullPointerException。
    建议先判断是否为null,然后再使用。

6.Integer的面试题

public class MyTest3 {
    public static void main(String[] args) {
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);//false
        //Integer类重写了equals方法比值是否相同
        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); //true
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值