2020.10.19-2020.10.23

一、内部类(inner class)

1、定义

  在一个类中,定义另一个类的代码结构,通常定义在类内部的类称为 “内部类” ,外面的类称为“外部类” , 在逻辑关系上 内部类与外部类是从属关系,比如 一个People类 存在收货地址类(收货人,收货联系方式)

2、分类

   2.1、 普通内部类(inner class),一个类A中定义另一个类B,其中类B就是类A的内部类,也是类A的一部分

    private String pname="张三";
    public void sayHello(){
        System.out.println("Let us say Hello");
        // 知识点1 :外部类的方法中,可以使用内部类的属性、方法
        Address address = new Address();
        address.addressName ="湖北武汉";
        address.contentName="张某某";
        address.showAddressInfo();
    }
 /**
     * 定义普通内部类   收货地址
     */
     class Address{
        private String addressName;// 收货地址
        private String contentName;// 联系人
        public void showAddressInfo(){
            System.out.println("联系人:"+contentName + "--收货地址:"+addressName);
            // 内部类的方法 可以直接访问外部类的属性  (由于通常情况属性的访问必须通过对象才可以使用)
             System.out.println("访问外部类的属性:"+pname);
     }
}

  注意两点:
  外部类的方法中,可以直接访问内部类的所有成员(包括私有)
  内部类的方法中,也可以直接方法外部类的所有成员,当外部和内部的成员名相同时,就近原则访问成员,或者引入外部类的对象访问
  2.2、 静态内部类(static inner class): 在普通内部类基础上,增加“static”关键字,与静态方法相似,满足静态的要求

public class People{
/**
     * 2、定义静态内部类
     *   卡信息
     */
    static  class Card{
        private static String cardNo="4200018888000022";
        private String cardName="身份证";
        // 定义静态方法
        public static void showCard(){
            System.out.println("身份证号:"+ cardNo);
        }
        // 定义非静态方法
        public void showCard2(){
            System.out.println("cardName:"+cardName + "----"+ cardNo);
        }
    }
    // 外部类的方法 
    public void method2(){
        Card card = new Card();
        // 对于静态方法可以直接类名.方法名
        // 对于非静态方法,需要创建Card类的对象访问
        card.showCard2();
    }
}
使用:
        // 2 创建静态内部类的对象
        People.Card.showCard();
        // 创建静态内部类的对象
        People.Card  card = new People.Card();
        card.showCard2();

  2.3、方法内部类: 在一个方法中定义的类,其中这个类只属于该方法,也只能在该方法中使用

 /**
     * 3、方法内部类 (将一个类定义在方法里面)
     */
    public void method3(){
         int score = 98;
         // 在这里定义一个类
        class MyClass{
            String subject="Java";
            public void getSubjectScore(){
                //方法内部类中 也可以使用方法的属性
                System.out.println(pname+"的"+subject+":"+score);
            }
        }
        //调用方法里面的类
        MyClass  mycls = new MyClass();
        mycls.getSubjectScore();
    }
 People  people = new People();
    // 3 调用方法
        people.method3();

  2.4 匿名内部类: 定义一个没有类名,只有对方法的具体实现。通常它依赖于实现关系(接口)或继承关系(父类)
  a、基于实现关系

public interface MyInterface {
    // 学习
    public void  study();
    //  工作
    public void work();
}
// 创建一个匿名类(让接口的引用 指向匿名类的对象)
        MyInterface  person = new MyInterface() {
            @Override
            public void study() {
                System.out.println("这个人也好好学习");
            }
            @Override
            public void work() {
                System.out.println("这个人也好好工作");
            }
        };
        person.study();
        person.work();

  b、基于继承关系

public class MyClass {
   public void service(){
        System.out.println("提供服务的方法。");
    }
}
// 父类 new 一个 匿名类,这个匿名类是它的子类
        MyClass cls = new MyClass(){
            @Override //匿名类重写父类的方法 service
            public void service() {
                System.out.println("这是子类的方法");
            }
        };
        cls.service();

二、异常

1、异常的概述

  异常定义: 在程序中,发生“不正常”的事件,导致程序无法正常运行,并使JVM中断,称为异常
  生活中的异常: 早上起床上课,平时骑车20分钟可以到达教室,由于天气原因或者闹钟响了自动关闭,不能按时到达教室上课,迟到了,此时就属于异常现象 。
  捕获异常: 当程序在运行时,发生了异常 ,为了让程序正常执行,需要对异常捕获(catch),称之为捕获异常
  Java是面向对象的语言, 异常本身就是一个类(Exception),当发生异常时会创建异常对象,捕获的就是该对象。

 System.out.println("请输入一个数字");
        Scanner sc = new Scanner(System.in);
        // 对可能发生的异常 进行处理
        int num = sc.nextInt();
        if(num%2==0){
            System.out.println("这个数是偶数");
        }

   异常代码可能发生异常, 当用户输入非数字时, 导致程序抛出一个异常对象 :

 at java.util.Scanner.throwFor(Scanner.java:864)

2、异常关键字以及层次关系

   try: 试一试 ,将可能发生的代码使用try包裹 ,try不能单独出现
  catch : 捕获异常, 当发生指定的异常对象时,执行catch代码

System.out.println("请输入一个数字");
        Scanner sc = new Scanner(System.in);
        // 对可能发生的异常 进行处理
        try {
            int num = sc.nextInt();  // 发生异常后,try里面的代码不再执行
            if (num % 2 == 0) {
                System.out.println("这个数是偶数");
            }
            System.out.println("结束");
        }catch(Exception ee){// 对应的异常类 来捕获对应的异常对象  ,不能确定异常类,可以使用父类Exception
            System.out.println("你的输入不正确");
        }
        System.out.println("程序继续运行直到结束。。。。");

  一个try + 多个catch

//  抛出的异常 不能被catch捕获,会发生什么?
        try {
            int[] num = {1, 2, 3};
            System.out.println(num[1]); // 没有捕获该异常对象,JVM依然终止运行
            System.out.println(10/0);
        }catch(NullPointerException ee){
            System.out.println("这是空指针异常");
        }catch(ArrayIndexOutOfBoundsException  ee){
            System.out.println("数组下标越界异常");
        }catch(Exception ee){
            // 输出异常 堆栈消息  方便程序员排错(尽可能避免用户看见)
            ee.printStackTrace();
            System.out.println("系统繁忙!"+ee.getMessage());
        }
        System.out.println("程序结束");

   finally : 异常之后的最终处理 (无法是否发生异常,程序都执行)try… finally 结构

 try{
            System.out.println("请输入两个数 ,计算两个数相除");
            Scanner sc = new Scanner(System.in);
            int  num1 =  sc.nextInt();
            int num2 = sc.nextInt();
            double  s = num1/num2; // 可能出错
            System.out.println(" try里面结束,结果:"+s);
        }finally{
            System.out.println("无论是否发生异常,都会执行这个语句块,一般用于资源回收");
        }

   try… catch…finally 结构

try {
            System.out.println("请输入两个数 ,计算两个数相除");
            Scanner sc = new Scanner(System.in);
            int num1 = sc.nextInt();
            int num2 = sc.nextInt();
            double s = num1 / num2; // 可能出错
            System.out.println(" try里面结束,结果:" + s);
        }catch(ArithmeticException ee){
            ee.printStackTrace();
            System.out.println("除数不能为0 !!");
        }catch(Exception ee){
            ee.printStackTrace();
            System.out.println("系统繁忙!!!");
        }finally {
            System.out.println("用于资源回收。");
        }

3、捕获异常

  try…catch…finally

4、抛出异常

/**
     * 根据下标访问数组元素
     * @param array
     * @param index
     * @return
     */
    public static int getEleByIndex(int [] array , int index){
         // 抛出异常: 可以在异常发生时 或发生之前 创建一个异常对象并抛出
        //  手动抛出一个异常  throw new 异常类([异常消息]);
        if(index <0 || index > array.length-1){
            //抛出异常
            throw new ArrayIndexOutOfBoundsException("你的下标越界了");
        }
        int n =  array[index];
        return n;
    }

    public static void main(String[] args) {
          //数组
        int  [] array = {2,1,4,5};
        int index=4;
        // 定义方法访问下标的元素  此时会产生异常 并抛出给方法的调用者
        try {
            int num = getEleByIndex(array, index);
            System.out.println("访问的元素:" + num);
        }catch(ArrayIndexOutOfBoundsException ee){
            System.out.println(ee.getMessage());
        }
        System.out.println("结束。。。");
    }

5、异常分类

  由于有些异常是不能直接抛出的 ,需要先声明才可以抛出,异常可以分为两大类:
   1、 编译期异常(check 异常或者检查异常):在编译期间检查异常,如果没有处理异常,则编译出错。

//创建一个文件类的对象
        File  file = new File("d:/aaa.txt");
         // 在写代码(编译之前)时 一定要处理的异常(try..catch 或者 throws),就是编译时异常 
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

  这里的IOException 就是 编译期异常,需要手动处理的
  2、运行期异常(runtime 异常或者运行异常):在运行期间检查异常, 编译期可以不处理异常。

// 在运行期间抛出异常  不需要事先处理的  NullPointException是运行异常
        String str=null;
        System.out.println(str.length());

在这里插入图片描述
Exception中常用的异常类

  • ​ RuntimeException
    • ArrayIndexOutOfBoundsException :数组下标越界异常
    • NullPointerException:空指针异常
    • ArithmeticException: 算术异常
    • NumberFormatException :数字格式化异常
    • ClassNotFoundException: 类没找到异常
    • ClassCaseException: 类转换异常

6、自定义异常

1、为什么需要使用自定义异常
   在Java中每一个异常类都表示特定的异常类型, 例如 NullPointerException表示空指针 ,ArithmeticException表示算术异常, 但是sun公司提供的API中不可能将实际项目中的业务问题全部定义为已知的异常类 ,这是需要程序员根据业务需求来定制异常类,例如 用户注册,可以定义用户注册异常(RegisterException),分数不能为负数也可以定制异常(ScoreExcecption)。
2、什么是自定义异常
  在开发中根据自己的业务情况来定义异常类 , 灵活性较高,且方便易用。
3、如何实现自定义异常
  a、定义编译期异常类,创建一个类继承 java.lang.Exception ;
  b、定义运行期异常类,创建一个类继承java.lang.RuntimeException;
4、案例分析:自定义异常应用
  要求: 模拟用户注册操作, 用户输入用户名 ,验证用户名是否存在,如果存在,则抛出一个异常消息 “亲,该用户已存在,不能注册” ,通过自定义异常提示消息

public class RegisterException  extends  Exception {
    public RegisterException(){
    
    }
    public RegisterException(String message){
        // 将message 赋值给父类的构造
        super(message); //  将message赋值给父类的 属性,可通过getMessage()方法
        }
     }
public class TestRegister {
     // 模拟已存在的用户
    String []  users = {"张三","王麻子","王小花"};
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要注册的用户:");
        String uname = sc.next();
        TestRegister obj = new TestRegister();
        try {
            // 调用方法
            obj.checkUserName(uname);
            System.out.println("注册成功");
        } catch (RegisterException e) {
            System.out.println("注册失败");
            System.out.println(e.getMessage());
        }
    }
     /**
     * 检查用户是否存在
     * @param username
     * @return   true  表示通过
     *    异常表示不通过
     */
    public boolean  checkUserName(String username) throws RegisterException{
         // 使用foreach遍历
        /**
         *   for(数据类型 变量名  : 数组名/集合名 ){
         *        循环中的 变量名代表的就是数组的元素
         *   }
         */
        for(String  u : users){
            // 判断u是否与 username相等 ,相等说明用户存在,需要抛出异常
            if(u.equals(username)){
                throw new RegisterException("亲,"+username+" 已存在,不能注册");
            }
        }
        return true;
     }
}

三、包的结构与功能分析

   Java是一门面向对象的语言,sun公司提供基于面向对象的帮助文档(API Application Program Interface) ,并针对不同的版本生成的API
  API中根据不同的功能分如下包 (package):

  • java.applet.* : java的小应用程序
  • java.awt.* 和 java.swing.* : java的图形用户界面(开发单机版的小游戏)
  • java.lang.* : java的语言包
  • java.util.* : java的工具类包、集合框架包
  • java.io.* : java文件读写包(Input 、Output)
  • java.net.* : java的网络编程包(Socket机制相关,URL)
  • java.sql./ javax.sql. : java的数据库操作
  • java.lang.reflect.* 反射相关包

四、java的lang包

1、包装包

  定义: Java的8个基本数据类型对应的 对象类型,称为它们的包装类
  为什么需要包装类:
  基本数据类型中不能提供方法, 不能与其他引用数据类型转换,此时包装类作为该基本数据类型的对象类型,不仅提供常用的方法,还可以与其他数据类型互相转换 和 “装箱”、“拆箱”

基本数据类型包装类型包装类的默认值
byteBytenull
shortShortnull
intIntegernull
longLongnull
floatFloatnull
doubleDoublenull
charCharacter
booleanBoolean

   问题1: 基本数据类型、包装类以及字符串的相互转换
在这里插入图片描述

public static void main(String[] args) {
        // 1、byte 的包装类   Byte
        // 创建包装类的对象
        byte b=123;
        Byte obj1 = new Byte(b);
        //1、 包装类 转字符串   包装类对象.toString()
        String s1 = obj1.toString();
        //2、字符串转包装类      new 包装类(s) 或者   包装类.valueOf(s)
        String s2="100";
        Byte obj2 = new Byte(s2);
        // 或者
        Byte obj3 = Byte.valueOf(s2);
        //3  获取包装类的数值,包装类转基本数据类型  Byte  - >  byte
        //  包装类.valueOf(基本数据类型) 或者 byteValue()
        byte b2 = obj2;  // 包装类可以直接复制给基本数据类型  ,这个过程 “拆箱”过程
        byte b3 = Byte.valueOf(obj2);
        // 4、字符串转 基本类型     包装类.paseByte(s)
        byte b4 = Byte.parseByte(s2);
        byte b5=122;
        String s5 = new Byte(b5).toString();
    }

  再以 Integer 举例

public static void main(String[] args) {
        int n=250;
        // 转包装类
        Integer obj1 = new Integer(n);
        //包装类转基本数据类型
        int n3 = Integer.valueOf(obj1)
        // 转字符串
        String s1 = obj1.toString();
        //  字符串再转成  Integer
        Integer obj2 = Integer.parseInt(s1);
        Integer obj3 = new Integer(s1);
        // 字符串转int
        int n2 = Integer.valueOf(s1);
        // int  转 转字符串
        String s3 = new Integer(n2).toString();
        System.out.println("-------------Intger的常用方法------");
        int num = Integer.bitCount(2);  // 个位数 + 高位数的和
        System.out.println(num);
        // n1>n2 返回1   n1==n2 返回0   n1<n2 -1
        //比较两个数是否相等
        System.out.println(Integer.compare(100,200));
        System.out.println(Integer.decode("123"));
        //equals  比较两个数是否相等  对于基本数据类型的包装类比较他们的数值
        Integer  n1  = new Integer(90);
        Integer n4 = new  Integer(90);
        System.out.println(n1.equals(n4));// 比较两个对象的 值
        System.out.println(n1 == n4);// 比较 两个对象的地址
        int n5 =100;
        int n6 =100;
        System.out.println(n5==n6);// 对于基本数据类型 == 比较的值
        // 进制转换
        System.out.println(Integer.toBinaryString(18));//转成二进制表示形式
        System.out.println(Integer.toHexString(15));//转成16进制表示形式
        System.out.println(Integer.toOctalString(10));//转成8进制表示
    }

  问题2: 数据类型的装箱和拆箱
  装箱: 将基本数据类型自动转换成 它的包装类,可以使用包装类的方法和属性

// 自动装箱: 100自动转成 Integer 
        Integer num1 = 100;

  拆箱: 将包装类型 自动转成 对应的基本数据类型。

// 自动拆箱:  Integer 自动转成  int
        int num2 = num1;

  面试题:

public static void main(String[] args) {
        // 包装类
        // 自动装箱: 100自动转成 Integer
        Integer num1 = 100;
        // 自动拆箱:  Integer 自动转成  int
        int num2 = num1;
        Integer n1 =100;
        Integer n2 =100;
        System.out.println(n1.equals(n2)); //true
        System.out.println(n1 == n2);// 应该true   他们同时指向常量池100
        Integer n3 = 150; // 等价于 Integer n3 = new Integer(150);
        Integer n4 = 150; // 等价于 Integer n4 = new Integer(150);
        System.out.println(n3.equals(n4));//true
        System.out.println(n3 == n4);//false
        Integer n6 = new Integer(100);// 一定会创建新对象
        System.out.println(n6 == n1); // false
    }
        //结论
  //对于    -128 <=Integer的值 <=127 之间(byte范围),
        // 装箱时不会创建新对象 而是直接引用 常量池中的值
        // 如果超出byte 的返回,则自动创建新对象,各自指向新对象的内存

2、Object类

  Object类是lang包提供的 ,对于lang包的类不需要import,所以 Object类无处不在,你不需要自己创建
  常用方法:
  a、getClass: 返回该对象的类型 任何类都有它的类型
  b、equals : Java中所有的equals 方式都是重写Object的方法
  原生的equals 比较的是 对象的地址 ,我们通常说的 equals比较两个对象的值是因为几乎所有的数据类型(包装类,String)都重写了equals 方法的

 public boolean equals(Object obj) {
        return (this == obj);
    }

  c、 hashCode() : 返回该都对象的hash值

// Object中的hashCode 本身没有实现 ,
        /**
         * 1、对于基本数据类型的包装类 其值就是其本身
         * 2、对于String类型的HashCode ,也是String自己实现的,其算法目的尽可能减少hash冲突
         * 3、对于自定义类,需要你自己重写HashCode ,如果不重写 就在程序运行期间 JVM根据内存地址
         *    类自动分配。(原则: 根据每个有意义的属性值,计算各自的hashCode 相加等一系列操作得到)
         */

   d:finalize() 资源回收调用该方法, 当对象地址不在被引用时,会被GC回收 并调用该方法
  Object obj = null ;
  toString() : 返回该对象的字符串表现形式 (通常会被子类重写)
  wait():线程等待
  notify():唤醒其中一个等待的线程
  notifyAll:唤醒所有等待中的线程

对象的比较

public class Student {
    private  int id; //学生编号
    private String sname;
    private Integer age;
    public void showInfo(){
        System.out.println( sname +"---"+ age);
    }
    public Student(){
    
    }
    public Student(int id ,String sname ,int age){
        this.id = id;
        this.sname = sname;
        this.age = age;
    }
    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }
        // 判断类型 是否一致
        if(obj  instanceof  Student){
            // 强转
            Student stu = (Student)obj;
            // 开始比较 id 和 sname
            if(this.id == stu.id &&  this.sname.equals(stu.sname)){
                    return true;
            }
        }
        return false;
    }
    @Override
    public int hashCode() {
        return id;
    }
}
public static void main(String[] args) {
          // 创建对象   比较对象是否相等
        // 比较内存相等 或 比较值(对象的属性)相等
        Student stu1 = new Student(1001,"敖园",22);
        Student stu2 = new Student(1001,"敖园",22);
        System.out.println(stu1==stu2);  // 比较两个对象的地址 (不相等)    false
        System.out.println(stu1.equals(stu2));   // true
        // 由于equals本身没办法解决
        //    两个对象因id 和name相等业务上是同一个对象的问题
        // 所以需要重写 equals 和 hashcode 。
         // 为什么要重写HashCode呢?
        //  回答: 在JMV中如果HashCode不相等,一定不能认为是同一个对象
        Student stu3 = stu1;  // stu3 的地址于stu1的地址是同一个
    }

3、System类

public static void main(String[] args) {
        // System 属于系统类
       //  System.out; // 获取控制台的打印流
        // 设置JVM运行时 系统参数
        System.setProperty("encoding","UTF-8");
        System.out.println("获取:"+System.getProperty("encoding"));
        // 时间从 1970-01-01
        System.out.println("获取当前系统的时间毫秒数:"+ System.currentTimeMillis());
        System.exit(0); // 0 : 表示JVM正常退出    -1 表示非正常退出
    }

4、字符串类

  java.lang.String类,Java中所有的字符串都会创建该类的实例 , 它可以对字符串查找,检索,转变大小写,截取等一系列操作,并提供了大量的字符串操作方法。

String类的特点:

  它是一个不可变字符串 ,它的值创建后不能被改变。

String的构造器:

// 创建字符串对象
        String s1="abc";
        String s2 = new String("abc");
        //通过字符数组构建
        char [] chars = {'a','b','c'};
        String s3 = new String(chars);  //  或指定长度构建字符串
        String s4 = new String(chars,0,2);
        //或根据字节数组构建
        byte [] byts = {97,98,99};
        String s5 = new String(byts);
        
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        System.out.println(s5);
// 字符串是一个不可变的对象
        // class类被JVM装载时,会分配一个存放字符串的常量池(String Pool)
        // 在类加载时 先检查常量池中是否有“abc”常量,如果有,直接将ss1指向该常量
        // 如果没有,则创建常量abc
        // 创建2个对象
        String ss1 = "abc";
        //  abc常量不能改变,  则再创建 abcd的常量,由ss1重新指向
        ss1+="d";
// 创建3个对象
        String ss2 ="abcd";  // abcd
        String ss3 = "aaa";  // aaa
        ss2 += ss3;  // abcdaaa   重新创建abcdaaa并由ss2重新指向
 String a1="abc";
        String b1="abc"; // 两个地址同时指向一个常量 “abc”
        System.out.println(a1==b1);  // true
        System.out.println(a1.equals(b1));
       
        String c1=new String("abc");// 堆内存中  对abc包装后的地址
        System.out.println(a1==c1);  // false
        System.out.println(a1.equals(c1));//true

在这里插入图片描述

字符串类常用方法

  将此字符串与指定对象进行比较:public boolean equals (Object anObject)
  将此字符串与指定对象进行比较,忽略大小写:public boolean equalsIgnoreCase (String anotherString)
举例:

public static void main(String[] args) {
  String s1 = "hello";
  String s2 = "hello";
  String s3 = "HELLO";
  //boolean equese(Object obj):比较字符串的内容是否相同
  System.out.println(s1.equals(s2));
  System.out.println(s1.equals(s3));
  System.out.println("-------------");
  //boolean equalsIgnoreCose(String str):比较字符串的内容是否相同,忽略大小写
  System.out.println(s1.equalsIgnoreCase(s2));
  System.out.println(s1.equalsIgnoreCase(s3));
  System.out.println("--------------");
 }

4.1、获取功能的方法

  • 返回字符串的长度:public int length()
  • 将指定的字符串连接到该字符串的末尾:public String concat (String str)
  • 返回指定索引处的char值:public char charAt (int index)
  • 返回指定字符串第一次出现在该字符串内的索引:public int indexOf(String str)
  • 返回一个子字符串,从beginIndex开始截取字符串到字符串结尾:public String substring (int beginIndex)
  • 返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndexpublic String substring (int beginIndex,int endIndex)
    举例:
public static void main(String[] args) {
  String s = "helloworld";
  //length() :获取字符串的长度,其实也就是字符的个数
  System.out.println(s.length());
  System.out.println("---------");
  //String concat (String str):将指定的字符串连接到该字符串的末尾
        String s2 = s.concat("**hellow itheima");
        System.out.println(s2);
        //charAt(int index):获取指定索引处的字符串
        System.out.println(s.charAt(0));
        System.out.println(s.charAt(1));
        System.out.println("-------");
        //int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回-1
        System.out.println(s.indexOf("l"));
        System.out.println(s.indexOf("owo"));
        System.out.println(s.indexOf("ak"));
        System.out.println("---------");
        //String sbustring(int start):截取从start开始,到字符串结尾的字符串
        System.out.println(s.substring(0));
        System.out.println(s.substring(5));
        System.out.println("----------");
        //String substring(int start,int end):从start到end截取字符串,含start,不含end
        System.out.println(s.substring(0,s.length()));
        System.out.println(s.substring(3,8));
 }

4.2、转换功能的方法

  • 将字符串转换为新的字符数组:public char[] toCharArray()
  • 使用平台的默认字符集将该String编码转换为新的字节数组:public byte[] getBytes()
  • 将与targer匹配的字符串使用replacement字符串替换public String rep;ace (CharSequence targer,CharSequence replacement)
    举例:
public static void main(String[] args) {
  String s = "helloworld";
        //char[] toCharArray():把字符串转换为字符数组
        char[] chs = s.toCharArray();
        for(int x = 0 ; x < chs.length;x++){
         System.out.println(chs[x]);
        }
        System.out.println("---------");
        //byte[] getBytes():把字符串转换为字节数组
        byte[] bytes = s.getBytes();
        for(int x = 0;x < bytes.length; x++){
         System.out.println(bytes[x]);
        }
        System.out.println("--------");
        String str = "softeem";
        String replace = str.replace("s","S");
        System.out.println(replace);
 }

4.3、分割功能的方法
  将字符串按照给定的regex(规则)拆分为字符串数组:public String[] split(String regex)
举例:

public static void main(String[] args) {
  String s = "aa|bb|cc";
  String[] strArray = s.split("|");
  for(int x = 0;x < strArray.length; x++){
   System.out.println(strArray[x]);
  }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值