集合10 - Exception

Collection-Exception

public class _Exception {
    public static void main(String[] args) {
        //TODO Collection Exception

        //TODO ArrayList list = new ArrayList(-1);  => IllegalArgumentException 非法容量
        ArrayList list = new ArrayList(10);
        list.add("a");
        list.add("b");
        list.add("c");

        //TODO list.get(3); => IndexOutOfBoundsException 访问索引越界
        //如果访问的集合是数组,那么索引的范围是 0 到 数组长度-1
        //如果访问的集合是List,那么索引的范围是 0 到 数据长度-1  => 底层数组有空间但无数据的不能访问

        LinkedList list1 = new LinkedList();
        //TODO list1.getFirst(); => NoSuchElementException 访问数据不存在

        HashMap map = new HashMap();
        map.put("a","1");
        map.put("b","2");
        map.put("c","3");

        //TODO ConcurrentModificationException 遍历过程中修改数据 => 数据不一致 -- 迭代器解决
//        for (Object o : map.keySet()) {
//            if ("b".equals(o)) {
//                map.remove(o); 或 map.put
//            }
//
//            System.out.println(map.get(o));
//        }

    }
}
posted @   LaViez  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
点击右上角即可分享
微信分享提示