Java_基本类型包装类_Integer

Integer包装类: 

package com.hb2.基本类型包装类;

/*
      构造方法:

              public Integer (int value), 根据int值创建Integer对象(已过时)

              public Integer (String s), 根据String值创建Integer对象(已过时)


      静态方法获取对象:

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

public class interger {
    public static void main(String[] args) {

        System.out.println(Integer.MIN_VALUE);//int的最小值
        System.out.println(Integer.MIN_VALUE);//int的最大值
        
        Integer i1 = Integer.valueOf(100);
        System.out.println(i1);

        Integer i2 = Integer.valueOf("123");//只能输入数字字符串
        System.out.println(i2);

    }
}

int 和 String 相互转换:

int 转 String:

 方法1:

int number = 100;
String s1 = ""+number;
System.out.println(s1);

方法2: 

//public static String valueOf(int i)
int number = 100;
String s2 = String.valueOf(number);
System.out.println(s2);

方法3: 

//        整数->字符串的另一种方法:Integer.toString(int num)
        int num = 8;
        String arr = Integer.toString(num);
        System.out.println(num);

String 转 int: 

方法1: 

        

//String --> Integer --> int
//将Integer作为桥梁 
//public int inValue(): 将Integer转成int方法    

String s = "100";
Integer i = Integer.valueOf(s);//将String 转为Integer

int x = i.intValue();//将Integer转成int
System.out.println(x);

方法2:  

//public static int parseInt(String s):String 转 int方法

String s3 = "1234";
int y = Integer.parseInt(s3);
System.out.println(y);

字符串中的数字数据排序: 

package com.hb2.基本类型包装类;
/*
    得到字符串中每一个数字数据
       public String[] split (String regex)
    定义一个int数组,把 String[] 数组中的每一个元素存储到int数组中
       public static int parseInt (String s)
 */

import java.util.Arrays;

public class 字符串中数据排序 {
    public static void main(String[] args) {
        String s = "91 27 46 38 50";

        //把字符串中的数字数据存储到一个String类型的数组中
        String[] strArray = s.split(" ");//以空格分割得到一个字符数组
        for (int i = 0; i < strArray.length; i++) {
            System.out.println(strArray[i]);
        }

        //定义一个int数组,把 String[]数组中的每一个元素存储到int数组中
        int[] arr = new int[strArray.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(strArray[i]);
        }

        Arrays.sort(arr);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]+" ");
        }
        String result = sb.toString();

        System.out.println(result);

    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值