zoukankan      html  css  js  c++  java
  • JAva集合框架

    JAVA集合:包括两大类:collection和map。collection存储的是单个数据,map存储的是键值对。collection包括:set,list,queue. list的接口类是:array list是数组序列,

    set的接口类是hashset。list是有序的序列,set是存储不重复的元素,无序的序列。

    java 创建类时,属性私有化,通过共有的get,set方法获取。

    添加的方法,add(),add(),addall().分别为直接添加对象,添加索引值添加对象。(若索引值超过size或小于0则报出异常。)或者添加数组(Arrays.asList)是讲数组转化为list。或者通过索引值添加数组。

    remove是删除方法,类似于添加方法

    列子:

    Course c1=new Course("高数","1");
    coursetoselect.add(c1);
    Course temp=(Course) coursetoselect.get(0);
    System.out.println("添加了课程"+temp.getId()+":"+temp.getName());


    Course c2=new Course("语文","2");
    coursetoselect.add(0, c2);
    Course temp2=(Course) coursetoselect.get(0);
    System.out.println("添加了课程"+temp2.getId()+":"+temp2.getName());

    Course[] c3={ new Course("英语","3"), new Course("C#","4")};
    coursetoselect.addAll(Arrays.asList(c3));
    Course temp3=(Course) coursetoselect.get(2);
    Course temp4=(Course) coursetoselect.get(3);
    System.out.println("添加了两门课程: "+temp3.getId()+":"+temp3.getName()+temp4.getId()+":"+temp4.getName());

    Course[] c4={ new Course("英语1","5"), new Course("C#2","6")};
    coursetoselect.addAll(1,Arrays.asList(c4));
    Course temp5=(Course) coursetoselect.get(2);
    Course temp6=(Course) coursetoselect.get(3);
    System.out.println("添加了两门课程"+temp5.getId()+":"+temp5.getName()+temp6.getId()+":"+temp6.getName());
    }

    获取方法:

    通过size遍历,通过foreach,通过迭代器获取

    如下

    int size=coursetoselect.size();
    for(int i=0;i<size;i++)
    {
    Course course=(Course) coursetoselect.get(i);
    System.out.println("添加了课程:"+course.getId()+":"+course.getName());
    }

    for(Object obj:coursetoselect){
    Course cou=(Course)obj;
    System.out.println("添加:"+cou.getId()+":"+cou.getName());
    }@注释 通过for each循环获取数据。循环list数组,作为object ,参数obj;

    Iterator it=coursetoselect.iterator();
    while(it.hasNext()){
    Course co=(Course) it.next();
    System.out.println("添加课程:"+co.getId()+":"+co.getName());

    }

  • 相关阅读:
    铁乐学Python_Day35_Socket模块3和hmac模块
    铁乐学Python_Day34_Socket模块2和黏包现象
    铁乐学Python_Day33_网络编程Socket模块1
    铁乐学python_day29_模块与包学习4
    铁乐学python_day28_模块学习3
    铁乐学python27_模块学习2
    铁乐学python_md5校验两个文件的一致性
    铁乐学python26_hashlib+configparser+logging模块
    Flask与Ajax
    Javascript与Ajax
  • 原文地址:https://www.cnblogs.com/lvxiaojie/p/5087787.html
Copyright © 2011-2022 走看看