1.for循环方式(Set集合不能使用,因为Set是无序的没有索引) for (int i = 0; i < list.size(); i++) { Object o = list.get(i); System.out.println(o); } 2.foreach String[] QQ={"26578","34535353","756345234","757445676","34535353",&…
Exception in thread "main" java.util.ConcurrentModificationException 并发修改异常引发的思考! 1 foreach循环删除元素 ①list遍历删除元素时会报错,比如下面删除字符串"aa",也有遍历不报错的例子,看下面的例子 public class TestMain { public static void main(String[] args) { ArrayList<String>…
习惯用法 for.foreach循环.iterator迭代器都是我们常用的一种遍历方式,你可以用它来遍历任何东西:包括数组.集合等 for 惯用法: List<String> list = new ArrayList<String>(); String[] arr = new String[]{"1,2,3,4"}; for(int i = 0;i < arr.length;i++){ System.out.println(arr[i]); } for(i…
先来看一段代码,摘自阿里巴巴的java开发手册 List<String> a = new ArrayList<String>(); a.add("1"); a.add("2"); for (String temp : a) { if("1".equals(temp)){ a.remove(temp); } } 此时执行代码,没有问题,但是需要注意,循环此时只执行了一次.具体过程后面去分析.再来看一段会出问题的代码: Lis…
Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string will be in the form of each letter followed by a positive integer representing the numbe…