Java基础(Arrays类、包装类)

这篇博客介绍了Java中的Arrays类,包括其方法如toString、sort和binarySearch等,并详细讲解了基本类型包装类的作用,如Integer类的构造方法和String与int类型的相互转换。还讨论了JDK5的自动装箱和拆箱特性,以及其他如Boolean、Long等包装类的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

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

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

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

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

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

 6、其他包装类演示


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

Arrays类概述
    针对数组进行操作的工具类。
    提供了排序,查找等功能

成员方法
    public static String toString(int[] a)
    public static void sort(int[] a)

public class MyTest {
    public static void main(String[] args) {
        //Java为了我们更加方便的操作数组,给我们提供了一个工具类 Arrays
        int[] arr = {4, 8, 6, 2, 12, 85, 36, 14, 25, 35, 52, 200, 30, 68, 98, 58};
        Arrays.sort(arr);
        String s = Arrays.toString(arr);
        System.out.println(s);
    }
    //重写了toString 方法
    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(", ");
        }
    }
}

    public static int binarySearch(int[] a,int key)

package org.westos.demo4;

import java.util.Arrays;

public class MyTest2 {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; //100
        //二分查找,前提数组元素有序
        int index = Arrays.binarySearch(arr, 60);
        System.out.println(index);
    }
    //binarySearch0方法源码
    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.
    }
}

    static boolean equals(int[] a, int[] a2) 比较两个数组中的元素,是否一样

package org.westos.demo4;

import java.util.Arrays;

public class MyTest3 {
    public static void main(String[] args) {
       // static boolean equals ( int[] a, int[] a2)比较两个数组中的元素,是否一样
        //比较两个数组中的元素,是否一样
        int[] arr1 = {10, 20, 30, 40};
        int[] arr2 = {10, 20, 30, 40};
        boolean b = equals(arr1, arr2);
        System.out.println(b);

        boolean b1 = Arrays.equals(arr1,arr2);
        System.out.println(b1);
    }
    //equals方法重写,源码
   public static boolean equals(int[] a, int[] b){
        if(a==b){
           return true;
        }
       if (a == null ||b == null){
           return false;
       }

       if (a.length!=b.length) {
           return false;
       }
       for (int i = 0; i < a.length; i++) {
           if (a[i]!=b[i]) {
               return false;
           }
       }
       return true;
    }

}

    static int[] copyOf(int[] original, int newLength)  
复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
    static int[] copyOfRange(int[] original, int from, int to)
复制旧数组中的指定范围间的几个元素到新数组中 from 起始索引 to 终止索引

package org.westos.demo4;

import java.util.Arrays;

public class MyTest4 {
    public static void main(String[] args) {
        //static int[] copyOf ( int[] original, int newLength)
        // 复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
        int[] arr1 = {10, 20, 30,50,90,69, 40};

        int[] newArray = Arrays.copyOf(arr1,2);

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

        System.out.println("==========================");
        //static int[] copyOfRange(int[] original, int from, int to)
        // 复制旧数组中的指定范围间的几个元素到新数组中 from 起始索引 to 终止索引
        int[] ints = Arrays.copyOfRange(arr1, 2, 6); //含头不含尾
        System.out.println(Arrays.toString(ints));
    }
}

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

基本类型包装类
    为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型.

常用操作:    常用的操作之一:用于基本数据类型与字符串之间的转换。

基本类型和包装类的对应
    byte——Byte
    short——Short
    int ——Integer
    long——Long
    float——Float
    double——Double
    char——Character
    boolean——Boolean

 需求:
    a:将100转换成二进制 , 八进制 , 十六进制
    b:判断一个数是否在int的范围内

package org.westos.demo;

public class MyTest {
    public static void main(String[] args) {
       /* 
        需求:
        a:
        将100转换成二进制, 八进制, 十六进制
        b:
        判断一个数是否在int的范围内*/
       int num=100;
        //Integer integer = new Integer(num);
       /* static String toBinaryString ( int i)
        以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
        static String toHexString ( int i)
        以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
        static String toOctalString ( int i)
        以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。*/
        String s = Integer.toBinaryString(num);
        String s1 = Integer.toHexString(num);
        String s2 = Integer.toOctalString(num);
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);

     /*   static int MAX_VALUE
        值为 231-1 的常量,它表示 int 类型能够表示的最大值。
        static int MIN_VALUE
        值为 -231 的常量,它表示 int 类型能够表示的最小值。
*/
        if(Integer.MIN_VALUE<=55555&&55555<=Integer.MAX_VALUE){

            System.out.println("在Int类型的范围内");
        }


    }
}

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

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

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

构造方法
    public Integer(int value)
    public Integer(String s)  //要个一个字面上是数字的字符串,如果不是就会报错

package org.westos.demo;

public class MyTest2 {
    public static void main(String[] args) {
       /* Integer( int value)
        构造一个新分配的 Integer 对象,它表示指定的 int 值。
        Integer(String s)
        构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。*/
        int num = 100;
        Integer integer = new Integer(num);
        //Integer 类重写了父类的toString()方法,把他包装的值,转换成了字符串。
        String s = integer.toString();
        System.out.println(s);

        //构造方法要的是 字面上是一个有效数字的字符串 不然就会报 NumberFormatException 异常
        Integer integer1 = new Integer("100");
    }
}

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

int ——String
    a:和""进行拼接
    b:public static String valueOf(int i)
    c:int -- Integer -- String
    d:public static String toString(int i)

String——int
    a:String -- Integer -- intValue();
    b:public static int parseInt(String s)

package org.westos.demo;

public class MyTest3 {
    public static void main(String[] args) {
        // int======String   100---------"100"
        int num=100; //"100"
        //方式1
        String s=num+"";
        //方式2
        String s1 = String.valueOf(num); //记这个
        //方式3
        String s2 = new Integer(num).toString();

        //String=======int  "100"-----100
        String a="100";
        //方式1
        Integer integer = new Integer(a);
        int i = integer.intValue();
        System.out.println(i);
        //方式2:直接调用Integer类中的静态方法
        int i1 = Integer.parseInt(a); //经常用这个
        System.out.println(i1);

    }
}

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

JDK5的新特性
    自动装箱:把基本类型转换为包装类类型
    自动拆箱:把包装类类型转换为基本类型

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

package org.westos.demo;

public class MyTest4 {
    public static void main(String[] args) {
        //JDK1.5之后提供的语法
        //自动拆箱:将包装类型自动转换成他所对应的基本类型。
        //自动装箱:将基本类型自动转换成他所对应的包装类型。
        int num=100;
        //Integer integer = new Integer(num);
        Integer a=num; // 自动装箱
        Integer b=20;  // 自动装箱

        Integer x = new Integer(1);
        Integer y = new Integer(2);
        //自动拆箱
        int sum=x+y;
        System.out.println(sum);

    }
}

 6、其他包装类演示

boolean----- Boolean 

package org.westos.demo3;

public class MyTest {
    public static void main(String[] args) {
         /*
       为了我们使用方便,Java针对我们的8中基本数据类型,都提供了他所对应的包装类型
       基本类型    包装类型  
        boolean----- Boolean
       * */
         double num=3.14; //"3.14"

        Double aDouble = new Double(num);
        String s = aDouble.toString();

        String str="1.36"; //1.36
        double v = Double.parseDouble(str);
        System.out.println(v);

        Double aDouble1 = new Double(str);
        double v1 = aDouble1.doubleValue();

        Double aDouble2 = Double.valueOf("3.145");
        Double aDouble3 = Double.valueOf(3.14);


        Double a=36.9;




    }
}

long——Long
boolean——Boolean 

package org.westos.demo3;

public class MyTest2 {
    public static void main(String[] args) {
        long num=100L;
        String s = String.valueOf(num);
        String s1 = new Long(num).toString();
        System.out.println(s1);
        long l = Long.parseLong("155555555555555555");
        Long aLong = new Long("55555555555555555555");
        long l1 = aLong.longValue();
        Long num2=2000L;
        
        String  ss="true"; //true
        Boolean aBoolean = new Boolean(ss);
        boolean b = aBoolean.booleanValue();
        boolean aFalse = Boolean.parseBoolean("false");
        Boolean aBoolean1 = Boolean.valueOf(true);
        Boolean b5=false;
        
    }
}

 char——Character 中判断方法

package org.westos.demo3;

public class MyTest3 {
    public static void main(String[] args) {
        //char
       // Character

        char ch='a';

        boolean lowerCase = Character.isLowerCase(ch);//是否小写
        System.out.println(lowerCase);

        boolean upperCase = Character.isUpperCase(ch);//是否大写
        System.out.println(upperCase);

        boolean letter = Character.isLetter(ch);//是否字母
        System.out.println(letter);

        boolean digit = Character.isDigit(ch);//是否数字
        System.out.println(digit);

        Character.isLetterOrDigit(ch);

        boolean spaceChar = Character.isSpaceChar(ch);//是否空格
        System.out.println(spaceChar);


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值