标题:Collection中的小问题
只看楼主
heilong
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:777
专家分:0
注 册:2007-6-7
 问题点数:0 回复次数:3 
Collection中的小问题

import java.util.*;

public class Collection1 {
public static Collection
fill(Collection c, int start, int size) {
for(int i = start; i < start + size; i++)
c.add(Integer.toString(i));
return c;
}
public static Collection
fill(Collection c, int size) {
return fill(c, 0, size);
}
public static Collection fill(Collection c) {
return fill(c, 0, 10);
}
public static Collection newCollection() {
return fill(new ArrayList());
}
public static Collection
newCollection(int start, int size) {
return fill(new ArrayList(), start, size);
}
public static void print(Collection c) {
for(Iterator x = c.iterator(); x.hasNext();)
System.out.print(x.next() + " ");
System.out.println();
}
public static void main(String[] args) {
Collection c = newCollection();
c.add("ten");
c.add("eleven");
print(c);
Object[] array = c.toArray();
// Make a String array from the List:
String[] str =
(String[])c.toArray(new String[1]);
// Find max and min elements; this means
// different things depending on the way
// the Comparable interface is implemented:
System.out.println("Collections.max(c) = " + //我想问下,这里为什么是ten?
Collections.max(c));

System.out.println("Collections.min(c) = " +
Collections.min(c));
// Add a Collection to another Collection
c.addAll(newCollection());
print(c);
c.remove("3"); // Removes the first one
print(c);
c.remove("3"); // Removes the second one
print(c);
// Remove all components that are in the
// argument collection:
c.removeAll(newCollection());
print(c);
c.addAll(newCollection());
print(c);
// Is an element in this Collection?
System.out.println(
"c.contains(\"4\") = " + c.contains("4"));
// Is a Collection in this Collection?
System.out.println(
"c.containsAll(newCollection()) = " +
c.containsAll(newCollection()));
Collection c2 = newCollection(5, 3);
// Keep all the elements that are in both
// c and c2 (an intersection of sets):
c.retainAll(c2);
print(c);
// Throw away all the elements in c that
// also appear in c2:
c.removeAll(c2);
System.out.println("c.isEmpty() = " +
c.isEmpty());
c = newCollection();
print(c);
c.clear(); // Remove all elements
System.out.println("after c.clear():");
print(c);
}
}

搜索更多相关主题的帖子: Collection 
2007-07-07 15:20
ynw520
Rank: 1
等 级:新手上路
帖 子:100
专家分:0
注 册:2006-8-24
得分:0 
ten 和eleven
t比e大

走自已的路,让别人去说吧!
2007-07-07 16:29
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 
楼上正解

可惜不是你,陪我到最后
2007-07-07 16:42
heilong
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:777
专家分:0
注 册:2007-6-7
得分:0 

谢谢``````努力`````


风水鸡蛋壳,财去人安乐!----->
2007-07-07 16:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-153479-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.610786 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved