Arrays类的概述和方法使用、基本类型包装类、Integer类、String和int类型的相互转换、自动装箱和拆箱

Arrays类的概述和方法使用

A:Arrays类概述
针对数组进行操作的工具类。
提供了排序,查找等功能。
B:成员方法
public static String toString(int[] a)
public static void sort(int[] a)
public static int binarySearch(int[] a,int key)

案例一:

public class ArrayDemo2 {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
        //int index=findIndex(arr,40);
        //System.out.println(index);
        //二分查找的前提:是数组元素必须有序
        int index = selectIndex(arr, 80);
        System.out.println("索引是" + index);
    }

    private static int selectIndex(int[] arr, int ele) {
        //定义三个索引
        int minIndex = 0;
        int maxIndex = arr.length - 1;
        int centerIndex = (minIndex + maxIndex) / 2;

        while (minIndex <= maxIndex) {
            if (ele == arr[centerIndex]) {
                return centerIndex;
            } else if (ele < arr[centerIndex]) {
                maxIndex = centerIndex - 1;
            } else if (ele > arr[centerIndex]) {
                minIndex = centerIndex + 1;
            }
            //重新计算中间索引
            centerIndex = (minIndex + maxIndex) / 2;
        }
        return -1;
    }

    private static int findIndex(int[] arr, int ele) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == ele) {
                return i;
            }
        }
        return -1;
    }
}

案例二:

import java.util.Arrays;
public class MyDemo {
    public static void main(String[] args) {
       // Arrays 数组工具类,针对数组进行的一些操作

        //B:
        //成员方法
        //public static String toString ( int[] a)
        //public static void sort ( int[] a)
        //public static int binarySearch ( int[] a, int key)
        //static void sort ( int[] a, int fromIndex, int toIndex)
        //对指定 int 型数组的指定范围按数字升序进行排序。

        int[] arr = {1, 2, 45, 90, 43, 65, 87, 99, 234, 987, 154, 92, 12, 19000, 1768, 2909};
        Arrays.sort(arr, 0, arr.length);//只排序一段

        System.out.println(Arrays.toString(arr));
        //二分查找:元素必须有序
        int index = Arrays.binarySearch(arr, 900000);
        System.out.println(index);


        //String s = showArray(arr);
        //System.out.println(s);
    }

    private static String showArray(int[] arr) {
        StringBuilder sb=new StringBuilder("[");
        for (int i = 0; i < arr.length; i++) {
            if(i==arr.length-1){
                sb.append(arr[i]).append("]");
            }else{
                sb.append(arr[i]).append(",");
            }
        }

        return sb.toString();


    }
}

案例三:

import java.util.Arrays;
public class ArrayDemo2 {
    public static void main(String[] args) {
        //static boolean equals ( int[] a, int[] a2)
        //如果两个指定的 int 型数组彼此相等,则返回 true。
        int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
        int[] arr2 = {10, 20, 30, 40, 50, 60, 70, 80, 90, 10};
        boolean b = Arrays.equals(arr, arr2);
        System.out.println(b);

        //static void fill ( int[] a, int val)
        //将指定的 int 值分配给指定 int 型数组的每个元素。

        Arrays.fill(arr, 222);

        //static void fill ( int[] a, int fromIndex, int toIndex, int val)
        //将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。
        System.out.println(Arrays.toString(arr));
    }
}

基本类型包装类的概述

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 MyDemo {
    public static void main(String[] args) {
        //A:
        //需求:
        //a:
        //将100转换成二进制, 八进制, 十六进制
        //b:
        //判断一个数是否在int的范围内

        //  int num=100;
        //     <= 100000000<=

        //Java为了我们更方便的去操作基本数据类型,给我们提供与之对应的包装类型
        //基本类型                  包装类型
        //byte -----------------------Byte
        //short------------------------Short
        //int--------------------------Integer
        //long---------------------------Long
        //float------------------------ Float
        //double----------------------- Double
        //char ------------------------Character
        //boolean -----------------------Boolean

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

       // System.out.println(new Object().toString());

        //public String toString () {
        //    return getClass().getName() + "@" + Integer.toHexString(hashCode());
        //}
        boolean b= 100000>=Integer.MIN_VALUE&& 100000<=Integer.MAX_VALUE?true:false;
        System.out.println(b);
    }
}

Integer类的概述和构造方法

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

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

B:构造方法
public Integer(int value)
public Integer(String s)

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:案例演示
案例:
使用Integer转换int–String及String–int

  1. int i1 = Integer.parseInt(str2);
  2. int i = integer.intValue();//将包装类型,转换成他所对应的基本类型
public class MyTest {
    public static void main(String[] args) {
        //A:
        //int --String
        int num=100;
        String str=num+"";
        String.valueOf(num);

        String s = new Integer(num).toString();


        //String -----int
        String str2="50";
        //方式1
        Integer integer = new Integer(str2);
        int i = integer.intValue();//将包装类型,转换成他所对应的基本类型
        System.out.println(i+1);

        //方式2

        int i1 = Integer.parseInt(str2);
        System.out.println(i1);




    }
}

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

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

Integer ii = 100;
ii += 200;

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

案例:

public class MyTest {
    public static void main(String[] args) {
        Integer ii = 100;//自动装箱


        ii += 200;//自动拆箱。自动装箱

        Integer integer = new Integer(1000);
        int i = integer.intValue(); //手动拆箱

        Integer integer1 = Integer.valueOf(100);//手动装箱
        Integer integer2 = Integer.valueOf("10000");
    }
}

案例二:

public class MyTest2 {
    public static void main(String[] args) {
        //JDK1.5之后,引入的自动拆装箱
        //自动拆箱:将包装类型自动转换成他所对应的基本类型
        //自动装箱:将基本类型自动转换成他所对应的包装类型
        Integer num=new Integer(100);
        Integer num2=100;//自动装箱
        int num3=50;

        int i = num.intValue();//手动拆箱
        int r=num+num3;
        System.out.println(r);

    }
}

案例三:

public class MyTest {
    public static void main(String[] args) {
        //String toString ()
        //返回一个表示该 Integer 值的 String 对象。
        //static String toString ( int i)
        //返回一个表示指定整数的 String 对象。

        //int intValue ()
        //以 int 类型返回该 Integer 的值。
        //static int parseInt (String s)
        //将字符串参数作为有符号的十进制整数进行解析。

        //static Integer valueOf ( int i)
        //返回一个表示指定的 int 值的 Integer 实例。
        //static Integer valueOf (String s)
        //返回保存指定的 String 的值的 Integer 对象。


        String string = Integer.
        toString(100);


        String string1 = new Integer(100).toString();

    }
}

案例四:

public class MyTest2 {
    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("-----------");
        //这个值,超过了一个字节的范围,回去重新new一个Integer对象
        Integer i5 = 128;
        Integer i6 = 128;
        System.out.println(i5 == i6);//false
        System.out.println(i5.equals(i6));//true
        System.out.println("-----------");

        // Integer integer = Integer.valueOf(127);
        //没有超过一个字节 会从缓存里面取出一个对象
        Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8);//true
        System.out.println(i7.equals(i8));//true


        Integer a = new Integer(127);


        Integer b = 127;
        System.out.println(a == b);

        Integer.valueOf(100);
    }
}

注意:Integer赋值超过了127范围会重新new对象所以两次赋值地址值不同;如果在-128到127则会直接取出引用其地址值不用重新new对象。
没有超过一个字节的范围 因为在方法区中存在一个 字节常量池 范围-128—127

案例四:

public class MyTest4 {
    public static void main(String[] args) {
        boolean b=true;
        Boolean aBoolean = new Boolean(b);
        String str="false";
        Boolean aBoolean1 = new Boolean(str);
        boolean b1 = aBoolean.booleanValue();
        Boolean aBoolean2 = Boolean.valueOf(true);

        String str2 = "false";
        boolean b2 = Boolean.parseBoolean(str2);


    }
}

案例五:

import java.time.LocalDate;
c class MyTest5 {
    public static void main(String[] args) {
        long num=1L;
        Long aLong = Long.valueOf(num);
        long l = aLong.longValue();

        long l1 = Long.parseLong("100000000");

    }
}

案例六:

public class MyTest6 {
    public static void main(String[] args) {
        char ch='a';
        Character character = new Character(ch);
        char c = character.charValue();

        //static boolean isLetter ( char ch)
        //确定指定字符是否为字母。
        //
        //static boolean isLowerCase ( char ch)
        //确定指定字符是否为小写字母。
        //
        //static boolean isUpperCase ( char ch)
        //确定指定字符是否为大写字母。
        //
        //static boolean isDigit ( char ch)
        //确定指定字符是否为数字。
        //
        //static boolean isSpaceChar ( char ch)
        //确定指定字符是否为 Unicode 空白字符。

        boolean spaceChar = Character.isSpaceChar(' ');
        System.out.println(spaceChar);


    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值