Android开发小问题——java使用

2013-09-25

 

导语:离上次写博客有点久了,这次写两个开发中解决的问题吧。


正文:

1、ArrayList<E>使用remove问题;

2、字符串映射到函数运行方法;

====

1、ArrayList可以直接remove某个object,但是如果在遍历的过程中去remove个别选项的话就会报如下错误:

 

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
    at java.util.HashMap$KeyIterator.next(Unknown Source)
    at test.Test.main(Test.java:23)

解决方法 :新建一个ArrayList<Object>,然后在遍历的过程添加需要remove的选项,最后遍历结束调用removeAll即可。

2、函数映射还是很方便的,外部传入一个字符串,然后直接调用对应的函数(比如Fun_0(), Fun_1()。。。这样的话就很方便了),例子:

 

	try {
		Method method = this.getClass().getDeclaredMethod("action"+mActionFun, Canvas.class, Paint.class);
		method.invoke(this, canvas, paint);
	} catch (Exception e) {
		e.printStackTrace();
	}

其中getDeclareMethod传入的参数为: 函数名字, 变量类,变量类...,然后调用的时候使用invoke对应传入即可


以上!

 

结尾: 1)坚持写写博客

      2)继续学习开发

      3)我是IT程序猿

原文地址:https://www.cnblogs.com/pangblog/p/3341774.html