美文网首页
Java基础系列28-常用api之包装类

Java基础系列28-常用api之包装类

作者: 只是甲 | 来源:发表于2021-08-26 17:18 被阅读0次

一.包装类概述

基本数据类型包装类最常用就是用于和字符串之间进行相互转换。

需求:我要判断一个数据是否在int范围内?
  要想判断一个数据是否在int范围内,首先我们得知道int范围,在前面我们讲解基本数据类型的时候说过了:
    1)-2147483648 到 2147483647
    2)为了对基本数据类型进行更多更方便的操作,Java就针对每一种基本数据类型提供了一个对应的引用类型。

基本类型包装类:

  1. Byte byte
  2. Short short
  3. Integer int
  4. Long long
  5. Float float
  6. Double double
  7. Character char
  8. Boolean boolean

二.Integer类

Integer类 在对象中包装了一个基本类型 int 的值。

构造方法:

  1. Integer(int value)
  2. Integer(String s)

注意:
这个字符串必须由数字字符组成

代码:

package Java_study;

/**
 * 
 * @author  只是甲
 * @date    2021-07-01
 * @remark  Integer类的概述和构造方法
 *
 */
public class integer1 {
    public static void main(String[] args) {
        //Integer(int value)
        int value = 100;
        Integer i = new Integer(value);
        System.out.println(i); //100
        System.out.println("----------");
        
        //Integer(String s)
        String s = "100";
        //NumberFormatException:数据格式化异常
        //String s = "abc";
        Integer ii = new Integer(s);
        System.out.println(ii);
    }

}

测试记录:

100
----------
100

三. int <=> String 相互转换

int类型和String类型的相互转换:

  1. int --> String
    1)String类中:String.valueOf()
    public static String valueOf(int i)

  2. String --> int
    1)Integer类中:Integer.parseInt()
    public static int parseInt(String s)

代码:

package Java_study;

/**
 * 
 * @author  只是甲
 * @date     2021-07-02
 * @remark  int类型和String类型的相互转换
 *
 * * int       -->  String
 * 方式一:  "" + int;
 * 方式二: String类中   public static String valueOf(int i) {
 *                         return Integer.toString(i);
 *                      }
 *
 * String  -->  int
 * 方式一:   Integer的带参构造  new Integer(String); int = Integer.intValue();
 * 方式二: Integer类中  public static int parseInt(String s) throws NumberFormatException {
 *                          return parseInt(s,10);
 *                      }
 */

public class integer2 {
    public static void main(String[] args) {
        //TODO [int --> String]
        int number = 100;
        //方式一
        String s1 = "" + number;
        System.out.println(s1);//100
        //方式二 public static String valueOf(int i)
        String s2 = String.valueOf(number);
        System.out.println(s2);
        System.out.println("----------");
        
        //TODO [String --> int]
        String s = "100";
        //方式一 String --> Integer --> Integer.intValue --> int
        Integer i = new Integer(s);
        int x = i.intValue();
        System.out.println(x);
        //方式二   public static int parseInt(String s)
        int y = Integer.parseInt(s);
        System.out.println(y);
    }

}

测试记录:

100
100
----------
100
100

四. Integer练习之把字符串中的数据排序

需求:
我有如下一个字符串:”91 27 46 38 50”
请写代码实现最终输出结果是:”27 38 46 50 91”

提示:
这里需要参考String类中的方法

public String[] split(String regex)

分析:

  1. 定义一个字符串对象
  2. 把字符串中的数字数据存储到一个int类型的数组中
  3. 对int数组进行排序
  4. 把排序后的数组中的元素进行拼接得到一个字符串
  5. 输出字符串

代码:

package Java_study;

import java.util.Arrays;

/**
 * 
 * @author 只是甲
 * @date   2021-07-02
 * @remakr 
 *
 */
public class integer3 {
    public static void main(String[] args) {
        //定义一个字符串对象String
        String s = "91 27 46 38 50";
        
        //把字符串中的数字数据存储到一个int类型的数组中
        //String -> String.split(String regex) -> String[] strArr
        String[] strArr = s.split(" ");
        
        //定义一个数组 String[] strArr -> int[] arr
        int[] arr = new int[strArr.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(strArr[i]);
        }
        
        //对int数组进行排序 int[] arr -> Arrays.sort -> int[] arr
        Arrays.sort(arr);
        
        //int --> StringBuilder
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if (i == 0) sb.append("[");
            sb.append(arr[i]);
            if (i != arr.length - 1) sb.append(" ");
            if (i == arr.length - 1) sb.append("]");
        }
        
        //查看结果
        System.out.println("遍历排序后的int数组:");
        System.out.println(sb.toString());
    }

}

测试记录:

遍历排序后的int数组:
[27 38 46 50 91]

参考:

  1. https://blog.csdn.net/qq_43529621/article/details/115858596

相关文章

网友评论

      本文标题:Java基础系列28-常用api之包装类

      本文链接:https://www.haomeiwen.com/subject/bmckultx.html