c)將指定collection中的所有元素都添加到此collection中(可選操作)。voidcl" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Java集合2:類 AbstractCollection及源碼

系統 1842 0

?

1.繼承關系圖


Java集合2:類 AbstractCollection及源碼
?
?

2. 概覽

此類提供? Collection ?接口的骨干實現,以最大限度地減少了實現此接口所需的工作。


Java集合2:類 AbstractCollection及源碼
3.方法

構造方法摘要
protected AbstractCollection () ?
??????????唯一的構造方法。
? 方法摘要
?boolean add (E?e) ?
??????????確保此 collection 包含指定的元素(可選操作)。
?boolean addAll (Collection<? extends?E>?c) ?
??????????將指定 collection 中的所有元素都添加到此 collection 中(可選操作)。
?void clear () ?
??????????移除此 collection 中的所有元素(可選操作)。
?boolean contains (Object?o) ?
??????????如果此 collection 包含指定的元素,則返回? true 。
?boolean containsAll (Collection<?>?c) ?
??????????如果此 collection 包含指定 collection 中的所有元素,則返回? true
?boolean isEmpty () ?
??????????如果此 collection 不包含元素,則返回? true 。
abstract ?Iterator<E> iterator () ?
??????????返回在此 collection 中的元素上進行迭代的迭代器。
?boolean remove (Object?o) ?
??????????從此 collection 中移除指定元素的單個實例,如果存在的話(可選操作)。
?boolean removeAll (Collection<?>?c) ?
??????????移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。
?boolean retainAll (Collection<?>?c) ?
??????????僅保留此 collection 中那些也包含在指定 collection 的元素(可選操作)。
abstract ?int size () ?
??????????返回此 collection 中的元素數。
?Object[] toArray () ?
??????????返回包含此 collection 中所有元素的數組。
<T> T[]
toArray (T[]?a) ?
??????????返回包含此 collection 中所有元素的數組;返回數組的運行時類型與指定數組的運行時類型相同。
?String toString () ?
??????????返回此 collection 的字符串表示形式。

?

4.添加元素相關方法源碼

?

      	// 確保此 collection 包含指定的元素
	public boolean add(E e) {
		throw new UnsupportedOperationException();
	}

	// 將指定 collection 中的所有元素都添加到此 collection 中
	public boolean addAll(Collection<? extends E> c) {
		boolean modified = false;
		Iterator<? extends E> e = c.iterator();
		while (e.hasNext()) {
			if (add(e.next()))
				modified = true;
		}
		return modified;
	}
    

?

?

5.移除元素相關方法源碼

?

      	// 從此 collection 中移除指定元素的單個實例,如果存在的話
	public boolean remove(Object o) {
		Iterator<E> e = iterator();
		if (o == null) {
			while (e.hasNext()) {
				if (e.next() == null) {
					e.remove();
					return true;
				}
			}
		} else {
			while (e.hasNext()) {
				if (o.equals(e.next())) {
					e.remove();
					return true;
				}
			}
		}
		return false;
	}

	// 移除此 collection 中那些也包含在指定 collection 中的所有元素
	public boolean removeAll(Collection<?> c) {
		boolean modified = false;
		Iterator<?> e = iterator();
		while (e.hasNext()) {
			if (c.contains(e.next())) {
				e.remove();
				modified = true;
			}
		}
		return modified;
	}

	// 僅保留此 collection 中那些也包含在指定 collection 的元素
	public boolean retainAll(Collection<?> c) {
		boolean modified = false;
		Iterator<E> e = iterator();
		while (e.hasNext()) {
			if (!c.contains(e.next())) {
				e.remove();
				modified = true;
			}
		}
		return modified;
	}

	// 移除此 collection 中的所有元素
	public void clear() {
		Iterator<E> e = iterator();
		while (e.hasNext()) {
			e.next();
			e.remove();
		}
	}
    

?

?

6.查找相關方法源代碼

?

      	// 如果此 collection 包含指定的元素,則返回 true
	public boolean contains(Object o) {
		Iterator<E> e = iterator();
		if (o == null) {
			while (e.hasNext())
				if (e.next() == null)
					return true;
		} else {
			while (e.hasNext())
				if (o.equals(e.next()))
					return true;
		}
		return false;
	}

	// 如果此 collection 包含指定 collection 中的所有元素,則返回 true
	public boolean containsAll(Collection<?> c) {
		Iterator<?> e = c.iterator();
		while (e.hasNext())
			if (!contains(e.next()))
				return false;
		return true;
	}

	// 返回此 collection 中的元素數
	public abstract int size();

	// 如果此 collection 不包含元素,則返回 true
	public boolean isEmpty() {
		return size() == 0;
	}
    

?

?

7.轉換相關方法源代碼

?

      	// 返回在此 collection 中的元素上進行迭代的迭代器
	public abstract Iterator<E> iterator();

	// 返回包含此 collection 中所有元素的數組
	public Object[] toArray() {
		// Estimate size of array; be prepared to see more or fewer elements
		Object[] r = new Object[size()];
		Iterator<E> it = iterator();
		for (int i = 0; i < r.length; i++) {
			if (!it.hasNext()) // fewer elements than expected
				return Arrays.copyOf(r, i);
			r[i] = it.next();
		}
		return it.hasNext() ? finishToArray(r, it) : r;
	}

	// 返回包含此 collection 中所有元素的數組;返回數組的運行時類型與指定數組的運行時類型相同
	public <T> T[] toArray(T[] a) {
		// Estimate size of array; be prepared to see more or fewer elements
		int size = size();
		T[] r = a.length >= size ? a : (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
		Iterator<E> it = iterator();

		for (int i = 0; i < r.length; i++) {
			if (!it.hasNext()) { // fewer elements than expected
				if (a != r)
					return Arrays.copyOf(r, i);
				r[i] = null; // null-terminate
				return r;
			}
			r[i] = (T) it.next();
		}
		return it.hasNext() ? finishToArray(r, it) : r;
	}

	//
	private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
		int i = r.length;
		while (it.hasNext()) {
			int cap = r.length;
			if (i == cap) {
				int newCap = ((cap / 2) + 1) * 3;
				if (newCap <= cap) { // integer overflow
					if (cap == Integer.MAX_VALUE)
						throw new OutOfMemoryError("Required array size too large");
					newCap = Integer.MAX_VALUE;
				}
				r = Arrays.copyOf(r, newCap);
			}
			r[i++] = (T) it.next();
		}
		// trim if overallocated
		return (i == r.length) ? r : Arrays.copyOf(r, i);
	}

	// 返回此 collection 的字符串表示形式
	public String toString() {
		Iterator<E> i = iterator();
		if (!i.hasNext())
			return "[]";

		StringBuilder sb = new StringBuilder();
		sb.append('[');
		for (;;) {
			E e = i.next();
			sb.append(e == this ? "(this Collection)" : e);
			if (!i.hasNext())
				return sb.append(']').toString();
			sb.append(", ");
		}
	}
    

?

?

Java集合2:類 AbstractCollection及源碼


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久精品人人做人人 | 欧美激情精品久久久久 | 免费观看h片 | 天天影院在线观看 | 天天操天天操天天操香蕉 | 国产精品国产三级国产播12软件 | JLZZJLZZ亚洲乱熟在线播放 | 黄色片免费在线播放 | 五月天激情视频在线观看 | 亚洲w码 | 婷婷综合久久狠狠色99h | 亚洲看 | 亚洲日韩中文字幕天堂不卡 | 亚洲精品一区二区三区在线看 | 国产午夜精品理论片影院 | 久久福利青草精品免费 | 国产精品毛片在线 | 一区二区日韩 | 午夜激情视频 | 国产精品久久婷婷六月丁香 | 亚洲午夜在线 | 日韩精品成人 | 日韩三级网| 麻豆精品一区二区 | 久久国产天堂福利天堂 | 国产欧美精品一区二区三区 | 欧美欧美欧美欧美 | 久草在线高清全免费 | 成人97在线观看免费高清 | 国产精品冒白浆免费视频 | 久久精品国产一区二区三区不卡 | 亚洲国产一区二区三区四区色欲 | 国产亚洲视频在线 | 一级毛片 在线播放 | 天天碰天天操 | 精品啪啪| 天天操夜夜噜 | 午夜在线免费视频 | 九色在线观看 | 亚洲国产日韩a在线亚洲 | 午夜丰满少妇高清毛片1000部 |