8.2学习笔记

知识点

1.TCP/IP

服务器server
public class MyServer {

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8080);
            System.out.println("服务器启动");
            Socket client = server.accept();
            InputStream is = client.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            OutputStream os = client.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            String rWord;
            Scanner scanner = new Scanner(System.in);
            String tWord;
            while(true){
                rWord = br.readLine();
                System.out.println(rWord);
                tWord = scanner.next();
                bw.write("褚文斌服务器:"+tWord+"\n");
                bw.flush();
            }
        } catch (IOException e) {
            System.err.println("客户端已经关闭");
        }
    }

}
客户端client
public class MyClient {

    public static void main(String[] args) {
        Socket client = null;
        try {
            client = new Socket("192.168.0.30", 8080);
            System.out.println("客户端启动");
            InputStream is = client.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            OutputStream os = client.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            Scanner scanner = new Scanner(System.in);
            String tWord;
            String rWold;
            while(true){
                tWord=scanner.next();
                bw.write("褚文斌客户端:"+tWord+"\n");
                bw.flush();
                rWold = br.readLine();
                System.out.println(rWold);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("服务器没有运行");
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

UDP

接收方
public class UDPReceiver {

    public static void main(String[] args) {
        try {
            DatagramSocket receiver = new DatagramSocket(8080, InetAddress.getLocalHost());
            byte[] array = new byte[1024];
            DatagramPacket packet = new DatagramPacket(array, 1024);
            receiver.receive(packet);
            System.out.println(new String(packet.getData()));
            receiver.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
发送方
public class UDPSend {

    public static void main(String[] args) {
        try {
            DatagramSocket send = new DatagramSocket();
            String s = "你能接收到我吗";
            byte[] array = s.getBytes();
            DatagramPacket packet = new DatagramPacket(array,array.length, InetAddress.getLocalHost(), 8080);
            System.out.println("正在发送数据...");
            send.send(packet);
            System.out.println("数据发送完毕");
            send.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

集合

  • 集合
    • collection
      • List
        • ArrayList
        • LinkedList(链表结构)
      • Set
        • HashSet
        • TreeSet
    • map
      • HashMap
      • TreeMap
ArrayList
    /*相对于LinkedList
     *ArrayList遍历容易,添加、删除麻烦
     *
     */
    List<String> list = new ArrayList<>();
    list.add("李涛");//原第0个元素
    list.add("李海萌");//原第1个元素
    list.add("李大嘴");//原第2个元素
    list.add(1, "陈冠希");//添加,后面依次后移一位。添加后是第1个元素
    for(String m:list){
        System.out.println(m);
    }
Set
    /*
     * Set 中不能放置重复的元素
     * HashSet无序    TreeSet有序
     */
    Set<Integer> set = new HashSet<>();
    Random random = new Random();
    int count = 0;
    while(count<10){
        int num = random.nextInt(90)+10;
        set.add(num);
        System.out.println("放完第"+count+"个数:"+num);
        count++;
    }
    System.out.println("已经放置完毕");
    Iterator<Integer> it = set.iterator();//迭代器方法
    while(it.hasNext()){
        int i = it.next();
        System.out.println(i);
    }
Map
    Map<String, Student> map = new HashMap<>();
    //第一个参数是键值对,第二个参数是被添加的内容
    map.put("张三", new Student("张三"));
    map.put("李四", new Student("李四"));
    map.put(null, new Student("张王五"));
    Set<String> keys = map.keySet();
    //第一种方法
    System.out.println("方法一:");
    Iterator<String> it = keys.iterator();
    while(it.hasNext()){
        String key = it.next();
        System.out.println(key+"||"+map.get(key).getName());
    }
    //第二种方法
    System.out.println("方法二:");
    for(String key:map.keySet()){
        System.out.println(key+"||"+map.get(key).getName());
    } 
Collections的排序方法sort()
    List<Student> list = new ArrayList<>();//尖括号里面的叫泛型
    list.add(new Student("战三",19));
    list.add(new Student("李四", 20));
    list.add(new Student("王五", 23));
    list.add(new Student("赵六", 21));
    list.add(new Student("马奇", 18));
    list.add(new Student("小明", 22));
    Collections.sort(list, new StuComparator());//升序
    Collections.reverse(list);//排列倒置
    for(Student stu:list){
        System.out.println(stu.getName()+"/"+stu.getAge());
    }

泛类型

泛型方法的写法
public class Student<C,P extends Pet> {//可以给尖括号中的泛类型限定范围
    private String name;
    private int age;
    private C clothes;//泛类型
    private P p;

    public C getClothes() {
        return clothes;
    }
    public void setClothes(C clothes) {
        this.clothes = clothes;
    }
    public P getP() {
        return p;
    }
    public void setP(P p) {
        this.p = p;
    }
    public Student(String name,int age,P p) {
        this.name = name;
        this.age = age;
        this.p = p;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}
随便写几个类充当泛型
public class Dog extends Pet{
    public void bite(){
        System.out.println("咬死你!");
    }
}
public class Cat extends Pet{
    public void miao(){
        System.out.println("喵喵喵");
    }
}
public class NanZhuang {

}
public class NvZhuang {

}
实现泛型方法的试例
public static void main(String[] args) {
    //尖括号内的叫做泛型
    Student<NanZhuang,Dog> xiaoming = new Student<>("小明", 19,new Dog());//此处只能填写Dog类,否则会出错
    xiaoming.getP().bite();//用的是狗的方法
    xiaoming.getClothes();//返回类型的女装
    Student<NvZhuang, Cat> xiaohong = new Student<>("小红", 17,new Cat());
    Cat cat = xiaohong.getP();
    cat.miao();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值