20200810

StringBuilder类

字符串缓冲区,可以提高字符串的操作效率(看成一个长度可以变化的字符串)

底层也是一个数组,但是没有被final修饰,可以改变长度

构造方法:

public StringBuilder ( )  :构造一个空的StringBuilder容器

public StringBuilder (String str )  构造一个StringBuilder容器,并将字符串添加进去

public class Demo01StrringBuilder {
    public static void main(String[] args) {
        StringBuilder bu1 = new StringBuilder();
        System.out.println("bu1:" + bu1);//bu1:

        StringBuilder bu2 = new StringBuilder("abc");
        System.out.println("bu2:" + bu2);//bu2:abc

    }
}

StringBuilder类的成员方法:

public StringBuilder append (...) :添加任意数据类型的字符串形式,并返回当前对象自身

使用append方法无需接收返回值

链式编程:方法的返回值是一个对象,可以根据对象继续调用方法

public class Demo02StringBuilder {
    public static void main(String[] args) {
        StringBuilder bu1 = new StringBuilder();
        StringBuilder bu2 = bu1.append("abc");
        System.out.println(bu1);//abc
        System.out.println(bu2);//abc
        System.out.println(bu1 == bu2);//true 两个对象是同一个对象

        bu1.append("Imagine");
        bu1.append(0501);
        bu1.append(true);
        bu1.append(12.18);
        bu1.append("三千");
        System.out.println(bu1);//abcImagine0501true12.18三千

        //链式编程
        StringBuilder bu3 = new StringBuilder();
        bu3.append("几秋").append(8.10).append("miss").append("三千");
        System.out.println(bu3);//几秋8.1miss三千
    }
}

toString方法

StringBuilder和String可以互相转换

String ---> StringBuilder :可以使用StringBuilder的构造方法

StringBuilder (String str) 构造一个字符串生成器,并初始化为指定的字符串内容

StringBuilder --->String :可以使用StringBuilder中的toString方法

public String toString ( ) : 将当前StringBuilder对象转换为String对象

public class Demo02toString {
    public static void main(String[] args) {
        //String --> StringBuilder
        String str = "hello";
        System.out.println("str:" + str);
        StringBuilder bu = new StringBuilder(str);//str:hello

        //往StringBuilder中添加数据
        bu.append("world");
        System.out.println("bu:" + bu);//bu:helloworld

        //StringBuilder --> String
        String s = bu.toString();
        System.out.println("s:" + s);//s:helloworld


    }
}

包装类

装箱:把基本类型的数据,包装到包装类中(基本类型的数据 ---> 包装类)

构造方法:

Integer (int value)  :构造一个新分配的 Integer对象,它表示指定的int值

传递的字符串,必须是基本类型的字符串,否则会抛出异常

静态方法:

static Integer valueOf (int i)  : 返回一个表示指定的 int 值的Integer实例

stativ Intreger valueOf (String s)  : 返回保存指定的String的值的Integer对象

拆箱:在包装类中取出基本类型的数据(包装类 --> 基本类型的数据)

成员方法:

int intValue ( )  : 以 int 类型返回该Integer的值

public class Demo03Integer {
    public static void main(String[] args) {
        //装箱
        Integer in1 = new Integer(1);
        System.out.println(in1);//1 重写了toString方法

        Integer in2 = new Integer("1");
        System.out.println(in2);//1

        //静态方法
        Integer in3 = Integer.valueOf(1);
        System.out.println(in3);//1

        Integer in4 = Integer.valueOf("1");
        System.out.println(in4);//1
        
        //拆箱
        int i = in1.intValue();
        System.out.println(i);//1
    }
}

自动装箱与自动拆箱:

基本类型的数据和包装类之间可以自动的互相转换

自动装箱:直接把int类型的整数赋值给包装类

自动拆箱:in是包装类,无法直接参与运算,可以自动转换为基本类型的数据,再参与计算

public class Demo03Integer2 {
    public static void main(String[] args) {
        //自动装箱
        //Integer in = 1 ; 就相当于 Integer in = new Integer (1);
        Integer in = 1;

        //自动拆箱
        //in + 2;就相当于 in.intValue() + 3 = 3;
        //in = in + 2;就相当于 in = new Integer (3)自动装箱
        in = in + 2;
        System.out.println(in);//3
        
        //ArrayList集合无法直接存储整数,可以存储Integer包装类
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);//自动装箱 list.add(new Integer(1));
        int a = list.get(0);//自动拆箱 list.get(0).intValue();

    }
}

基本类型与字符串之间的转换

基本类型 ---> 字符串

  • 基本类型数据的值 + “ ” 最简单的方式
  • 使用包装类中的静态方法
  • static String toString(int i)  返回一个表示指定整数的String对象

使用String类中的静态方法

static String valueOf (int i)  返回int参数的字符串表示形式

字符串 -----> 基本类型

使用包装类的静态方法parseXX("字符串")

Integer类 : static int parseInt (String s)

Double类 : static double parseDouble (String s)

..........

public class Demo04Integer {
    public static void main(String[] args) {
        //基本类型 --> 字符串
        String s1 = 100 + "";
        System.out.println(s1 + 200);//100200

        String s2 = Integer.toString(100);
        System.out.println(s2 + 200);//100200

        String s3 = String.valueOf(100);
        System.out.println(s3 + 200);//100200
        System.out.println("==========");

        //字符串 --> 基本类型
        int i = Integer.parseInt("100");
        System.out.println(i + 200);//300
    }
}

 

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值