获取一个序列中的不重复数据
来源:互联网 发布:匡恩网络内部融资 编辑:程序博客网 时间:2024/06/16 14:30
1: import java.util.ArrayList;
2: import java.util.List;
3:
4: public class Tools {
5:
6: /**得到参数的不重复字符串序列返回
7: * @param origin
8: * @return
9: */
10: public static ListgetUniversal(List origin) {
11: Listval = new ArrayList ();
12: val.add(origin.get(0));
13: for (String str : origin) {
14: int i;
15: for (i = 0; i < val.size(); i++) {
16: if (str.equals(val.get(i))) {
17: break;
18: }
19: }
20: if (i > val.size() - 1) {
21: val.add(str);
22: }
23: }
24: return val;
25: }
26:
27: public static void main(String[] args) {
28: Lista = new ArrayList ();
29: a.add("String");
30: a.add("Integer");
31: a.add("String");
32: a.add("Long");
33: a.add("String");
34: a.add("Long");
35: a.add("String");
36: a.add("Double");
37: a.add("String");
38: a.add("String");
39: System.out.println(Tools.getUniversal(a));
40: }
41: }
花了些时间写了上面的这些代码,在解决如题的问题时,写过如下代码:
1: public static ListgetUniversal(List origin){
2: Listval = new ArrayList ();
3: int i = 0;
4: val.add(origin.get(i++));
5: for(String s:val){
6: String str = origin.get(i++);
7: if(!str.equals(s)){
8: val.add(str);
9: }
10: }
11: return val;
12: }
运行后得到的一个 ConcurrentModificationException ,查了一下 API,得知这个是 Exception 的大致意思是不能对正在迭代的元素进行修改。可见 foreach 循环是通过 iterator 实现的。 API 如下:
public class ConcurrentModificationException
- extends RuntimeException
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.
Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast operations throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: ConcurrentModificationException should be used only to detect bugs.
- Since:
- 1.2
- See Also:
Collection
,Iterator
,ListIterator
,Vector
,LinkedList
,HashSet
,Hashtable
,TreeMap
,AbstractList
, Serialized Form
---EOF---
自我感觉这段代码挺重要,留下笔记。
- 获取一个序列中的不重复数据
- 生成一个不重复随机序列
- erlang获取一个5900——65535之间不重复的一个数据
- 如何(SQL):使得一张表中的数据可以同时被多个源所获取而不重复
- java中获取不重复的序列(线程安全)
- 一个C语言编写的不重复随机序列算法
- 获取一个连续不重复的流水号
- 消除序列中的重复
- 获取不重复随机数
- 获取不重复随机数
- 获取字符串中的第一个不重复的字符
- mysql 获取某字段不重复数据,删除某字段重复数据
- mysql 获取某字段不重复数据,删除某字段重复数据
- 删除数据库中的重复数据记录、查找重复记录、查找不重复记录
- Datatable 中的数据查询(查询不重复记录)
- Datatable 中的数据查询(查询不重复记录)
- mysql中的不显示重复数据--group by
- 查询不重复数据
- php-handlersocket
- updateConfiguration 的一些使用及修改 系统的 Locale
- 标准博客 API .BLOG APIS
- sh 文件执行异常
- Java 6 JVM参数选项大全(中文版)
- 获取一个序列中的不重复数据
- tlb与dll文件区别
- 在屏幕旋转时屏蔽应用重启
- 找不到命令:repo
- 程序NSIS ERROR出错解决方案
- 解决 Servlet 乱码问题
- 【Android基础】调用系统发Email和短信
- Servlet 读取 HTML 中同一 NAME 属性值的多个参数
- 如何用repo下载android源码 (转)