十、常用API

十、常用API

一、object常用方法、

1、toString

直接输出对象名,输出底层调用的是该对象的toString()

2、equel和==

==:可以比较基本数据类型,比较基本数据类型时,比较的是基本数据类型的值是否相同

也可以比较引用类型,比较引用类型的时候比较的是引用数据类型的地址是否相同 

如果想要比较内容是否相同,重写equals()方法,默认比较对象的地址是否相同。

String类已经重写改方法了,String类用equals可以直接比较内容

 

三、String

1、常见方法

    ①:构造方法

String string=new String("hello");//hello,把字符串封装成字符串对象

        char[] c={'J','i','a'};

        Stringstr=new String(c);//Jia把字符数组封装成字符串对象

        Strings=new String(c,1,2);//ia把一部分字符数组封装成字符串对象

    ②:常用方法方法

equals(),重写了String类型的equals方法所以可以直接比较字符串对象的内容是否相等。

charAt(),字符串的遍历

String s="12345";

        for (int i = 0; i <s.length(); i++) {

            System.out.print(s.charAt(i));

方法摘要

 char charAt(int index)

          返回指定索引处的 char 值。

int compareTo(String anotherString)

          按字典顺序比较两个字符串。

 intcompareToIgnoreCase(String str)

          按字典顺序比较两个字符串,不考虑大小写。

 String concat(String str)

          将指定字符串连接到此字符串的结尾。

 boolean contains(CharSequences)

          当且仅当此字符串包含指定的 char 值序列时,返回 true。

 booleancontentEquals(CharSequence cs)

          将此字符串与指定的 CharSequence 比较。

 booleancontentEquals(StringBuffer sb)

          将此字符串与指定的 StringBuffer 比较。

static String copyValueOf(char[] data)

          返回指定数组中表示该字符序列的 String。

static String copyValueOf(char[] data, int offset, int count)

          返回指定数组中表示该字符序列的 String。

 boolean endsWith(Stringsuffix)

          测试此字符串是否以指定的后缀结束。

 boolean equals(ObjectanObject)

          将此字符串与指定的对象比较。

 booleanequalsIgnoreCase(String anotherString)

          将此 String 与另一个 String 比较,不考虑大小写。  

 byte[] getBytes(Charsetcharset)

          使用给定的 charset将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。

 void getBytes(int srcBegin,int srcEnd, byte[] dst, int dstBegin)

          已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 方法,该方法使用平台的默认字符集。

 byte[] getBytes(StringcharsetName)

          使用指定的字符集将此String 编码为 byte 序列,并将结果存储到一个新的byte 数组中。

 void getChars(int srcBegin,int srcEnd, char[] dst, int dstBegin)

          将字符从此字符串复制到目标字符数组。

 int hashCode()

          返回此字符串的哈希码。

 int indexOf(int ch)

          返回指定字符在此字符串中第一次出现处的索引。

 int indexOf(int ch, intfromIndex)

          返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

 int indexOf(String str)

          返回指定子字符串在此字符串中第一次出现处的索引。

 int indexOf(String str, intfromIndex)

          返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

boolean isEmpty()

          当且仅当 length() 为 0 时返回true。

 int lastIndexOf(int ch)

          返回指定字符在此字符串中最后一次出现处的索引。

 int lastIndexOf(int ch, intfromIndex)

          返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。

 int lastIndexOf(String str)

          返回指定子字符串在此字符串中最右边出现处的索引。

 int lastIndexOf(String str,int fromIndex)

          返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。

 int length()

          返回此字符串的长度。

 boolean matches(String regex)

          告知此字符串是否匹配给定的正则表达式。

 String replace(char oldChar,char newChar)

          返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

 String replace(CharSequencetarget, CharSequence replacement)

          使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。

 String replaceAll(Stringregex, String replacement)

          使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。

 String replaceFirst(Stringregex, String replacement)

          使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。

 String[] split(String regex)

          根据给定正则表达式的匹配拆分此字符串。

 String[] split(String regex,int limit)

          根据匹配给定的正则表达式来拆分此字符串。

 boolean startsWith(Stringprefix)

          测试此字符串是否以指定的前缀开始。

 boolean startsWith(Stringprefix, int toffset)

          测试此字符串从指定索引开始的子字符串是否以指定前缀开始。

 CharSequence subSequence(intbeginIndex, int endIndex)

          返回一个新的字符序列,它是此序列的一个子序列。

 String substring(intbeginIndex)

          返回一个新的字符串,它是此字符串的一个子字符串。

 String substring(intbeginIndex, int endIndex)

          返回一个新字符串,它是此字符串的一个子字符串。

 char[] toCharArray()

          将此字符串转换为一个新的字符数组。

 String toLowerCase()

          使用默认语言环境的规则将此 String 中的所有字符都转换为小写。

 String toLowerCase(Localelocale)

          使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。

 String toString()

          返回此对象本身(它已经是一个字符串!)。

 String toUpperCase()

          使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

 String trim()

          返回字符串的副本,忽略前导空白和尾部空白。

static String valueOf(Object obj)     

 

 

 

2、==

String a="hello";

        Stringb="hello";

        Stringc=new String("hello");

        Stringd=new String("hello");

        System.out.println(a==b);//    true

        System.out.println(c==d);//    false

        System.out.println(c.equals(d));// false

        System.out.println(a.equals(b));// true

        System.out.println(a.equals(c));// true

1、直接赋值两个字符串指向的是方法区字符串常量池中相同的地址,new出来的指向堆内存不同的内存地址

 

*

四、StringBuilder

 

*每次进行字符串拼接操作都会构建一个新的对象,既耗时又浪费时间而StringBuilder可以解决这个问题

Stringbuilder内容是可变的,String是不可变的

 

2、stringbuilder方法

    append(),reverse()

 

 

十一、常用API(二)

一、Array

1、array的构造方法是被private修饰的,不能被访问,是常用工具类的做法(构造方法私有,方法用static修饰)。例如;math

 

 

二、基本包装类

1、Integer

    Integer.MAX_VALUES

Integer.MIN_VALUES

    *主要用于String的转换

    String.valueof()将任意类型的数据转成String

        方式一:

  Strings="100";

    Integer a=new Integer(s);

    int value = a.intValue();

    System.out.println(value);

方式二:

a.parseInt(s);

 

 

2、练习

      /*将字符串转成int数组,并排序之后储存在stringbuilder里,

         *再把stringbuilder转成string

         **/

        String s="1 34 23 4 5 12";

        String[] split = s.split(" ");

        int []a=new int[split.length];

        for (int i = 0; i < split.length;i++) {

            a[i]=Integer.parseInt(split[i]);

        }

       

        Arrays.sort(a);

        StringBuilder builder=newStringBuilder();

        for (int i = 0; i < a.length; i++) {

            if (i == a.length - 1) {

                builder.append(a[i]);

            } else {

                builder.append(a[i]+"");

            }

        }

       

        System.out.println(builder.toString());

 

三、自动拆箱和装箱

1、JDK1.5开始

    自动装箱:把基本的数据类型转化成包装类型

    自动拆箱:把包装类型转化成基本数据类型

*在开发过程中只要是对象使用时就要进行非空判断

   

 

    例:

­­­­        Integer i=100;//自动装箱

        i+=200;//i=i+200//拆箱-->装箱

       

        System.out.println("----------------");

        Integer a=20;//自动装箱

        Integer b=20;//自动装箱

        i=a+b;//(拆箱+拆箱)---->自动装箱

       

        /*反编译结果

        Integer i = Integer.valueOf(100);

        i = Integer.valueOf(i.intValue() + 200);

        System.out.println("----------------");

        Integer a = Integer.valueOf(20);

        Integer b = Integer.valueOf(20);

        i = Integer.valueOf(a.intValue() +b.intValue());

        */

 

四、Date类

1、构造方法:

        无参构造:系统当前时间

        有参构造方法(long 毫秒值):从1970.1.1 00:00:00起加上毫秒值

        1000毫秒=1秒

2、成员方法

Date date = new Date();

        longtime = date.getTime();

        System.out.println(time);//1517460363356

        System.out.println(time/ 1000 / 60 / 60 / 24 / 365);

        // 48:表示从1997年1月1日00:00:00开始到现在的毫秒数

        date.setTime(1000*6);

        System.out.println(date.getTime());

        //6000设置时间参数是毫秒值

 

 

 

 

 

 

 

 

3、simpleDate

SimpleDateFormat f=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss ");

        Stringformat = f.format(new Date());

        System.out.println(format);//dateàstring

       

        Strings="2018-02-01 13:09:41 ";

        Date date =f.parse(s);

        String d =f.format(date);

        System.out.println(d);//stringàdate

 

*工具类的特点成员方法静态,构造方法私有


 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值