- 浏览: 205033 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
jongde1:
Axure太难学了,分享mockplus工具,有兴趣可以去了解 ...
Axure RP 原型设计工具 -
di1984HIT:
这里面提到了好几种解决办法。
Spring AOP对日志记录、Exception日志记录 -
di1984HIT:
学习一下。
spring struts2 零配置 -
di1984HIT:
不错,不错啊
Struts2防止表单重复提交 -
di1984HIT:
kettle怎么样啊。
Kettle初探
懒程序员,在代码越写越多的情况下,总想着使用把代码精简一下,能不写if else的,能不写for循环的尽量不想写,但是遇到的问题都要解决的呀,因此调用同逻辑的已存在方法便是首选。
今天就集合类(List)两个容易出错的方法做个记录,可以引以为戒,并且也提供正常的使用方式, 都是在java.utils包,方便使用。
package com.longer.list; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import junit.framework.TestCase; /** * * @author Longer * Apr 23, 2010 2:49:13 PM */ public class ListTest extends TestCase { class TempBean{ public TempBean( String str ){ this.str = str; } String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } } List<TempBean> tempList = null; protected void setUp() throws Exception { super.setUp(); tempList = new ArrayList<TempBean>(); tempList.add( new TempBean("1") ); tempList.add( new TempBean("2") ); tempList.add( new TempBean("30") ); tempList.add( new TempBean("31") ); tempList.add( new TempBean("3") ); } protected void tearDown() throws Exception { super.tearDown(); tempList.clear(); tempList = null; } public void testRemove1(){ for (TempBean bean : tempList ) { //exception:java.util.ConcurrentModificationException //tempList.remove( bean ); } System.out.println( tempList ); } public void testRemove2(){ for (int i = 0; i < tempList.size(); i++) { TempBean bean = tempList.get(i); tempList.remove( i );//or tempList.remove(bean); i--; } System.out.println( tempList ); } public void testRemove3(){ System.out.println("before remove:" + tempList ); for (Iterator iterator = this.tempList.iterator(); iterator.hasNext();) { iterator.remove(); //exception:java.lang.IllegalStateException //tempList.add( new TempBean("") ); } System.out.println("after remove:"+ tempList ); } public void testCopy1(){ List<TempBean> newList = new ArrayList<TempBean>(); //exception:java.lang.IndexOutOfBoundsException: Source does not fit in dest //Collections.copy( newList, tempList ); } public void testCopy2(){ List<TempBean> newList = new ArrayList<TempBean>(Arrays.asList(new TempBean[ this.tempList.size()])); Collections.copy( newList, tempList ); System.out.println( newList ); } }
异常解释:
1:testRemove1-->java.util.ConcurrentModificationException
此类的JavaDOC部分原文:
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible。
简单翻译:
检测到一个对象存在同步修改,并且此修改不被允许的情况下,此异常将被方法抛出。
因此, 可以看出List是不支持同步修改的,其实整个Collection、Map都与List一样的情况,详细的原因得从迭代器Iterator去研究,这里不做分析。大概理解是Iterator在遍历的时间内,其源对象索引发生了改变,导致了不同步现象。
2)testRemove3-->java.lang.IllegalStateException
此类的JavaDOC部分原文:
Signals that a method has been invoked at an illegal or inappropriate time
简单翻译:
信息灯,一个方法在非法或者不恰当的时间内被调用。
因此, 问题的根源同上。
3)testCopy1-->java.lang.IndexOutOfBoundsException
Collections.copy JavaDOC部分原文:
......The destination list must be at least as long as the source list.......
简单翻译:
目的list必须至少要与源list等长。(目的List可以比源List长度长)
因此, 它这里指的长度是List里的size()方法值,我们都知道new ArrayList()初始化的时候size()等于0,即便是你使用new ArrayList(10)来初始化,也只是预设了一个initialCapacity==10的存储空间,size()还是等于0。因此在使用Collections.copy之前,需要把目的List加一些空的元素,直到目的List的size()值与源List的size()值等长(或更长)。
发表评论
-
Linux下部署多个Tomcat多个域名
2015-12-12 19:02 3646一、安装JDK 1、安装jdk-7u79-linux-x64. ... -
linux下安装swftools和openOffice
2015-07-03 17:09 699最近公司实现一个仿豆丁网百度文库阅读器的功能,需要用到两个软件 ... -
redis Java develop
2014-10-23 18:01 6061. http://javacrazyer.iteye.com ... -
验证码 原理 破解
2014-08-20 17:52 579验证码 原理 破解 reference: http://bl ... -
HttpClient 学习经验
2014-08-14 11:01 596HttpClient学习经验 HttpCl ... -
P2P resources
2013-12-09 18:24 9231.P2P导航收录 http://www.p2peye.com ... -
运用加密技术保护Java源代码
2013-06-08 08:38 1066运用加密技术保护Java源代码 http://www.ibm ... -
log4j.properties配置详解
2013-03-15 17:00 0log4j.properties配置详解 Log4J的配置文 ... -
Struts 2 studing
2012-12-28 17:28 7071. Struts 2的基石——拦截器(Interceptor ... -
J2EE项目异常处理
2012-12-26 11:08 1042J2EE项目异常处理 为什 ... -
如何将基于 Struts、Spring 和 Hibernate 的应用从 Tomcat 迁移到 WebSphere Application Server
2012-12-21 10:28 1143引言 现在很多的企业都 ... -
详解spring事务属性
2012-12-20 10:22 780Spring声明式事务让我们从复杂的事务处理中得到解脱。使得我 ... -
MySQL---ORACLE序列解决方案
2012-11-13 00:55 1158MySQL自增长与Oracle序列的区别: 自增长只能用于表 ... -
使用反射循环查找所有父类属性
2012-11-02 01:28 2090使用反射循环查找所有父类属性 ... -
list,set,map,数组间的相互转换
2012-11-02 01:25 919list,set,map,数 ... -
java bean自动进行rowMapper or handler的类
2012-10-20 03:14 1254一般情况下在进行jdbc编程的时候避免不了的要写n多的bean ... -
URL encoding 乱码处理
2012-10-10 15:53 850搞了两三天的乱码处理,试了很多方法,过滤器啊,编码转换啊,试来 ... -
JAVA 按任意角度旋转图片,并生成新的旋转后图片
2012-07-11 11:03 3613JAVA 按任意角度旋转图片,并生成新的旋转 ... -
QR Code
2012-06-12 12:29 01. 在线生成QR Code 网站 http://www. ... -
convert BufferedImage to byte[]
2012-06-12 11:26 1206How to convert BufferedImage to ...
相关推荐
List 标记: interface ArrayList 标记: class CRUD : boolean add(E e) boolean remove(Object o) E set(int index, E element) E get(int index) 底层数组实现,查询快,增删慢 LinkedList 标记: ...
configuring a server to relay messages or to add and remove e-mail accounts. POP POP stands for Post Office Protocol. Currently in version 3, also known as POP3, RFC 1939 defines this protocol. ...
Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary ...
L.remove(v) L.pop(index) L.sort(reverse=False) L.extend() 深拷贝 和 浅拷贝 copy.deepcopy L.copy() L2 = L 字符串方法: S.split(sep=None) S.join(序列) 列表推导式 [表达式 for 变量 in 可迭代...
每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium Remove Nth Node From End of List Swap Nodes in Pairs Spiral Matrix Path Sum II Copy List with Random ...
import java.util.List; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.JTree; import javax.swing.Timer; import javax.swing.event.TreeExpansionEvent; import javax.swing.event....
F1Engine V0.1 alpha版本是我用晚上的业余时间写的一个java文件操作组件,目前只是在原型的测试阶段。 主要的设计目标是:简单方便 可以方便地加到应用中,可以满足日常应用中的大部分应用场景。 二、使用...
installed, remove it from the approved list and delete the "extensions\ActivityGrader" folder. Then follow the "Installing Activity Grader" section. Installing Activity Grader ----------...
installed, remove it from the approved list and delete the "extensions\ActivityGrader" folder. Then follow the "Installing Activity Grader" section. Installing Activity Grader ----------...
需要将 J2EE 用对地方,那么只有理解 J2EE 此类框架软件的精髓,那么你才能真正灵活应用 Java 解决你的问题,甚至构架出你自 己企业的框架来。(我们不能总是使用别人设定好的框架,为什么不能有我们自己的框架?) ...
Used with -c / --copy-original on [b]uild unknown = Files / folders that are not part of the standard AOSP build procedure. These files will be injected back into the rebuilt APK. apktool.yml ...
Remove blank lines A question we often see is "I have a lot of blank lines in my file and I don't want to go through and manually delete them. Is there an easier way to do this?" The answer is: yes! ...
1.x/6.0 (Java) <END><br>6 , ocxex.zip "This is a quick example I made to show you how to use Events and properties in a OCX."<END><br>7 , news.exe This control aids as a complete Newsgroup ...
Classes Doing Work in Constructors Default Constructors Explicit Constructors Copy Constructors Structs vs. Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control ...
Overrides the standard java.lang.Object.clone method to return a copy of this cookie. containsHeader(String) - Method in class javax.servlet.http.HttpServletResponseWrapper The default behavior of ...