美文网首页
2019-03-13JavaSE工具类

2019-03-13JavaSE工具类

作者: 果冻_4c9b | 来源:发表于2019-04-20 15:21 被阅读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 num =100;
    //    自动装箱
    // Integer num2 = 100;// Integer num2 =new Integer(100);
    //    自动拆箱
    // System.out.println(num+num2);
    // Double num3= 9.9;
    //
    // 2.与字符串之间进行类型转换
    // String str = "1000";
    // String str1 = "2000";
    // System.out.println(str+str1);
    // 字符串转成其他基本数据类型
    // int num1 = Integer.parseInt(str);
    // int num1 =Integer.valueOf(str);
    // double num2 = Double.parseDouble(str1);
    // System.out.println(num1+num2);
    // 基本数据类型转成其他字符串
    int num =100;
    String Str = num+"";
    String Str2 = String.valueOf(num);
    String Str3 = Double.toString(num);
    System.out.println(Str2+"\n"+Str3);
    }
    }
    

    String 字符串类

    public class StringDemo4 {
    public static void main(String[] args) {
    // 字符串类
    /*
    * String:代表一组不可变得”字符串“,字符串常量。。。对他的所有修改的都是创建新的字符串,不是在原有的基础上修改
    * 
    * StringBuffer:
    * StringBuilder:
    *
    */
    // * 1.equals比较字符串内容是否相等
    // System.out.println("hello".equals("hello"));
    // System.out.println("hello".equalsIgnoreCase("Hello"));//忽略大小写
    //    2.String toUpperCase转成大写  
    // String toLowerCase转成小写
    // System.out.println("hello".toUpperCase());
    // System.out.println("hello".toLowerCase());
    // * 3.char charAT(int)返回指定索引处字符
    // System.out.println("hello".charAt(0));
    // 4.String substring(int begin) 截取字符串
    // String substring(int begin ,int end)  begin截取到end-1;
    // System.out.println("hello".substring(2));//llo
    // System.out.println("hello".substring(0, 3));
    // 5. int indexOf(xxx)返回一个字符串在另一个字符串当中第一次出现的索引,若一次都不出现,返回-1
    //   int lastindexOf();返回一个字符串在另一个字符串当中最后一次出现的索引若一次都不出现,返回-1
      System.out.println("sadasdsdf".indexOf("sd"));
      System.out.println("sadasdsdf".lastIndexOf("s"));
    }
    }
    public class StringDemo1 {
    public static void main(String[] args) {
    // 去空格 String  trim() 掐头去尾
    // System.out.println("  hello  world  ".trim());
    // 长度  int length()
    //          System.out.println("hello  world".length());
    
              //  byte[] getBytes
    //          byte b[] = "hello".getBytes();
    //          for(byte bb:b) {
    //          System.out.println(bb);
    //          }
    //          String str = new String(b);
    //          System.out.println(str);
    
    
          //String cincat(String concat)连接字符串
    // System.out.println("hello".concat("world"));//要比+连接效率高
    
    // char s[] = "5445656".toCharArray();
    // String[] split(String regex)  分割字符串成数组
    // String str[]="黑桃;红桃;方片".split(";");//  用 "."分割\\.
    // for(String s :str) {
    // System.out.print(s+" ");
    // }
    // String replace(char oldchar,char newChar)新字符串替换旧字符串
    // System.out.println("aafdfsdf".replace("a", "z"));
    //  boolean startWith(String prefix) // 以什么开头  返回类型为布尔型
    System.out.println("hello".startsWith("hel"));
    // boolean endsWith(String prefix)  //以什么结尾   返回类型为布尔型
    System.out.println("hello".endsWith("leo"));
    }
    }
    

    StringBuffer与StringBuilder

    public class StringDemo2 {
    public static void main(String[] args) {
    // String 代表一组字符串常量
    // StringBuffer:代表一组可变的字符串,对它的所有修改在原有基础上进行的,数据同步(线程安全)
    // StringBuilder :效率更高(使用同上)
    //   1.equals方法:StringBuffer没有重写,先将StringBuffer转换成String在equals 
    StringBuffer sbf = new StringBuffer("hello");//声明
    //        String s =sbf.toString();
    System.out.println(sbf.toString().equals("hello"));
    // 2.length跟String使用一样
    // 3.reverse()//字符串翻转
    sbf.reverse();
    System.out.println(sbf);
    // 4.setCharAt(0, 'z')  替换(索引数,字符型)
    sbf.setCharAt(0, 'z');
    System.out.println(sbf);
    // 5.StringBffer delete (int begin,int end)删除从...到...(0开始删除到end-1)
    sbf.delete(0, 1);
    System.out.println(sbf);
    // *6.append追加
    sbf.append("aaa");
    System.out.println(sbf);
    }
    }
    
    StringBuffer sbf = new StringBuffer("13312345678");
    sbf.insert(1, "a");插入下脚标1的为a
    System.out.println(sbf);
    
    public int hashCode():
    public class Demo2 {
    public static void main(String[] args) {
    // public int hashCode():该方法返回对象的哈希码十进制
    // 如果俩个对象用equals比较返回true,俩个对象的哈希码必定一样的
    // 俩对象的哈希码一样,如果俩对象用equals比较不一定返回true
          Demo2 d = new Demo2();
          System.out.println(d);
          System.out.println(d.hashCode());
    }
    }
    

    例题

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

    例题二

    public class Practice3 {
    // indexof
    // substring
    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("asaddasdfgds","as"));
    }
    }
    

    例题三

    public class Practice4 {
    //    判断是否合法
    public static boolean getNum(int num) {
    String str[]= {"1","2","2","3","4","5"};
    //判断传进来的num 是不是包含12345
    for(String s:str) {
    int index=(num+"").indexOf(s);
    if(index ==-1) {
    return false;
    }
    }
    //检查是否有俩个2
    if((num+"").indexOf("2")==(num+"").lastIndexOf("2")) {
    return false;
    }
    //4不在第三位
    if((num+"").charAt(2)=='4') {
    return false;
    }
    //3和5不能相邻
    if((num+"").indexOf("35")!=-1||(num+"").indexOf("53")!=-1) {
    return false;
    }
    return true;
    }
    public static void main(String[] args) {
    // 写出122345这六个数字能组成六位数组合
    // 1.  4不能在第三位
    // 2.  3和5,5和3不能相连
    for(int i=122345;i<=543221;i++) {
    if(getNum(i)==true) {
    System.out.println(i);
    }
    }
    }
    }
    

    相关文章

      网友评论

          本文标题:2019-03-13JavaSE工具类

          本文链接:https://www.haomeiwen.com/subject/quqlgqtx.html