zoukankan      html  css  js  c++  java
  • Java学习

    1、编译 执行

    编辑 xx.java 类文件  ->  javac xx.java 编译  ->  java  xx  执行。 

    2、字符串

    //共享设计模式
    String a = "1"; String b = "1"; System.out.println(a.equals(b)); //true System.out.println(a == b); //true System.out.println(System.identityHashCode(a));//地址一致 System.out.println(System.identityHashCode(b));

    字符与字符串

    //  字符串是字符数组,而不是字节数组
    String str = "你好吗 亲爱的";
    char[] data = str.toCharArray();//字符串转字符数组
    int length = data.length;
    System.out.println(length); //7
    
    for (int i = 0; i < length; i++) {
          System.out.println(data[i]);
    }
    
    System.out.println(new String(data)); //字符数组转字符串
    System.out.println(new String(data, 1, 2)); //字符数组部分字符转字符串
            
    //判断是否为数字
    for (int i = 0; i < length; i++) {
         if(data[i] > '9' || data[i] < '0') {
    
         }
    }

    字节与字符串

    String str = "helloworld";
    //String str = "你好吗?亲爱的";
    byte[] data = str.getBytes();//字符串转字节数组
    for (byte a :
             data) {
         System.out.println(a);
    }
    
    System.out.println(new String(data)); //全部转换
    System.out.println(new String(data, 2, 4));//部分转换,假如是中文,会导致字符不全进而乱码

    比较

    String str = "helloworld";
    String str2 = "HelloWorld";
    System.out.println(str.equalsIgnoreCase(str2)); //不区分大小写
    System.out.println(str.compareTo(str2)); //比较大小
    System.out.println(str.compareToIgnoreCase(str2));//忽略大小写

     字符串查找

    String str = "helloworld";
    System.out.println(str.contains("hel"));
    System.out.println(str.indexOf("wora"));
    System.out.println(str.indexOf("wor", 6));//fromIndex:查询起始位置
    System.out.println(str.lastIndexOf("o"));//最后一个符合条件的位置
    System.out.println(str.startsWith("h")); //是否已指定的字符串开头
    System.out.println(str.startsWith("o", 4)); //从指定位置开始,是否以该字符串开头
    System.out.println(str.endsWith("d")); //是否以该字符串结尾

     字符串替换

    String str = "helloworld";
    System.out.println(str.replaceAll("o", " "));//全部替换
    System.out.println(str.replaceFirst("o", " "));//替换首个满足条件

    字符串截取

    String str = "helloworld";
    System.out.println(str.substring(1, 3)); //截取 index 1-3
    System.out.println(str.substring(1)); //从 1 开始截取 到结尾

    字符串拆分

    String str = "hell-o-wo-r-ld";
    String[] strs = str.split("-"); //全部拆分
    for (String string :
             strs) {
         System.out.println(string);
    }
    
    String[] strs2 = str.split("-", 3); //limit 最大拆分个数(拆分后最大数组长度),从前往后["hell", "o", "wo-r-ld"]
        for (String string :
             strs2) {
         System.out.println(string);
    }
    
    
    String IPv4 = "192.168.1.101";
    String[] ips = IPv4.split("\."); //由于拆分原理为正则,"."拆分会导致失败,需要改为"\."
    for (String string :
            ips) {
       System.out.println(string);
    }

    空判断

    String strb = "1";
    //System.out.println(strb.isEmpty()); //不可判断null
    if(strb == null || strb.length() <= 0) {
            System.out.println("空值");
    } else {
            System.out.println("".equals(strb)); //可判断null
    }

    3、对象 

    对象比较

    public class HelloWorld {
    
        public String title;
        double price;
        public boolean bijiao(HelloWorld helloWorld) {
            if(helloWorld == null) {
                return false;
            }
            if(this == helloWorld) {
                return true;
            }
            if(this.title.equals(helloWorld.title) && this.price == helloWorld.price) {
                return true;
            }
            return false;
        }
    }

     static 属性的一点奇葩,实例居然能调用static属性,并且static属性只要一改变,就会影响到所有该类的所有对象

     代码块

    //普通类
    class Book {
        public Book() {
            System.out.println("构造方法" + this);
        }
        //构造快
        {
            System.out.println("构造快" + this);//构造快优先构造方法执行
        }
        //静态块
        static {
            System.out.println("静态块");//静态块优先构造快执行 ,不管实例多少个,只执行一次
        }
    }
    //主类
    public class Main {
    
        static {
            System.out.println("主类中的静态块");//优先于主方法执行
        }
    
        public static void main(String[] args) {
            new Book();
        }
        //普通代码块 构造快 静态块 同步代码块
    }

     链表

    class Node {
        private String data;
        private Node next;
    
        public void setData(String data) {
            this.data = data;
        }
    
        public void setNext(Node next) {
            this.next = next;
        }
    
        public Node getNext() {
            return this.next;
        }
    
        public String getData() {
            return this.data;
        }
    
        public Node(String data) {
            this.data = data;
        }
        public void addNode(Node newNode) {
            if(this.next == null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
        public void printNode() {
            System.out.println(this.data);
            if(this.next != null) {
                this.next.printNode();
            }
        }
    }
    class Link {
        private Node root;
        public void add(String data) {
            Node newNode = new Node(data);
            if(this.root == null) {
                this.root = newNode;
            } else {
                this.root.addNode(newNode);
            }
        }
        public void print() {
            if(this.root != null) {
                this.root.printNode();
            }
        }
    }
    
    public class Main {
    
    
        public static void main(String[] args) {
    
            Link link = new Link();
            link.add("你好");
            link.add("世界");
            link.add("!");
            link.add("你好");
            link.add("世界");
            link.add("!");
            link.print();
    
    //        Node root = new Node("火车头");
    //        Node n1 = new Node("车厢1");
    //        root.setNext(n1);
    //        Node n2 = new Node("车厢2");
    //        n1.setNext(n2);
    //        Node n3 = new Node("车厢3");
    //        n2.setNext(n3);
    
    //        Node temp = root;
    //        while (temp != null) {
    //            System.out.println(temp.getData());
    //            temp = temp.getNext();
    //        }
    //        print(root);
    
        }
        //递归
    //    public static void print(Node temp) {
    //        if(temp == null) {
    //            return;
    //        }
    //        System.out.println(temp.getData());
    //        print(temp.getNext());
    //    }
    
    }

     类型判断

    String[] array = new String[4];
    System.out.println(array instanceof String[]);
    System.out.println(array.getClass().isArray());

     final

    class B {
        final double GOOD = 100.0;//不可改变(可以用final声明变量作为常量使用)
        public static final String MSG = "MSG"; //全局常量
    }

    其他

    //        String stra = "hello ";
    //        String strb = stra + "world";
    //        String strc = "hello world";
    //        System.out.println(strb == strc); //不成立
    
    //        String stra = "hello ";
    //        String strb = stra.concat("world");
    //        String strc = "hello world";
    //        System.out.println(strb == strc); //不成立
    
    //        String strb = "hello ".concat("world");
    //        String strc = "hello world";
    //        System.out.println(strb == strc); //不成立
    
            String strb = "hello " + "world";
            String strc = "hello world";
            System.out.println(strb == strc); //成立
  • 相关阅读:
    Python算法:推导、递归和规约
    K-means的缺点(优化不仅仅是最小化误差)
    从统计学角度来看深度学习(2):自动编码器和自由能
    从统计学角度来看深度学习(1):递归广义线性模型
    Why are Eight Bits Enough for Deep Neural Networks?
    VCS引起的oracle数据库异常重新启动一例
    赵雅智:service_startService生命周期
    第九章 两种模式的比較
    CSDN Markdown简明教程3-表格和公式
    OpenStack_Swift源代码分析——Object-auditor源代码分析(1)
  • 原文地址:https://www.cnblogs.com/liuguan/p/9689546.html
Copyright © 2011-2022 走看看