JAVAday13-14笔记

  • StringBuffer

    • java.lang
    • final
    • 线程安全的可变字符序列
    • 类似于String的字符缓冲区 但不能修改(ex “+”号串联)但可以用特定的方法改变
    • String是一个不可变的字符序列
    • StringBuilder:不执行同步的StringBuffer 速度更快(即线程不安全)
    • 构造
      • public StringBuffer(): 无参构造方法 初始容量为16字符 如果超过 就自动增大
      • public StringBuffer(String str): 构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容
      • public StringBuffer(int capacity): 构造一个不带字符,但具有制定初始容量的字符串缓冲区
      • public StringBuffer(CharSequense seq): 构造一个字符串缓冲区,它包含于指定的CharSequense相同的字符 //构造里面可以放String StringBuffer StringBuilder(父类引用指向子类对象)
      • sb.length() //容器中的字符个数 ,实际值
      • sb.capacity()//容器的初始容量 ,理论值
    • 添加功能
      • public StringBuffer append(String str): 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
      • StringBuffer是字符串缓冲区,当new时是在堆内存创建了一个对象,底层是一个长度为16的字符数组,当调用添加的方法时,不会再重新创建对象,在不断向原缓冲区添加字符
      • StringBuffer类中重写了tostring方法
      • public StringBuffer insert(int offset,String str): 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
      • 在指定位置添加元素,如果没有指定位置的索引就会报越界异常
    • 删除
      • public StringBuffer deleteCharAt(int index): 删除指定位置的字符,并返回本身
      • 当缓冲区中这个索引上没有元素就会报StringIndexOutOfBoundsException
      • publc StringBuffer delete(int start,int end): 删除从指定位置开始指定位置结束的内容,并返回本身 //包含头不包含尾、
      • 清空缓冲区sb.delete(0,sb.length());
    • 替代和反转
      • public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
      • public StringBuffer reverse(): 字符串反转
    • 截取
      • public String substring(int start): 从指定位置截取到末尾,用String接受返回值
      • puclic String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
    • StringBuffer和String的相互转换

      • String - StringBuffer
        • 通过构造方法
        • StringBuffer sb1 = new StringBuffer(“abc”);
        • 通过append
      StringBuffer sb2 = new StringBuffer();
               sb2.append(“abc)
      • StringBuffer - String

        • 通过构造

              StringBuffer sb = new StringBuffer(“abc”);
              String s1 = new String(sb);
          • toString()
          • sb.toString 常用
          • 通过substring()
          • substring(0,sb.length());
    • StringBuffer与String的操作重合 但StringBuffer可以用特定函数修改不会产生垃圾
  • StringBuilder
    • 与StringBuffer的内容一样
    • StringBuffer是线程安全的,效率低
    • StringBuilder是线程不安全的,效率高
  • 将String和StringBuffer作为参数传递
    • 基本数据类型的值传递不改变其值
    • 引用数据类型的值传递,改变其值
    • String类虽然是引用数据类型,但是当他当作参数传递时和基本数据类型时一样的
  • 高级冒泡排序
    • 冒泡排序:轻的上浮,沉的下降
    • 两个相邻位置比较,如果前面的元素比后面的元素大就换位置
    public static void main(String[] args) {
        int[] arr = {5,3,2,5,4};
        bubbleSort(arr);
        print(arr);
    }

    public static void bubbleSort(int[] arr) {
        for(int i = 0;i < arr.length - 1; i++){
            for( int j = 0; j < arr.length - 1 - i; j++){
                if( arr [j] > arr[j+1] ){
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    public static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);

        }
    }
  • 高级选择排序
    • 用一个索引位置上的元素,依次与其他索引位置上的元素比较,小的在前面,大的在后面
  public static void main(String[] args){
        int[] arr = {3,6,1,2,9};
        selectSort(arr);
        print(arr);

    }

    public static void selectSort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            for(int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    public static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
  • 二分查找(半查找)
    • 数组有序
    • 查找元素对应的索引
    public static int getIndex(int[] arr,int value){
        int min = 0;
        int max = arr.length - 1;
        int mid;
        while(min <= max){
            mid = (max + min)/2;
            if(arr[mid] == value){
                return mid;
            }
            else if (arr[mid] > value) {
                max = mid - 1;
            }
            else {
                min = mid + 1;
            }
        }
        return -1;
    }


////////////////////////////////////////


    public static int getIndex(int[] arr,int value){
        int min = 0;
        int max = arr.length - 1;
        int mid = (max + min)/2;
        while(arr[mid] != value){
            mid = (max + min)/2;
            if(arr[mid] > value) max = mid -1;
            else if(arr[mid] < value) min = mid + 1;

            if(min > max) return -1;
        }
        return mid;
    }
  • Arrays类的概述和方法使用
    • java.util
    • static
    • public static String toString(int[] a)
    • public static void sort(int[] a)
    • public static int binarySearch(int[] a,int key)
      • 使用二分查找法来搜索指定的value,使用前需要排序,如果有多个指定值,返回的值不确定
      • 如果找的值不包含在数组中,则返回(-插入点-1)
  • 基本类型包装类的概述
    • 变成对象以后 可以直接调用方法操作
    • 基本类型与包装类的对应

    • byte Byte
      short Short
      int Integer
      long Long
      float Float
      double Double
      char Character
      boolean Boolean
    • 常用操作:用于基本数据类型与字符串之间的转换
  • Integer
    • 构造方法
    • public Integer(int value);
    • publid Integer(String s);
      • Integer i = new Integer(“abc”); //NumberFormatException
  • String和Int型的转换
    • int-String
      • a:和”“进行拼接
      • b:public static String valueOf(int i)
      • c:int – Integer – String(Integer类的toString方法())
      • d:public static String toString(int i)(Integer类的静态方法)
    • String-int
      • String – Integer – int
        • intValue();
      • public static int parseInt(String s)
        • Integer.parseInt();
        • 基本数据类型包装类有八种,其中七中都有parseXxx(),可以将这七种的字符串表现形式转换成基本数据类型
        • char的包装类Character中没有 字符串到字符的转换通过toCharArray();
    • 自动装箱自动拆箱(JDK5新特性)
      • 自动装箱:把基本类型转换成包装类类型
        Integer i2 = 100;
      • 自动拆箱:把包装类类型转换位基本类型
        int z = i2 + 200;
        Integer i1 = new Integer(97);
        Integer i2 = new Integer(97);
        System.out.println(i1 == i2);
        System.out.println(i1.equals(i2));
        System.out.println("-----------");

//false
//true
//equals重写
        Integer i3 = new Integer(197);
        Integer i4 = new Integer(197);
        System.out.println(i3 == i4);
        System.out.println(i3.equals(i4));
        System.out.println("-----------");

//false
//true

        Integer i5 = 97;
        Integer i6 = 97;
        System.out.println(i5 == i6);
        System.out.println(i5.equals(i6));
        System.out.println("-----------");

//-128 到 127 是byte的取值范围,如果在这个取值范围内自动装箱,就不会新创建对象,而是从常量池中获取

//true
//true

        Integer i7 = 197;
        Integer i8 = 197;
        System.out.println(i7 == i8);
        System.out.println(i7.equals(i8));

//false
//true
  • 正则表达式 (regex)
    • 用来描述或匹配一系列符合某个语法规则的字符串的单个字符串
    • ex:注册邮箱,邮箱有用户名和密码,怎样限制长度
    • String regex = "[1-9]\\d{4-14}";
    • matches();
    • [] 代表单个字符
    • 字符类
      • [abc] 简单类 a/b/c
      • [^abc] 任何字符 除了a/b/c
      • [a-zA-Z] a-z/A-Z 两头的字母包括在内
      • [a-d[m-p]] a-d/m-p
      • [a-z&&[def]] d/e/f(交集)
      • [a-z&&[^bc]] a-z 除了b-c // [ad-z]
      • [a-z&&[^m-p]] a-z除了m-p
    • 预定义字符类
      • . 任何字符 一个点一个字符
      • \d 数字 [0-9] // \代表转义字符,如果想表示\d的话 要用\d
      • \D 非数字[^0-9]
      • \s 空白字符[ \t\n\x0B\f\r] // 空格、回车符(\r)、换行符(\n)、水平制表符(\t)、垂直制表符(\v)、换页符(\f)
      • \S 非空白字符 [^\s]
      • \w 单词字符[a-zA-Z_0-9]
      • \W 非单词字符
    • 数量词 Greedy
      • X? X一次或一次也没有
      • X* X零次或多次(包括一次)
      • X+ X一次或多次
      • X{n} X恰好n次
      • X{n,} X至少n次
      • X{n,m} X至少n次,不超过m次
    • 分割功能
      • split(); return String
      • split中”.”代表任意字符 如果要以”.”来分割 用 \.
    • 替换功能
      • String类的功能:
      • public String replaceAll(String regex,String replacement)
    • 分组功能
    • 正则表达式的分组功能
    • 捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 ((A)(B(C))) 中,存在四个这样的组:
  • 1     ((A)(B(C))) 
    2     (A 
    3     (B(C)) 
    4     (C) 
    
    组零始终代表整个表达式。
    `$1` 第一组中的内容
    
  • Pattern/Matcher

    Pattern p = Pattern.compile("a*b");   //获取到了正则表达式
    Matcher m = p.matcher("aaaaab");    //获取匹配器
    boolean b = m.matches();   //看是否能匹配,匹配就返回true


    "aaaaab".matches("a*b");
  • ex: 获取一个字符串中的手机号码
String s = "我的手机号是18988888888,曾经用过18999999999";

String regex = "1[3578]\\d{9}";    //手机号码的正则表达式  []内仅是举例

Pattern P = Pattern.comlile(regex);
Matcher m = p.matcher(s);
/*boolean b1 = m.find();   //在字符串中寻找匹配的
String s1 = m.group();*/   //获取以前匹配操作所匹配的输入子序列

while(m.find()){
    System.out.println(m.group());
}
  • Math类
    • public static int abs(int a) //取绝对值
    • public static double ceil(double a) //ceil天花板 向上取整,结果是一个double
    • public static double floor(double a) //floor地板 向下取整,结果是一个double
    • public static int max(int a,int b) min自学 //获取两个值中的最大值
    • public static double pow(double a,double b) //求a的b次方
    • public static double random() //生成0.0-1.0之间的任意数 左开右闭
    • public static int round(float a) 参数为double的自学 //四舍五入
    • public static double sqrt(double a) //开平方
  • random
    • java.util
    • 用于生成伪随机数
    • 种子指定时每轮生成的随机值相同
    • 自动以nanoTime()作为种子
    • nextInt(int x) 在0到x-1之间的随机数
  • System类
    • 不能被实例化
    • gc(); 运行垃圾回收器(finalize())
    • exit(); 终止当前正在运行的java虚拟机 通常,非0的状态码表示异常终止 建议括号内给0
    • currentTimeMillis(); 返回以毫秒为单位的当前时间
      * 计算程序运行时间
        * long start = System.currentTimeMillis();
        * 运行内容
        * long end  = System.currentTimeMillis();
        * 运行时间 =  start - end;
  • arraycopy(Object src, int srcPos, Object dest, int destPos, int length); //在dest中destPos位置开始拷贝src中srcPos开始的length个数据 应用于集合的变化
  • BigInteger();
    • 可以让超过Integer范围内的数据进行运算
    • BigInteger(String val);
    • 成员方法
      • public BigInteger add(BigInteger val) //+
      • public BigInteger subtract(BigInteger val) //-
      • public BigInteger multiply(BigInteger val) //*
      • public BigInteger divide(BigInteger val) ///
      • public BigInteger[] divideAndRemainder(BigInteger val) //返回商and余两个数据的数组
  • BigDecimal
    • 存储更精确的小数
    • BigDecimal(String val); //开发时通过构造中传入字符串的方式
    • BigDecimal.valueOf(); //开发时也可用这种
    • 成员方法
      • public BigDecimal add(BigDecimal augend)
      • public BigDecimal subtract(BigDecimal subtrahend)
      • public BigDecimal multiply(BigDecimal multiplicand)
      • public BigDecimal divide(BigDecimal divisor)
  • Date类
    • java.util
    • 表示特定的瞬间,精确到毫秒
    • Date(0) 1970.1.1
    • 无参数代表当前时间
    • getTime(); 获取当前毫秒值
    • setTime(); 设置毫秒值,改变时间对象
  • SimpleDateFormat()类
    • DateFormat(); 父类 抽象类不能被实例化 使用其子类SimpleDateFormat
    • 构造方法
      • public SimpleDateFormat()
      • public SimpleDateFormat(String pattern)
    • 成员方法
      • public final String format(Date date)
      • public Date parse(String source) //将时间字符串转换成日期对象
  • Calendar类
    • 替代了多数Date类
    • Calendar类的概述
      • Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
    • 成员方法
      • public static Calendar getInstance() //父类引用指向子类对象
      • public int get(int field)
    • mouth是从0开始计算的
    • 成员方法
      • public void add(int field,int amount) //增加
      • public final void set(int year,int month,int date) //设置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值