Java复习(1)-字符串,switch,数组

学过java太久没用,java语言还是挺有用的,开始复习,记录下复习的主要内容。简单的内容不做记录,记录内容主要是感觉生疏到内容。

复习方式以程序为主。

1.字符串

1.1 字符串的创建

输入如下:

package com.xxm.javaLange.test;

public class StringInit {
    public static void main(String[] args){
        String a = new String("我在");
        String b = "复习JAVA";
        String c = a + b;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }

}

输出:

我在
复习JAVA
我在复习JAVA

1.2 String类方法

代码如下:

package com.xxm.javaLange.test;

public class StringClassFunction {

    public static void main(String[] args){
        //String的类操作
        stringIndex();//String的索引方法
        stringConcat();//String的追加方法
        stringEqual();//String比较大小
        stringLength();//String长度
        stringReplace();//String替换
        stringSub();//String截取
        stringLowAndUp();//大小写转化
        stringTrim();//去掉最前的空白符
        stringTostring();//转义字符

        //StringBuffer的类操作
        stringStringbufferAppend();//StringBuffer追加
        stringStringbufferInsert();//StringBuffer插入
        stringStringBufferReverse();//StringBuffer颠倒
        stringStringbufferTostring();//StringBuffer转义字符
        stringStringLink();//String的链接,(像函数式编程)
    }

    public static void stringIndex(){
        String x="goodperson";
        System.out.println(x.charAt(5));//String的索引方法
        System.out.println("=========");
    }

    public static void stringConcat(){
        String x = "I love ";
        String y = "china";
        System.out.println(x.concat(y));//String的追加方法
        System.out.println("=========");
    }

    public static void stringEqual(){
        String x = "no";
        String y = "NO";
        String z = "no";
        String w = "Yes";
        System.out.println(x.equalsIgnoreCase(y));
        System.out.println(x.equalsIgnoreCase(z));//String比较中不区分大小写
        System.out.println(x.equalsIgnoreCase(w));
        System.out.println("==========");
    }

    public static void stringLength(){
        String x = "hello";
        String y = "长江三峡";
        String z = "my name is hellokity";
        System.out.println(x.length());
        System.out.println(y.length());//String长度,中文一个字也算一个
        System.out.println(z.length());
        System.out.println("==========");
    }

    public static void stringReplace(){
        String x = "恨你";
        System.out.println(x.replace('恨', '爱'));//String替换
        System.out.println("==========");

    }

    public static void stringSub(){
        String x = "杭州是一个美丽的城市";
        System.out.println(x.substring(5));
        System.out.println(x.substring(5,7));//String截取
        System.out.println("==========");
    }

    public static void stringLowAndUp(){
        String x = "I AM xianming";
        System.out.println(x.toLowerCase());//大写转小写
        System.out.println(x.toUpperCase());//小写转大写
        System.out.println("==========");

    }

    public static void stringTrim(){
        String x = "   I    am xianming";
        System.out.println(x);
        System.out.println(x.trim());//测试发现只能去掉前面掉空白符
        System.out.println("==========");
    }

    public static void stringTostring(){
        String x = "java is good";
        String y = x.toString();//转移字符
        System.out.println(x);
        System.out.println(y);//输出上没有区别
        System.out.println("==========");
    }

    //Stringbuffer operation
    public static void stringStringbufferAppend(){
        StringBuffer x = new StringBuffer("杭州西湖");
        x.append("是美丽的地方");
        System.out.println(x);
        StringBuffer y = new StringBuffer("杭州西湖");
        y.append(10+"分");
        System.out.println(y);
        System.out.println("==========");
    }

    public static void stringStringbufferInsert(){
        StringBuffer x = new StringBuffer("i am a boy");
        System.out.println(x.insert(7, "beauty "));
        System.out.println("==========");
    }

    public static void stringStringBufferReverse(){
        StringBuffer ch = new StringBuffer("谁是英雄");
        ch.reverse();
        System.out.println(ch);
        System.out.println("==========");
    }

    public static void stringStringbufferTostring(){
        StringBuffer ch = new StringBuffer("你是不是傻");
        ch.toString();
        System.out.println(ch);
        System.out.println("==========");
    }

    public static void stringStringLink(){
        String a = new String("GOOD");
        String b = a.concat(" boy").toLowerCase().replace('g', 'G');
        System.out.println(b);
        System.out.println("==========");
    }
}

输出:

e
=========
I love china
=========
true
true
false
==========
5
4
20
==========
爱你
==========
美丽的城市
美丽
==========
i am xianming
I AM XIANMING
==========
   I    am xianming
I   am xianming
==========
java is good
java is good
==========
杭州西湖是美丽的地方
杭州西湖10分
==========
i am a beauty boy
==========
雄英是谁
==========
你是不是傻
==========
Good boy
==========

2.Switch

这里记录一个switch比较特别大用法,来加深对default的理解。

package com.xxm.javaLange.test;

public class Switch {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int a = 1997;
        switch (a){
        case 1992:
            System.out.println(1992);
        default:
            System.out.println("no");
        case 1997:
            System.out.println(1997);
        case 2008:
            System.out.println(2008);
        }
    }

}

从上default可以理解为if else 中的else一样。
输出:

1997
2008

改a:

int a = 2000;

结果变为:

no
1997
2008

3.数组

输入:

package com.xxm.javaLange.test;

import java.util.Arrays;
import java.util.Random;

public class Array {
    public static void main(String[] args){

        arrayInit();//一维数组
        twoDimensionalArrayInit();//二维数组
        multiDimensionalArray();//多为数组(以三维为例)
        arrayCopy();//数组复制
        arraySort();//数组元素排序
        arrayEqual();//数组比较
        arraySearch();//数组调查
        arrayFill(args);//数组填充(这里用run configure来输入参数:6)
    }

    //一维数组
    public static void arrayInit(){
        Random rand = new Random();
        int[] a = new int[rand.nextInt(20)];
        double[] b = new double[rand.nextInt(20)];
        System.out.println("a的长度为" + a.length);
        System.out.println("b的长度为" + b.length);
        for(int i = 0;i<a.length;i++){
            a[i] = rand.nextInt(20);
            System.out.println("a["+i+"]="+a[i]);

        }
        for(int i = 0;i<b.length;i++){
            b[i] = rand.nextDouble();
            System.out.println("b["+i+"]="+b[i]);   
        }
        System.out.println("==========");
    }


    //二维数组
    int grades[][] = {
            {77,68,86,73},
            {96,87,89,81},
            {79,90,86,81}
    };

    public static void twoDimensionalArrayInit(){
        Array m = new Array();
        System.out.println("最小的数:" + m.minimum());
        System.out.println("最大的数:" + m.maximum());
        System.out.println("==========");
    }

    public int minimum(){
        int lowGrade = grades[0][0];
        for(int row = 0;row<grades.length;row++){
            for(int column = 0;column<grades[row].length;column++){  //用2个for循环遍历二维数组

                if(grades[row][column]<lowGrade)
                    lowGrade = grades[row][column];

            }
        }
        return lowGrade;
    }

    public int maximum(){
        int highGrade = grades[0][0];
        for(int row = 0;row<grades.length;row++){
            for(int column = 0;column<grades[row].length;column++){  //用2个for循环遍历二维数组
                if(grades[row][column]>highGrade)
                    highGrade = grades[row][column];

            }
        }
        return highGrade;
    }

    //多维数组(三维)
    public static void multiDimensionalArray(){
        //int[][][] a = new int[2][2][4];

        int[][][] a ={
                {{1,2,3,4},{3,4,5,6}},
                {{2,3,4,5},{5,6,7,8}}
        };

        for(int i=0;i<a.length;i++)
            for(int j=0;j<a[i].length;j++)
                for(int k=0;k<a[i][j].length;k++)
                    System.out.println("a["+i+"]["+j+"]["+k+"]="+a[i][j][k]);
        System.out.println("==========");
    }

    //数组的操作
    public static void arrayCopy(){
        int a;
        int b[] = {10,9,8,7,6,5,4,3,2,1};
        int c[] = new int[10];
        System.arraycopy(b, 2, c, 5, 4);
        for(a=0;a<c.length;a++)
            System.out.print(c[a]+" ");
        System.out.println();

        System.out.println("==========");
    }

    //排序数组
    public static void arraySort(){
        String[] s = new String[]{
                "abcd","ABCD","CDEF","2345"
        };
        Arrays.sort(s);
        System.out.println(Arrays.asList(s));
        System.out.println("==========");
    }

    //比较数组
    public static void arrayEqual(){
        int[] a1 = {1,2,3,4,5,6,7,8,9,0};
        int[] a2 = new int[9];
        System.out.println(Arrays.equals(a1, a2));
        int[] a3 = {1,2,3,4,5,6,7,8,9,0};
        System.out.println(Arrays.equals(a1, a3));
        int[] a4 = {1,2,3,4,5,6,7,8,9,0};
        System.out.println(Arrays.equals(a1, a4));
        System.out.println("==========");

    }

    //搜索数组
    public static void arraySearch(){
        int[] a = {1,2,3,4,1,2,3};
        Arrays.sort(a);
        System.out.println("排序后的数组为");
        for(int i=0;i<a.length;i++){
            System.out.println(+a[i]+" ");

        }
        System.out.println();
        int location = Arrays.binarySearch(a, 3); //查找数组a中的第一个数字3,
        System.out.println("查找3的位置是"+ location +",a["+location+"]="+a[location]);
        System.out.println("==========");

    }

    //填充数组
    public static void arrayFill(String[] args){
        int size = 0;
        if(args.length != 0)
            size = Integer.parseInt(args[0]);//字符串转为整数
        int[] a1 = new int[size];
        Arrays.fill(a1, 11);//填充数组值,都填为11
        for(int i=0;i<a1.length;i++){
            System.out.print("a1["+i+"]="+a1[i]+ " ");

        }
        System.out.println();
        System.out.println("==========");
    }
}

输出:

a的长度为4
b的长度为19
a[0]=13
a[1]=8
a[2]=0
a[3]=3
b[0]=0.6037102566105746
b[1]=0.8576519675359079
b[2]=0.020085167754375632
b[3]=0.011213092454811169
b[4]=0.34980622876166023
b[5]=0.36846424365250197
b[6]=0.2969837193697453
b[7]=0.3411644109532923
b[8]=0.18849447047455714
b[9]=0.16382836229419595
b[10]=0.15972358990378865
b[11]=0.9480945459180702
b[12]=0.5823281570544998
b[13]=0.06929513269880894
b[14]=0.9040077998033798
b[15]=0.39540186721725024
b[16]=0.014088259693522454
b[17]=0.15136295185772686
b[18]=0.019266337895619268
==========
最小的数:68
最大的数:96
==========
a[0][0][0]=1
a[0][0][1]=2
a[0][0][2]=3
a[0][0][3]=4
a[0][1][0]=3
a[0][1][1]=4
a[0][1][2]=5
a[0][1][3]=6
a[1][0][0]=2
a[1][0][1]=3
a[1][0][2]=4
a[1][0][3]=5
a[1][1][0]=5
a[1][1][1]=6
a[1][1][2]=7
a[1][1][3]=8
==========
0 0 0 0 0 8 7 6 5 0 
==========
[2345, ABCD, CDEF, abcd]
==========
false
true
true
==========
排序后的数组为
1 
1 
2 
2 
3 
3 
4 

查找3的位置是5,a[5]=3
==========
a1[0]=11 a1[1]=11 a1[2]=11 a1[3]=11 a1[4]=11 a1[5]=11 
==========
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值