Java基礎回顧(牛客網專案課程)

HowieLee59發表於2019-01-05

藉著專案的課程把Java基礎回顧了下,以下為程式碼部分:

package com.howie;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @Author:HowieLee
 * @Date:1/4/2019
 * @Description:com.howie
 * @version:1.0
 */
public class Main {
     /**
       * 功能描述:
       * @param:
       * @return:
       * @author:HowieLee
       * #date:2019/01/04
       */
    public static void print(int index,Object object){
        System.out.println(String.format("{%d},%s",index,object.toString()));
    }
    public static void main(String[] args) {
        //print(1,"Hello World!");
        //demoOperation();
        //demoString();
        //demoControlFlow();
        //demoList();
        //demoMapTable();
        //demoSet();
        //demoException();
        //demoOO();
        demoFunction();
    }
    public static void demoOperation(){
        print(1,5 + 2);
        print(2,5 - 2);
        print(3,5 * 2);
        print(4,5 / 2);
        print(5,5 % 2);
        print(6,5 << 2);
        print(7,5 >> 2);
        print(8,5 | 2);
        print(9,5 ^ 2);
        print(10,5 == 2);
        print(11,5 != 2);
        print(11,5.0 / 2);
        print(11,5 / 2.0);

        int a = 11;
        double b = 2.2f;
        a += 2;
        print(14,a);
    }
    public static void demoString(){
        String str = "Hello world!";
        print(1,str.indexOf('e'));
        print(2,str.charAt(3));
        print(3,str.compareToIgnoreCase("HELLO WORLD"));
        print(4,str.compareTo("hello world"));//看第一個不一樣的字元的差距
        print(5,str.contains("hello"));
        print(6,str.concat("!!!"));
        print(7,str.toUpperCase());
        print(8,str.startsWith("hello"));
        print(9,str.endsWith("world"));
        print(10,str.replace('o','e'));
        print(11,str.replaceAll("0|l","a"));//支援正規表示式
        print(12,str.replaceAll("Hello","hi"));
        print(13,str + str);

        StringBuilder sc = new StringBuilder();//StringBuilder為執行緒不安全的,應該用StringBuffer
        sc.append("x+");
        sc.append('a'+"+");
        sc.append(1.2+"+");
        sc.append(true);
        print(14,sc.toString());
    }
    public static void demoControlFlow() {
        int a = 2;
        int target = a == 2 ? 1 : 3;
        print(1, target);
        if (a == 2) {
            target = 1;
        } else {
            target = 3;
        }
        print(2, target);

        String grade = "B";
        switch (grade) {
            case "A":
                print(3, ">80");
                break;//遇到break才可以停止
            case "B":
                print(3, "60-80");
                break;
            case "C":
                print(3, "<60");
                break;
            default:
                print(3, "未知");
                break;
        }

        for(int i = 0 ; i < 4 ; ++i){
            if(i == 0){
                continue;
            }
            print(4,i);
            if(i == 2){
                break;
            }
        }

        int score = 60;
        while(score <= 100){
            if(score == 80){
                continue;
            }
            print(5,score);
            score += 20;
        }
    }
    public static void demoList(){
        List<String> strList = new ArrayList<String>(10);
        for(int i = 0 ; i < 4 ; ++i){
            strList.add(String.valueOf(i));
        }
        print(1,strList);

        List<String> strList2 = new ArrayList<String>();
        for(int i = 0 ; i < 4; i++){
            strList2.add(String.valueOf(i * i));
        }
        strList2.addAll(strList);
        print(2,strList2);

        strList.remove(0);
        print(3,strList);

        strList.remove(String.valueOf(1));
        print(4,strList);
        print(5,strList.get(1));

        Collections.reverse(strList2);
        print(6,strList2);

        Collections.sort(strList2);
        print(7,strList2);

        Collections.sort(strList2, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        print(8,strList2);

        for(String obj : strList2){
            print(9,obj);
        }

        for(int i = 0 ; i < strList2.size() ; i++){
            print(10,strList2.get(i));
        }

        int[] array = new int[]{1,2,3};
        for(int j : array){
            print(11,j);
        }
    }

    public static void demoMapTable(){
         Map<String,String> map = new HashMap<String,String>();
         for(int i = 0 ; i < 4 ; ++i){
             map.put(String.valueOf(i),String.valueOf(i * i));
         }
         print(1,map);


         for(Map.Entry<String,String> entry : map.entrySet()){
             print(2,entry.getKey() + " | " + entry.getValue());
         }

         print(3,map.values());
         print(4,map.keySet());
         print(5,map.get("3"));
         print(6,map.containsKey("A"));
         map.replace("3","27");
         print(7,map.get("3"));
    }
    public static void demoSet(){
        Set<String> strSet = new HashSet<String>();
        for(int i = 0 ; i < 3 ; i++){
            strSet.add(String.valueOf(i));
        }
        print(1,strSet);
        strSet.remove(String.valueOf(1));
        print(2,strSet);
        print(3,strSet.contains(String.valueOf(1)));
        print(4,strSet.isEmpty());
        print(5,strSet.size());
        strSet.addAll(Arrays.asList(new String[]{"A","B","C"}));
        print(6,strSet);

        for(String value : strSet){
            print(7,value);
        }
    }
    public static void demoException(){
        try{
            int k = 2;
            //k = k / 0;
            if(k == 2){
                throw new Exception("我是故意的");
            }
        }catch (Exception e){
            //e.printStackTrace();//顯示報錯
            print(2,e.getMessage());
        }finally {//要主動關掉一些東西
            print(3,"finally");
        }
    }
     /**
       * 功能描述:物件導向演示
       * @param:
       * @return:
       * @author:HowieLee
       * #date:
       */
    public static void demoOO(){
        Animal a = new Animal("Jim",1);
        a.say();
        Animal human = new Human("Lei",11,"China");
        human.say();
    }
     /**
       * 功能描述:隨機數
       * @param:
       * @return:
       * @author:HowieLee
       * #date:
       */


    public static void demoFunction(){
        Random random = new Random();//方式為偽隨機數
        random.setSeed(1);
        print(1,random.nextInt(1000));
        print(2,random.nextFloat());//預設是在0到1之間
        List<Integer> array = Arrays.asList(new Integer[]{1,2,3,4,5});
        Collections.shuffle(array);
        print(3,array);

        Date date = new Date();
        print(4,date);
        print(5,date.getTime());

        DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        print(6,df.format(date));

        print(7,UUID.randomUUID());
        print(8,Math.log(10));
        print(8,Math.min(3,10));
        print(8,Math.max(111,10));
        print(8,Math.ceil(2.2));
        print(8,Math.floor(2.2));
    }

}

Animal.java

package com.howie;

/**
 * @Author:HowieLee
 * @Date:1/4/2019
 * @Description:com.howie
 * @version:1.0
 */
public class Animal implements Talking{
    private String name;
    private int age;

    public Animal(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public void say() {
        System.out.println(name + " Animal Say");
    }
}

 

Human.java

package com.howie;

/**
 * @Author:HowieLee
 * @Date:1/4/2019
 * @Description:com.howie
 * @version:1.0
 */
public class Human extends Animal{
    private String country;

    public Human(String name,int age,String contry){
        super(name,age);
        this.country = contry;
    }

    @Override
    public void say() {
        System.out.println("This is Human from" + country);
    }
}

Talking.java

package com.howie;

/**
 * @Author:HowieLee
 * @Date:1/4/2019
 * @Description:com.howie
 * @version:1.0
 */
public interface Talking {
    void say();
}

 

相關文章