×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

在学java, 有一个例子的结果不明白,请教.

本文发表在 rolia.net 枫下论坛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.println(x.next() + " ");
System.out.println();
}
public static void main(String[] args) {
Collection c = newCollection();
c.add("ten");
c.add("eleven");
print(c);
System.out.println("Collections.max(c) = " + Collections.max(c)); // which one is the max?
System.out.println("Collections.min(c) = " + Collections.min(c));
}
}
最大是eleven or ten?更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 在学java, 有一个例子的结果不明白,请教.
    本文发表在 rolia.net 枫下论坛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.println(x.next() + " ");
    System.out.println();
    }
    public static void main(String[] args) {
    Collection c = newCollection();
    c.add("ten");
    c.add("eleven");
    print(c);
    System.out.println("Collections.max(c) = " + Collections.max(c)); // which one is the max?
    System.out.println("Collections.min(c) = " + Collections.min(c));
    }
    }
    最大是eleven or ten?更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • ten. Believe me, I am SCJP and SCWCD. Please read api as following:
      max
      public static Object max(Collection coll)
      Returns the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the collection).
      This method iterates over the entire collection, hence it requires time proportional to the size of the collection.

      Parameters:
      coll - the collection whose maximum element is to be determined.
      Returns:
      the maximum element of the given collection, according to the natural ordering of its elements.
      Throws:
      ClassCastException - if the collection contains elements that are not mutually comparable (for example, strings and integers).
      NoSuchElementException - if the collection is empty.
      See Also:
      Comparable
      • Thank you very much. Actually, after I posted it, I realized max method compars the value not the key. BTW, what's that meanSCJP and SCWCD? 这个贴子太幼稚了,我要删了它。
        • 斑竹帮帮忙,删了它.
        • Sun Certified Java Programmer and web component developer