Java笔记

No_16_0307 Java基础学习第十三天

2016-03-08  本文已影响51人  lutianfei
文档版本 开发工具 测试平台 工程名字 日期 作者 备注
V1.0 2016.03.07 lutianfei none

[TOC]


StringBuffer类

StringBuffer类概述及其构造方法

public class StringBufferDemo {
    public static void main(String[] args) {
        // public StringBuffer():无参构造方法
        StringBuffer sb = new StringBuffer();
        System.out.println("sb:" + sb);
        System.out.println("sb.capacity():" + sb.capacity());
        System.out.println("sb.length():" + sb.length());
        System.out.println("--------------------------");

        // public StringBuffer(int capacity):指定容量的字符串缓冲区对象
        StringBuffer sb2 = new StringBuffer(50);
        System.out.println("sb2:" + sb2);
        System.out.println("sb2.capacity():" + sb2.capacity());
        System.out.println("sb2.length():" + sb2.length());
        System.out.println("--------------------------");

        // public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
        StringBuffer sb3 = new StringBuffer("hello");
        System.out.println("sb3:" + sb3);
        System.out.println("sb3.capacity():" + sb3.capacity());
        System.out.println("sb3.length():" + sb3.length());
    }
}

/*
运行结果:
sb:
sb.capacity():16
sb.length():0
--------------------------
sb2:
sb2.capacity():50
sb2.length():0
--------------------------
sb3:hello
sb3.capacity():21 // 16+5
sb3.length():5
*/

StringBuffer类的成员方法

/*
result:
sb:霞青林爱我
sb:我爱林青霞
*/
```

    public class StringBufferDemo {
    public static void main(String[] args) {
        // 创建字符串缓冲区对象
        StringBuffer sb = new StringBuffer();

        // 添加元素
        sb.append("hello").append("world").append("java");
        System.out.println("sb:" + sb);

        // 截取功能
        // public String substring(int start)
        String s = sb.substring(5);
        System.out.println("s:" + s);
        System.out.println("sb:" + sb);

        // public String substring(int start,int end)
        String ss = sb.substring(5, 10);
        System.out.println("ss:" + ss);
        System.out.println("sb:" + sb);
    }
}

StringBuffer类练习

public class StringBufferTest {
    public static void main(String[] args) {
        // String -- StringBuffer
        String s = "hello";
        StringBuffer sb2 = new StringBuffer();
        sb2.append(s);
        System.out.println("sb:" + sb);
        System.out.println("sb2:" + sb2);
        System.out.println("---------------");

        // StringBuffer -- String
        StringBuffer buffer = new StringBuffer("java");
        // String(StringBuffer buffer)
        // 方式1:通过构造方法
        String str = new String(buffer);
        // 方式2:通过toString()方法
        String str2 = buffer.toString();
        System.out.println("str:" + str);
        System.out.println("str2:" + str2);
    }
}
public class StringBufferTest2 {
    public static void main(String[] args) {
        // 定义一个数组
        int[] arr = { 44, 33, 55, 11, 22 };

        //用StringBuffer做拼接的方式
        String s2 = arrayToString2(arr);
        System.out.println("s2:" + s2);
    }

    // 用StringBuffer做拼接的方式
    public static String arrayToString2(int[] arr) {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int x = 0; x < arr.length; x++) {
            if (x == arr.length - 1) {
                sb.append(arr[x]);
            } else {
                sb.append(arr[x]).append(", ");
            }
        }
        sb.append("]");

        return sb.toString();
    }
}
public class StringBufferTest3 {
    public static void main(String[] args) {
        // 键盘录入数据
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数据:");
        String s = sc.nextLine();

        // 方式1:用String做拼接
        String s1 = myReverse(s);
        System.out.println("s1:" + s1);
        // 方式2:用StringBuffer的reverse()功能
        String s2 = myReverse2(s);
        System.out.println("s2:" + s2);
    }

    // 用StringBuffer的reverse()功能
    public static String myReverse2(String s) {
        // StringBuffer sb = new StringBuffer();
        // sb.append(s);

        // StringBuffer sb = new StringBuffer(s);
        // sb.reverse();
        // return sb.toString();

        // 简易版
        return new StringBuffer(s).reverse().toString();
    }

    // 用String做拼接
    public static String myReverse(String s) {
        String result = "";

        char[] chs = s.toCharArray();
        for (int x = chs.length - 1; x >= 0; x--) {
            // char ch = chs[x];
            // result += ch;
            result += chs[x];
        }

        return result;
    }
}

public class StringBufferTest4 {
    public static void main(String[] args) {
        // 创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String s = sc.nextLine();

        // 一个一个的比较
        boolean b = isSame(s);
        System.out.println("b:" + b);
        
        //用字符串缓冲区的反转功能
        boolean b2 = isSame2(s);
        System.out.println("b2:"+b2);
    }
    
    public static boolean isSame2(String s) {
        return new StringBuffer(s).reverse().toString().equals(s);
    }

    public static boolean isSame(String s) {
        boolean flag = true;

        // 把字符串转成字符数组
        char[] chs = s.toCharArray();

        for (int start = 0, end = chs.length - 1; start <= end; start++, end--) {
            if (chs[start] != chs[end]) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}

StringBuffer类面试题

public class StringBufferDemo {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        System.out.println(s1 + "---" + s2);// hello---world
        change(s1, s2);
        System.out.println(s1 + "---" + s2);// hello---world

        StringBuffer sb1 = new StringBuffer("hello");
        StringBuffer sb2 = new StringBuffer("world");
        System.out.println(sb1 + "---" + sb2);// hello---world
        change(sb1, sb2);
        System.out.println(sb1 + "---" + sb2);// hello---worldworld

    }

    public static void change(StringBuffer sb1, StringBuffer sb2) {
        sb1 = sb2;//StringBuffer类型用`=`赋值的特殊点。
        sb2.append(sb1);
    }

    public static void change(String s1, String s2) {
        s1 = s2;
        s2 = s1 + s2;
    }
}


数组高级(排序和查找)

排序

public class ArrayDemo {
    public static void main(String[] args) {
        // 定义一个数组
        int[] arr = { 24, 69, 80, 57, 13 };
        System.out.println("排序前:");
        printArray(arr);

        //由于我可能有多个数组要排序,所以我要写成方法
        bubbleSort(arr);
        System.out.println("排序后:");
        printArray(arr);
    }
    
    //冒泡排序代码
    public static void bubbleSort(int[] arr){
        for (int x = 0; x < arr.length - 1; x++) {
            for (int y = 0; y < arr.length - 1 - x; y++) {
                if (arr[y] > arr[y + 1]) {
                    int temp = arr[y];
                    arr[y] = arr[y + 1];
                    arr[y + 1] = temp;
                }
            }
        }
    }

    // 遍历功能
    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int x = 0; x < arr.length; x++) {
            if (x == arr.length - 1) {
                System.out.print(arr[x]);
            } else {
                System.out.print(arr[x] + ", ");
            }
        }
        System.out.println("]");
    }
}
public class ArrayDemo {
    public static void main(String[] args) {
        // 定义一个数组
        int[] arr = { 24, 69, 80, 57, 13 };
        System.out.println("排序前:");
        printArray(arr);

        //用方法改进
        selectSort(arr);
        System.out.println("排序后:");
        printArray(arr);

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

    // 遍历功能
    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int x = 0; x < arr.length; x++) {
            if (x == arr.length - 1) {
                System.out.print(arr[x]);
            } else {
                System.out.print(arr[x] + ", ");
            }
        }
        System.out.println("]");
    }
}
练习题:字符串排序
public class ArrayTest {
    public static void main(String[] args) {
        // 定义一个字符串
        String s = "dacgebf";

        // 把字符串转换为字符数组
        char[] chs = s.toCharArray();

        // 把字符数组进行排序
        bubbleSort(chs);

        //把排序后的字符数组转成字符串
        String result = String.valueOf(chs);
        
        //输出最后的字符串
        System.out.println("result:"+result);
    }

    // 冒泡排序
    public static void bubbleSort(char[] chs) {
        for (int x = 0; x < chs.length - 1; x++) {
            for (int y = 0; y < chs.length - 1 - x; y++) {
                if (chs[y] > chs[y + 1]) {
                    char temp = chs[y];
                    chs[y] = chs[y + 1];
                    chs[y + 1] = temp;
                }
            }
        }
    }
}

查找

基本查找: 数组元素无序
public static int getIndex(int[] arr,int value) {
    int index = -1;
    for(int x=0; x<arr.length; x++) {
        if(arr[x] == value) {
            index = x;
            break;
        }
    }
    return index;
}
二分查找(折半查找) : 数组元素有序
public class ArrayDemo {
    public static void main(String[] args) {
        //定义一个数组
        int[] arr = {11,22,33,44,55,66,77};
        
        //写功能实现
        int index = getIndex(arr, 33);
        System.out.println("index:"+index);
        
        //假如这个元素不存在后有什么现象呢?
        index = getIndex(arr, 333);
        System.out.println("index:"+index);
    }
    
    /*
     * 两个明确:
     * 返回值类型:int
     * 参数列表:int[] arr,int value
     */
    public static int getIndex(int[] arr,int value){
        //定义最大索引,最小索引
        int max = arr.length -1;
        int min = 0;
        
        //计算出中间索引
        int mid = (max +min)/2;
        
        //拿中间索引的值和要查找的值进行比较
        while(arr[mid] != value){
            if(arr[mid]>value){
                max = mid - 1;
            }else if(arr[mid]<value){
                min = mid + 1;
            }
            //加入判断
            if(min > max){
                return -1;
            }
            mid = (max +min)/2;
        }
        return mid;
    }
}

数组高级练习题


Arrays类

Arrays类概述

Arrays类常用方法

常用方法源码详细解释

基本类型包装类

基本类型包装类概述


Integer类

Integer类概述

Integer类构造方法

Integer类成员方法

JDK5的新特性

public class IntegerDemo {
    public static void main(String[] args) {
        // 定义了一个int类型的包装类类型变量i
        // Integer i = new Integer(100);
        Integer ii = 100;
        ii += 200;
        System.out.println("ii:" + ii);

        // 通过反编译后的代码
        // Integer ii = Integer.valueOf(100); //自动装箱
        // ii = Integer.valueOf(ii.intValue() + 200); //自动拆箱,再自动装箱
        // System.out.println((new StringBuilder("ii:")).append(ii).toString());

        Integer iii = null;
        // NullPointerException
        if (iii != null) {
            iii += 1000;
            System.out.println(iii);
        }
    }
}

Integer的面试题

public class IntegerDemo {
    public static void main(String[] args) {
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);
        System.out.println(i1.equals(i2));
        System.out.println("-----------");

        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4);
        System.out.println(i3.equals(i4));
        System.out.println("-----------");

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

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

        // 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
        // Integer ii = Integer.valueOf(127);
    }
}

Character类

Character类概述

Character类构造方法

Character类成员方法

练习题:

    public static void main(String[] args) {
        // 定义三个统计变量。
        int bigCount = 0;
        int smallCount = 0;
        int numberCount = 0;

        // 键盘录入一个字符串。
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String line = sc.nextLine();

        // 把字符串转换为字符数组。
        char[] chs = line.toCharArray();

        // 历字符数组获取到每一个字符
        for (int x = 0; x < chs.length; x++) {
            char ch = chs[x];

            // 判断该字符
            if (Character.isUpperCase(ch)) {
                bigCount++;
            } else if (Character.isLowerCase(ch)) {
                smallCount++;
            } else if (Character.isDigit(ch)) {
                numberCount++;
            }
        }

        // 输出结果即可
        System.out.println("大写字母:" + bigCount + "个");
        System.out.println("小写字母:" + smallCount + "个");
        System.out.println("数字字符:" + numberCount + "个");
    }
}
上一篇 下一篇

猜你喜欢

热点阅读