StringController.java 3.0 KB
Newer Older
X
xiaoyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
package com.example.demo.controller;

import com.example.demo.bo.UserBo;
import com.example.demo.tool.Gengric;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@RestController
@RequestMapping("/")
public class StringController {

    @GetMapping("/string")
    public void stringDemo(){
        //字符
        char charDemo1 = 'x';
        Character charDemo2 = new Character('x');

        //字符串
        String stringDemo1 = "test";
        String stringDemo2 = new String("test");

        stringDemo2.length(); //获取长度
        stringDemo2.concat(" user"); //拼接字符串
        stringDemo2.toLowerCase(); //转小写
        stringDemo2.compareTo(stringDemo1); //对比
        stringDemo2.substring(1,3); //截取

        //字符串缓冲区
        StringBuffer stringDemo3 = new StringBuffer("test");
        stringDemo3.append(" user"); //追加
        stringDemo3.insert(2,"u"); //插入
        stringDemo3.delete(2,3); //删除
        stringDemo3.length(); //获取长度
        stringDemo3.substring(0,2); //截取

    }

    @GetMapping("/array")
    public void arrayDemo(){
        //数组,必须指定大小
        String[] arrDemo1 = new String[3];
        arrDemo1[0] = "str1";
        arrDemo1[1] = "str1"; //值可以重复
        arrDemo1[2] = "str1";

        String[] arrDemo2 = arrDemo1.clone(); //复制 克隆
        Arrays.fill(arrDemo2,"test2"); //Array类的使用

        UserBo[] arrDemo3 = new UserBo[5]; //对象数组,不是只能用基本类型
    }

    @GetMapping("/arraylist")
    public void arraylistDemo(){
        //泛型可以接收任何参数
        Gengric<String> aDemo1 =new Gengric<>();
        aDemo1.setDemo1("string");
        //不只是基本数据类型
        Gengric<UserBo> aDemo2 =new Gengric<>();
        UserBo userBo = new UserBo();
        aDemo2.setDemo1(userBo);

        //单向链表
        LinkedList<String> aDemo3 = new LinkedList<>();
        aDemo3.add("str1"); //插入
        aDemo3.get(0); //获取
        aDemo3.addFirst("str2"); //头部添加
        aDemo3.addLast("str3"); //尾部添加
        aDemo3.size(); //长度

        //动态数组
        ArrayList aDemo4 = new ArrayList<>();
        aDemo4.add(userBo);
        aDemo4.add("str5");
        aDemo4.add(100); //可以加入不同数据类型

        //HashSet
        HashSet<Integer> aDemo5 = new HashSet<>();
        aDemo5.add(1);
        aDemo5.add(2);

        //TreeSet
        TreeSet<Integer> aDemo6 = new TreeSet<>();
        aDemo6.add(20);
        aDemo6.add(10);
        aDemo6.add(50);

        //HashMap
        HashMap<Integer,String> aDemo7 = new HashMap<>();
        aDemo7.put(5,"str8");
        aDemo7.put(1,"str9");
        aDemo7.put(3,"str10");


        //TreeMap
        TreeMap<Integer,String> aDemo8 = new TreeMap<>();
        aDemo8.put(5,"str8");
        aDemo8.put(1,"str9");
        aDemo8.put(3,"str10");



    }


}