JavaSE之包装类与字符串

2019-03-13  本文已影响0人  伍陆柒_

Object

public class Citizen {
    int id;
    String name;
    @Override
    public boolean equals(Object obj) {
        Citizen c = (Citizen)obj;
        if(this.id == c.id) {
            return true;
        }else {
            return false;
        }
    }
}
public class Demo1 {
    public static void main(String[] args) {
        /*
         * Object类:java中所有类的父类,唯一一个没有父类的类
         * public boolean equals(Object obj):比较两个引用数据类型是否相等,但子类可以重写,指定自己的比较规则
         *  == : 可以比较基本数据或引用数据类型,比较基本数据类型是,比较的是字面真值,
         *       比较引用数据类型时,比较的是地址是否相等
         */
//      Demo1 d1 = new Demo1();
//      Demo1 d2 = new Demo1();
//      System.out.println(d1 == d2);
//      System.out.println(d1.equals(d2));
//      
//      Citizen c1 = new Citizen();
//      c1.id = 9527;
//      c1.name = "唐伯虎";
//      Citizen c2 = new Citizen();
//      c2.id = 9527;
//      c2.name = "华安";
//      System.out.println(c1.equals(c2));
        
//      String str1 = "hello";和String str2 = new String("hello");有什么区别
//      String str1 = "hello"只创建了一个hello对象
//      String str2 = new String("hello")堆里一个常量池一个
//      String str1 = "hello";
//      String str2 = new String("hello");
//      System.out.println(str1 == str2);
        // String类重写过equals,只比较字符串内容,不比较地址
//      System.out.println(str1.equals(str2));
        
//      String str1 = "hell"+new String("o");
//      String str2 = "hello";
//      System.out.println(str1 == str2);
//      System.out.println(str1.equals(str2));
    }

}

包装类

public class Demo4 {
    public static void main(String[] args) {
        // 包装类 : 将基本数据类型包装成对象
        // Everything is object
        // java中不把基本数据类型视为对象
        // 1.向基本数据一样去使用
//      int num1 = 100;
//      // 自动装箱
//      Integer num2 = 100;// Integer num2 = new Integer(100);
        // 自动拆箱
//      System.out.println(num1+num2);
//      Double d = 9.9;// Double num2 = new Double(9.9);
        
        // 2.与字符串之间进行类型转换
        String strNum1 = "1000";
//      String strNum2 = "2000";
//      System.out.println(strNum1+strNum2);
        // 字符串转成其他基本数据类型
        int num1 = Integer.parseInt(strNum1);
//      int num1 = Integer.valueOf(strNum1);
        double num2 = Double.parseDouble(strNum1);
//      double num2 = Double.valueOf(strNum2);
//      System.out.println(num2+1);
        
        //基本数据类型 转成其他字符串
//      double num1 = 100;
//      String strNum1 = num1+"";
//      String strNum2 = String.valueOf(num1);
//      String strNum3 = Double.toString(num1);
    }

}

String类的常用方法

public class StringDemo1 {

    public static void main(String[] args) {
        /*
         * 字符串类
         *  String : 代表一组“不可变”的字符串,字符串常量,对它的所有修改都是再创建新的字符串,不是在原有基础上修改的
         *  StringBuffer
         *  StringBuilder
         */
//      String str = "hello";
//      str = str + "world";
//      System.out.println(str);
        
        // 1.equals比较字符串内容是否相等
        System.out.println("hello".equals("Hello"));
        System.out.println("hello".equalsIgnoreCase("Hello"));
        
        // *2.String toUpperCase() 转成大写 
        //    String toLowerCase() 转成小写
//      String str = "hello";
//      str.toUpperCase();
//      System.out.println(str);
//      System.out.println("hello".toUpperCase());
//      System.out.println("HELLO".toLowerCase());
        
        
        // 3.char charAt(int) 返回指定索引处的字符
        System.out.println("hello".charAt(1));// e
        
        // 4.String substring(int begin)  截取字符串,
        //   String substring(int begin,int end) 从begin截取到end-1结束
        String str = "hello";
        System.out.println(str.substring(1));// ello
        System.out.println(str.substring(1, 3));// el

        // 5.int indexOf(xxx) 返回一个字符串在另一字符串当中第一次出现的索引,若一次都没有出现,返回-1
        //   int lastIndexOf() 返回一个字符串在另一字符串当中最后一次出现的索引,若一次都没有出现,返回-1
        System.out.println("abefcdefg".indexOf("efz"));
        System.out.println("abefcdefg".lastIndexOf("ef"));
    }
}

练习1:

public class Practice2 {
    public static void main(String[] args) {
        // 在一个字符串中查找e出现的次数
        String str = "adfsdeadsgeeeegsfgdfg";
        char[] c = str.toCharArray();// str.toCharArray()返回一个char数组
        int count = 0;
        for(char ch :c) {
            if(ch == 'e') {
                count++;
            }
        }
        System.out.println(count);
    }
}

练习2:查单词

public class Practice3 {
    /**
     * 返回son在father当中出现的次数
     * @param father 父串
     * @param son   子串
     * @return 次数
     */
    public static int findWord(String father,String son) {
        // 在父串中查找一次子串出现的索引
        int index = father.indexOf(son);
        int count = 0;
        while(index != -1) {
            // 计数
            count++;
            // 截取
            father = father.substring(index+son.length());
            // 再找一次
            index = father.indexOf(son);
        }
        return count;
    }

    public static void main(String[] args) {
        // 查单词: 在一个字符串中查找另一个字符串出现的次数
        System.out.println(findWord("aaaaaaaa","aa"));
    }

}

练习3:

public class Practice4 {
    /**
     * 判断是否是合法数字
     * @param num 要判断的数字
     * @return 合法返回true,不合法返回false
     */
    public static boolean getNum(int num) {
        String str[] = {"1","2","3","4","5"};
        // 1.   num是不是包含1,2,3,4,5
        for(String s : str) {
            // 检查num中是否有"1","2","3","4","5"字符串
            int index = (num+"").indexOf(s);
            if(index == -1) {
                return false;
            }
        }
        // 2.   检查是否有两个2
        if((num+"").indexOf("2") == (num+"").lastIndexOf("2")) {
            return false;
        }
        // 3.   4不能在第三位
        if((num+"").charAt(2) == '4') {
            return false;
        }
        // 4.   3和5不能相连
        if((num+"").indexOf("35") != -1 || (num+"").indexOf("53") != -1) {
            return false;
        }
        return true;
    }   
    public static void main(String[] args) {
        // 写出1,2,2,3,4,5这六个数字能组成的所有六位数组合
        // ①4不能在第三位
        // ②3和5不能相连
        for(int i = 122345;i <= 543221;i++) {
            if(getNum(i)) {
                System.out.println(i);
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读