LinkedList、ListIterator,指定位置插入、修改元素

		LinkedList<String> ll = new LinkedList<String>();
		ll.add("aaaa");
		ll.add("bbbb");
		ll.add("cccc");

		// ListIterator(列表迭代器)有add及previous方法
		ListIterator<String> it = ll.listIterator();
		/***
		 * 在 “bbbb” 后面插入 “xxxx”
		 */
		while(it.hasNext()) {
			String item = it.next();
			if("bbbb".equals(item)) {
				it.add("xxxx");
			}
		}
		System.out.println(ll); // 输出结果: [aaaa, bbbb, xxxx, cccc]
		
		
		/***
		 * 在 “bbbb” 修改为 “BBBB”
		 */
		it = ll.listIterator();
		while(it.hasNext()) {
			String item = it.next();
			if("bbbb".equals(item)) {
				it.set("BBBB");
			}
		}
		System.out.println(ll); // 输出结果: [aaaa, BBBB, xxxx, cccc]
	

猜你喜欢

转载自blog.csdn.net/davidhhs/article/details/84542631
今日推荐