zoukankan      html  css  js  c++  java
  • 第20节:Java集合框架 【多测师_王sir】

    1、   数组的长度不可变,ArrayList的长度可变。如下所示:
    添加长度的方法List的长度是不固定,是可变长度,有序的集合
           ArrayList<Integer> li=new ArrayList();
           li.add(1);
           li.add(1,40);
    2、   HashSet 该类实现了Set接口,不允许出现重复元素,不保证集合中元素的顺序,允许包含值为null的元素,但最多只能一个。
    3、   HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
    4、   Set 接口实例存储的是无序的,不重复的数据。 List 接口实例存储的是有序的,可以重复的元素。
    5、   ArrayList是实现了List的接口,实现了可变大小的数组,随机访问和遍历元素时,提供更好的性能。
    6、   集合的长度。如下所示:xx.size()
    7、   集合遍历的方法:
    ①System.out.print("遍历集合方法一:");
            for (Integer bb:li) {
                System.out.print(bb+"   "); }
    ②System.out.print("遍历集合方法二:");
            Integer [] ger=new Integer[li.size()];
            li.toArray(ger);
            for (Integer integer:ger) {
                System.out.print(integer+"   ");}
    ③System.out.print("遍历集合方法三:");
            Iterator<Integer>ite=li.iterator();
            while (ite.hasNext()){
                System.out.print(ite.next()+"   ");}
    8、   获取集合中选定索引的元素:xx.get(3)
    9、   集合中删除一个元素(删除对应的值)xx.remove(newInteger(x));
    10、 集合中删除一个元素(删除对应的索引值)xx.remove(x);
    11、 将一个集合中所有的元素添加到另外一个集合中:
    List<Integer>is =Arrays.asList(33,34,35);
            li.addAll(is);
    12、 遍历Map
    ①System.out.print("获取key的值:");
           Set<String> s1 =map.keySet();
           for (String ss:s1) {
               System.out.print(ss);
               // Map通过Value获取值
               Integer ii=map.get(ss);
               System.out.print("="+ii+"    ");}
    ②System.out.print("获取Value的值:");
            Collection<Integer> itg=map.values();
            for (Integer integer:itg) {
                System.out.print(integer+"   ");}
    ③System.out.print("通过Map.entrySet使用iterator遍历key和value:");
           Iterator<Map.Entry<String,Integer>>  it =map.entrySet().iterator();
            while (it.hasNext()){
                Map.Entry<String,Integer> en=it.next();
                System.out.print(en.getKey()+"="+en.getValue()+"   ");}
    ④System.out.print("通过Map.entrySet遍历key和value:");
            for (Map.Entry<String,Integer>en:map.entrySet()) {
               System.out.print(en.getKey()+"="+ en.getValue()+"   ");}
    13、 在Map是否存在某个Key:boolean b1=map.containsKey("xx");
  • 相关阅读:
    hdu 1025 lis 注意细节!!!【dp】
    简单的数据生成方法
    注意特殊情况!最长上升子序列!!poj2533
    括号序列问题 uva 1626 poj 1141【区间dp】
    UVa 10502【dp】
    hdu 1024 MAX Sum Plus Plus【dp】
    python 《核心编程》 1,2章
    c语言 文件链表实现最简单的学生管理系统
    Python和Python解释器
    计算机基础小结
  • 原文地址:https://www.cnblogs.com/xiaoshubass/p/13602336.html
Copyright © 2011-2022 走看看