包装类与一些常用的类

集合的使用

包装类与一些常用的类

1.包装类与基本数据类型之间的转换:

1.将基本数据类型转换成包装类:

1.构造方法进行转换:

Integer a=new Integer(i);
System.out.println(a);
Integer b=new Integer("123");
System.out.println(b+1);
Character char1=new Character('a');

2.通过包装类.valueof(变量)进行转换:

int m=10;
Integer l=Integer.valueOf(m);
char ch='男';
Character o=Character.valueOf(ch);
2.将包装类变成变成基本数据类型
  1. 通过 变量.基本数据类型value()方法 :

    int e=b.intValue();
    
3.将包装类变成字符串:
  1. 通过+“”方式:

    int i=10;
      //将基本数据类型变成包装类;
    Integer a=new Integer(i);
    String str2=a+"";
    System.out.println(str2);
    
  2. 通过变量.toString()方法:

    Integer b=new Integer("123");
    String str=b.toString();
    System.out.println(str);
    
4.将字符串转换成基本数据类型中
  1. 通过 包装类.parse基本变量(变量)方法:

    String str3="123.5";
    double g=Double.parseDouble(str3);
    String str4="23";
    int f=Integer.parseInt(str4);
    
  2. 注意点:char不存在parse方法,并且不支持将string类型转换成charactor;

2. 常用的类:

1.Math类(静态类):
  1. Math.random():随机生成0-1的数,左闭右开;

  2. Math.pow(变量,次方)

  3. Math.max(变量1,变量2)

  4. Math.min(变量1,变量2)

  5. Math.abs(变量)绝对值

    int a=10;
    int b=15;
    int c=Math.max(a, b);
    int d=Math.min(a, b);
    //生成0(包括0)-1(不包括1)之间的数
    double e=Math.random();
    int  f=(int)Math.random()*10;
    
2.random类(非静态)
  1. 声明对象:

    Random random=new Random();
    random.nextInt(10);
    
3.String类

计算字符串长度:length()方法

string的equals()方法:
  1. 判断字符串内容是否相同

    String str1="软件工程";
    String str2="软件工程";
    String str3="软件开发";
    //返回true
    System.out.println(str1.equals(str2));
    //返回false
    System.out.println(str1.equals(str3));
    
  2. 基本数据类型不存在equals()方法;

  3. 比较两个字符串内容是否相同;==比较是否是同一地址

    String str5="软件工程";
    String str4="软件工程";
    //返回true,因为只创建了一个对象
    System.out.println(str5==str4);
    

在这里插入图片描述

String str6="软件工程";
String str7=new String("软件工程");
//返回false,因为只创建了两个对象 导致地址值不同
System.out.println(str6==str7);

在这里插入图片描述

//共创建4个对象,堆中两个,字符串池中两个,
String str8=new String("软件信息");
String str9=new String("软件工程");
//返回false
System.out.println(str8==str9);
//返回true
System.out.println(str8.equals(str9));

在这里插入图片描述

4.string一些其他的比较方法:

  1. equalsIgnoreCase()方法 比较不分大小写

    String str10="Good";
    String str20="good";
    //返回true
    System.out.println(str10.equalsIgnoreCase(str20));
    
string的相关操作
  1. 变大小写: tolowcase(),touppercase()方法

    1. str10="Good";
      //返回GOOD
              System.out.println(str10.toUpperCase());
              //返回good
              System.out.println(str10.toLowerCase());
      
  2. 去除字符串前后空格:trim()方法

    1.  String str23="        good     ";
              //返回good
              System.out.println(str23.trim());
      
  3. 字符串提取操作:

    1. indexOf()方法,查找摸个元素,返回下标不存在返回-1:

      String str33="hello word";
              //查找第一个o的下标4
              System.out.println(str33.indexOf("o"));
              //查找最后一个o的下标7
              System.out.println(str33.lastIndexOf("o"));
      
    2. substring(开始,结束)方法:用于截取字符串 左闭右开

      String str33="hello word";
      //返回llo w
              System.out.println(str33.substring(2, 7));
      
  4. 字符串的拆分 split()方法,返回数组

    String songs="从小丘西行百二十步,隔篁竹,闻水声,如鸣珮环,心乐之。伐竹取道,下见小潭,水尤清冽。";
            String[] strs=songs.split(",");
            /**
             * 返回从小丘西行百二十步
             * 隔篁竹
             * 闻水声
             * 如鸣珮环
             * 心乐之。伐竹取道
             * 下见小潭
             * 水尤清冽。
             */
            for (String i:
                 strs) {
                System.out.println(i);
    
            }
    
  5. 字符串的连接

  6. 使用+号进行连接

 String str11="hello";
        String str22="everyone";
        //返回helloeveryone
        System.out.println(str11+str22);

2.使用concat()方法

 

```java
String str11="hello";
        String str22="everyone";
        //返回helloeveryone
        System.out.println(str11.concat(str22));

#### 4.StrintBuffer类的使用

1. 当字符串需要频繁操作时就用它,效率更高;

2. 添加  插入   tostring方法
```java
    StringBuffer buff=new StringBuffer("hello");
           System.out.println(buff.length());
           //stringbuffer 对象变成 string对象
           String buf=buff.toString();
           //添加元素
           buff.append("word");
           //返回helloword
           System.out.println(buff);
           //插入元素
           buff.insert(2, "11");
           //返回he11lloword
       System.out.println(buff);
  buff.append("word");
           //返回helloword
           System.out.println(buff);
           //插入元素
           buff.insert(2, "11");
           //返回he11lloword
           System.out.println(buff);






































​      



​     

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值