項目理解(一):Java語言基礎複習

1、版本控制系統:人人網,支付寶等用的都是svn;可以用: github, bitBucket等等。

2、封裝(實現接口)、繼承(繼承類)、多態(在類中對已有實現重新實現)。

3、Set、List、Map、Random、Date、Math

    public static void myPrint(int index, Object object) {
        System.out.println(String.format("{%d} %s", index, object.toString()));
    }

    public static void demoControlFlow() {
        int score = 60;
        if (score > 80) {
            myPrint(1, "A");
        } else if (score > 60) {
            myPrint(2, "B");
        } else {
            myPrint(3, "C");
        }

        String grade = "B";
        switch (grade) {
            case "A":
                myPrint(11, "better than B");
                break;
            case "B":
                myPrint(12, "is B");
                break;
            case "C":
                myPrint(13, "worse than B");
                break;
            default:
                myPrint(15, "not one");
        }

        for (int i = 0; i <= 4; ++i) {
            if (i == 1) continue;
            if (i == 3) break;
            myPrint(21, "loop");
            myPrint(31, i);
        }

        String str = "Hello world";
        for (char c : str.toCharArray()) {
            myPrint(41, c);
        }
    }

    public static void demoString() {
        String str = "Liuzewei is a greatful man";
        myPrint(1, str.indexOf('a'));
        myPrint(2, str.charAt(12));
        myPrint(3, str.codePointAt(12));
        myPrint(4, str.compareTo("uESTC"));
        myPrint(5, str.compareTo("Liuzewei is e greatful man"));
        myPrint(6, str.compareTo("liuzewei is a greatful man"));
        myPrint(7, str.compareToIgnoreCase("liuzewei is a greatful man"));
        myPrint(8, str.contains("s"));
        myPrint(9, str.concat(" \r\n Yes"));
        myPrint(10, str.startsWith("Liu"));
        myPrint(11, str.codePointBefore(2));
        myPrint(11, str.codePointCount(1, 2));
        myPrint(12, str.lastIndexOf('a'));
        myPrint(13, str.replace('a', 'e'));
        myPrint(14, str.replaceAll("a|L", "e"));

        StringBuilder strb = new StringBuilder();
        strb.append(true);
        strb.append(1);
        strb.append(2.2);
        myPrint(15, strb.toString());
    }

    public static void demoList() {
        List<String> strList = new ArrayList<String>();//ArrayList非常常用
        for (int i = 0; i < 4; i++) {
            strList.add(String.valueOf(i));
        }
        myPrint(1, strList);
        List<String> strListB = new ArrayList<String>();
        for (int i = 5; i < 7; i++) {
            strListB.add(String.valueOf(i));
        }
        myPrint(2, strListB);
        strListB.addAll(strList);
        myPrint(3, strListB);
        strListB.remove(0);
        myPrint(4, strListB);
        strListB.remove(String.valueOf(6));
        myPrint(5, strListB);

        myPrint(6, strListB.get(3));
        strListB.add("12");
        strListB.add(String.valueOf(15));
        Collections.sort(strListB);
        myPrint(7, strListB);
        /**
         * 首字母順序排序
         */
        Collections.sort(strListB, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        myPrint(8, strListB);
        Collections.reverse(strListB);
        myPrint(9, strListB);

        int[] array = new int[]{1, 2, 3};//用的少一些
        myPrint(10, array[2]);

    }
    @Test
    public void demoSet() {
        Set<String> strSet = new HashSet<String>();//集合中元素不重複
        for (int i = 0; i < 3; i++) {
            strSet.add(String.valueOf(i));
            strSet.add(String.valueOf(i));
            strSet.add(String.valueOf(i));
        }
        myPrint(1, strSet);
        strSet.remove("1");
        myPrint(2, strSet);
        strSet.contains("4");
        myPrint(3, strSet.contains("4"));
        strSet.addAll(Arrays.asList(new String[]{"12", "13", "14", "15"}));
        myPrint(4, strSet);
        for (String value : strSet) {
            myPrint(5, value);
        }
        myPrint(6, strSet.isEmpty());
        myPrint(7, strSet.size());
    }
    @Test
    public void demoKeyValue() {
        System.out.println(5/2);
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 4; i++) {
            map.put(String.valueOf(i), String.valueOf(i * i));
        }
        myPrint(1, map);
        map.entrySet().forEach(i->myPrint(8, i.getKey() + ":" + i.getValue()));
        map.entrySet().stream().forEach(i-> System.out.println(i.getKey()));
        for (Map.Entry<String, String> entry : map.entrySet()) {
            myPrint(2, entry.getKey() + ":" + entry.getValue());
        }
        myPrint(3, map.keySet());
        myPrint(4, map.values());
        myPrint(5, map.containsValue("4"));
        myPrint(6, map.get("2"));
        map.replace("1", "A");
        myPrint(7, map);

    }

    @Test
    public void demoException1() {
        try {
            myPrint(1, "hello");
            String a = null;
            a.indexOf('a');
            myPrint(4, a);
            int b = 2;
            b = b / 0;
            String c = null;
            c.indexOf("c");
        } catch (NullPointerException npe) {
            myPrint(3, "null");
        } catch (Exception e) {
            myPrint(5, "error");
        } finally {
            //做清理工作
            myPrint(2, "end1");
        }

    }
    @Test
    public void demoException() throws Exception {
        myPrint(1, "hello");
        String a = "e";
        a.indexOf('e');
        myPrint(4, a);
        int b = 2;
        if(b == 2){
            throw new Exception("我故意的");
        }
        b = b / 0;
        String c = null;
        c.indexOf("c");
    }
    @Test
    public void demoCommon() {
        Random random = new Random();//實際上是僞隨機數
        random.setSeed(1);    //初始seed設定後,刷新隨機函數不會改變值
        for (int i = 0; i < 4; i++) {
            myPrint(1, random.nextInt(100));
            myPrint(2, random.nextDouble());
        }
        List<Integer> array = Arrays.asList(new Integer[]{1, 2, 3, 8, 9});
        myPrint(3, array);
        Collections.shuffle(array);
        myPrint(4, array);

        Date data = new Date();
        myPrint(5, data.getTime());
        myPrint(6, data);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df2 = new SimpleDateFormat("y.M.d");
        myPrint(6, df.format(data));
        myPrint(8, df2.format(data));
        myPrint(7, df);
        myPrint(9, DateFormat.getDateInstance(DateFormat.SHORT).format(data));
        myPrint(10, UUID.randomUUID());
        myPrint(11, Math.ceil(1.1));//向上進位
        myPrint(12, Math.floor(5.8));//去除小數點,保留整數
    }

    public static void demoClass() {

        Animal animal = new Animal("Jim", 2);
        animal.say();
        animal = new Human("Bob",24,"China");
        animal.say();
    }
    @Test
    public void exercise(){
        Random random = new Random();
        random.setSeed(1);
        System.out.println(random.nextInt(100));
    }

    public static Animal getAnimal(int type){
        return new Human("LIu",25,"Jape");
    }
    public static void main(String[] args) {
//        demoControlFlow();
//        stringDemo();
//        demoString();
//        demoList();
//        demoSet();
//        demoKeyValue();
//        demoException1();
//        demoCommon();
        demoClass();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章