网络连接和集合、泛型

集合

Collection接口以及其中的常用方法

Collection是Set和List的父类
1.set是HashSet和TreeSet的父类
2.List是ArrayList和LinkedList的父类
Map是HashMap和TreeMap的父类

方法功能描述
add(E e)将指定的对象添加到该集合中
remove(Object o)将指定的对象从该集合中移除
isEmpty()返回booolean值,用与判断当前集合是否为空
iterator()返回在此Collection的元素上进行迭代,遍历集合
size()获取集合中元素的个数

示例:

//创建一个集合,注意导入包,
Collection<String> list=new ArrayList<>();
        list.add("a");
        list.add("b");
        //创建迭代器,然后遍历集合
        Iterator<String> it=list.iterator();
        while(it.hasNext()){
        //强制类型转换,恢复原来的特性
            String str=(String)it.next();//next()方法返回的是Object
            System.out.println(str);
        }

List集合(不唯一,有序)

List集合包括List接口以及List接口的所有实现类,List中允许重复,各元素的顺序就是对象插入的顺序。

ArrayList类实现了可变的数组,允许保存所有元素,包括Null,并可以根据索引的位置对集合进行快速的随机访问;缺点是向指定的位置插入对象或删除对象的速度较慢。

LinkedList类采用链表结构保存对象,这种结构的优点是便于向集合中插入和删除对象时效率较高。

//set(int index,Object obj):将集合中指定索引位置的对象修改为指定对象。

//get(int index):得到指定索引位置的对象

//创建实例化的List集合
List<E> list=new ArrayList<>();
List<E> list2=new LinkedList<>();


List<String> list=new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("d");
        int i=(int)(Math.random()*(list.size()-1));
        int j=list.size();//size比数组数大一,就是实际的长度。
        System.out.println(i);
        System.out.println(j);
        list.remove(2);
        for(int m=0;m<list.size();m++){
            System.out.println(list.get(m));
        }

ArrayList

对集合的各种操作,用到Collections类

"*********对集合的各种操作,用到Collections类**********"
//集合类
ArrayList<Student> list=new ArrayList<>();
        list.add(new Student("李四",11));
        list.add(new Student("王刚",12));
        list.add(new Student("小强",9));
        list.add(new Student("小红",10));
        list.add(new Student("小明",32));
        list.add(new Student("地方",15));
        list.add(new Student("李的",26));
        //必须创建一个类,在类中设置比较的方法,查看api文档
        Collections.sort( list,new StuComparator());//集合的排序,new一个新对象
        Collections.reverse(list);//集合的反转
        for(Student stu:list){//for each语句
            System.out.println(stu.getName()+"\t"+stu.getAge());
        }



        //实现接口类,另一个类
public class StuComparator implements Comparator<Student> {
    @Override
    public int compare(Student stu1, Student stu2) {
        //从大到小排序
        return stu1.getAge()-stu2.getAge();
        //从小到大排序
        //return stu2.getAge()-stu1.getAge();
    }

}       

当对其他非整形的数据进行比较时,用封装类进行转换

例如:上面的第二个值传入的是字符型的,这时可用封装类Integer的parseInt(String str)
list.add(new Student("李的","26"));
//则上式中改为
return Integer.parseInt(stu1.getAge())- Integer.parseInt(stu2.getAge());

Set集合(唯一,无序)

Set集合不是按特定的顺序排序,只是简单的加入,但Set中不能包含重复对象
1.HashSet类实现Set接口,由哈希表支持,它不保证Set的迭代顺序,特别是它不保证该顺序恒久不变,此类允许使用null元素
2.TreeSet类实现的Set集合在遍历集合时按照自然顺序递增,也可以按照指定的比较器递增排序,即可以通过比较器对用TreeSet类实现的Set集合中的对象进行排序。

技巧

headSet()、subSet()、tailSet()方法截取对象生成新集合时是否包含指定的参数,可通过如下方法判断:

如果指定参数位于新集合的起始位置,则包含该对象,如subSet()方法的第一个参数和tailSet()方法的参数;如果指定的参数是新集合的终止位置,则不包含参数,如headSet()方法的入口参数和subSet()方法的第二个入口参数。

1. TreeSet类增加的方法

Map(用map.put(k,v) 和 get( v) api-HashMap

可以通过HashMap类穿件Map集合,当需要顺序输出时,再创建一个完全相同映射关系的TreeMap类实例

HashTable不可以存放空值

java
//TreeMap(Map<? extends K,? extends V> m)
构造一个与给定映射具有相同映射关系的新的树映射,该映射根据其键的自然顺序 进行排序。

1.Map接口实现类有HashMap和TreeMap,建议使用HashMap类实现Map集合,因为这样集合的添加和删除映射效率更高;如果希望Map集合中的对象也存在一定的顺序,应该用TreeMap来实现。

HashMap<String, Student> map=new HashMap<>();
        map.put("xiao", new Student("张三"));
        map.put("赵六", new Student("王五"));
        map.put("zx", new Student("李四"));//用put进行赋值
        System.out.println(map.size());
        Set<String> set=map.keySet();//先生成set表
        Iterator<String> it=set.iterator();//用itrator遍历set表
        while(it.hasNext()){//hasNext()返回boolean值
            String key=it.next();
            Student stu=map.get(key);//通过key得到value
            System.out.println(stu.getName());//调用value的方法
        }

网络连接(TCP/UDP协议)

InetAddress(api InetAddress),没有构造方法

    try {
    //不能直接new一个对象,用getLocalHost()取得对象
        InetAddress address=InetAddress.getLocalHost();
        System.out.println(address.getHostName());
        System.out.println(address.getCanonicalHostName());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

C–S方式(api ServerSocket)

运行时先运行服务器代码,在运行客户端代码
在eclipse下面窗口可查看两个控制台的信息

//服务器代码
public static void main(String[] args) {
    try {
        System.out.println("服务器运转");
        ServerSocket server=new ServerSocket(8080);
        Socket socket=server.accept();//开启服务,等待连接
        InputStream is=socket.getInputStream();//获得socket输入流
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br=new BufferedReader(isr);
        String str=br.readLine();
        System.out.println("ZAId等待"+str);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
//客户端代码
public static void main(String[] args) {
    try {

        Socket socket=new Socket("192.168.0.31",8080);
        System.out.println("客户端启动");
        OutputStream os=socket.getOutputStream();
        OutputStreamWriter osw=new OutputStreamWriter(os);
        BufferedWriter bw=new BufferedWriter(osw);
        osw.write("hello\n");
        osw.flush();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

C–S交互访问

//服务器代码
public static void main(String[] args) {
    try {
        System.out.println("服务器运转");
        ServerSocket server=new ServerSocket(8080);
        Socket socket=server.accept();
        InputStream is=socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is);
        OutputStream os=socket.getOutputStream();
        OutputStreamWriter osw=new OutputStreamWriter(os);
        BufferedWriter bw=new BufferedWriter(osw);
        BufferedReader br=new BufferedReader(isr);
        Scanner input=new Scanner(System.in);
        while(true){
        String str=br.readLine();//等待客户端
        System.out.println("ZAId等待"+str);
        String s=input.next();
        osw.write(s+"\n");
        osw.flush();
        String str1=br.readLine();//等待客户端
        System.out.println("ZAId等待"+str1);

        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
//客户端代码
public static void main(String[] args) {
    try {
        Socket socket=new Socket("192.168.0.31",8080);
        InputStream is=socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br=new BufferedReader(isr);
        OutputStream os=socket.getOutputStream();
        OutputStreamWriter osw=new OutputStreamWriter(os);
        BufferedWriter bw=new BufferedWriter(osw);
        Scanner input=new Scanner(System.in);
        while(true){
            String s=input.next();
            bw.write(s+"\n");
            bw.flush();
            String sr=br.readLine();
            System.out.println("服务端返回"+sr); 
        }
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

泛型

使用泛型类型的通配符

泛型类名称<? extends List> a=null;
"其中<? extends List>表示未知类型,当需要时要单独实例化,另外使用通配符声明的实例化对象不能加入新的信息,只能获取或删除。"
A<? extends List> a=null;
a=new A<ArrayList>;
a=new A<LinkedList>;


"如果使用A<? super List> a=null;"//这样定义对象a,只能接受List接口或上层父类类型
//测试代码
Student<ManColth,Dog> xiaoming=new Student<>("李四",11);
        xiaoming.setPet(new Dog());
        Dog dog=xiaoming.getPet();
        dog.voice();

"******************************************************"
//student类部分代码
//通过继承对其传入对象进行限制
public class Student<T,E extends Pet> {
private String name;
private int age;
private T colth;//用泛类创建变量设置,添加set、get方法
private E pet;

public T getColth() {
    return colth;
}
public void setColth(T colth) {
    this.colth = colth;
}

"**************************************************"
public class Pet {

}
"**************************************************"
public class Dog extends Pet{
    public void voice(){
        System.out.println("小狗叫");
    }
}
"**************************************************"

public class Cat extends Pet {

}
"**************************************************"
public class ManColth {

}
"**************************************************"
public class WomenColth {

}
"**************************************************"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值