0

我在这里阅读了很多 SCJP 问题,以及来自 Sun 和 Head First 出版商的所有提示和技巧,我想知道我是否遗漏了什么。

我无法从有关 Java 组件的知识转变为能够回答应用该知识的问题。如果您问我有关语言或 API 的具体问题,我可以回答。但是,当我希望应用这些知识来回答模拟编码问题时,我需要很长时间才能处理这个问题,而且我很难将这些问题联系起来。就像没有点击一样。是否有一个过程可以让我根据我所知道的更好地得出关于问题的结论?

4

4 回答 4

4

很简单:你用它。

这听起来可能很陈词滥调,但在编程中学习新东西不能替代尝试用它做点什么。就像写代码一样。

如果它真的是新的,你可以从一个现有的程序开始,然后修改它来做你想做的事情。这通常会破坏它,您将在接下来的 2 小时内找出原因。在那 2 小时内,您将学到更多关于程序结构的基础知识(在该语言/框架中)、如何将程序组合在一起等等,而不是从书中阅读它的 5 倍。

并不是说我说书一文不值:远非如此。但编程归根结底是一门实用的学科。

于 2009-06-22T13:39:57.710 回答
1

I'm a pretty good programmer. I have read through half of the SCJP book and when I took the test at JavaOne (for free, thankfully) I only scored a 60% on it.

What I learned from taking this exam is that they are going to try to trick you as much as possible. The only way to combat this is to start trying to code silly things and see what happens.

class Foo {
     public void doSomething() { System.out.println("Foo"); }
}
class MyFoo extends Foo {
     public void doSomething() { System.out.println("MyFoo"); }
}
public class MyClass extends MyFoo {
     Foo f;
     static {
        f = new MyFoo().doSomething();
     }
     public static void main(String args[]) {
        new MyClass();
     }
}

Create stupid examples like the one above and see what prints out. Then try playing around with Sets, TreeSets and all other kinds of Sets. Then do the same with Maps and all of the subsets of Maps. I came up with the following class that was taken from snipits from others playing with sets.

public class PlayingWithSets {
private Set<Integer> s1;
private Set<Integer> s2;

enum Test {
    A,B,C

};
/*
 * Cannot be a static block because the variable is not static. This block
 * happens before the call to super() in the constructor.
 */
{
    s1 = generateSet();
    s2 = generateSet();
}

/**
 * Helper method to set up a new HashSet
 * @return
 */
private Set<Integer> generateSet() {
    Set<Integer> s = new HashSet<Integer>();
    Random r = new Random();
    for (int i = 0; i < 20; ++i) {
        s.add(r.nextInt(30));
    }
    return s;
}

/* **********************  Merges two sets *****************************/
private void mergeSets() {
    System.out.println("Set s1 = " + s1);
    System.out.println("Set s2 = " + s2);

    /*
     * Causes an error if you use the wildcard for the generic type. I.E.
     * Set<?> s1; in the declaration.
     * 
     * The cast is needed when using wild cards for declaration. The reason
     * is that you cannot gurantee that the object is of the same type,
     * which defeats the purpose of generics and type safety.
     */
    s1.addAll(s2);
    System.out.println(s1);
}

/* ************************  Sorting on a set  ***************************/
private void sortSets() {
    /*
     * Collections.sort() is ONLY for lists.
     */
    // Collections.sort(s1);

    TreeSet<Integer> ts = new TreeSet<Integer>(s1);
    System.out.println("Sorted set s1 = " + ts);
}

/* ********************  Tests the Uniqueness of sets (i.e. no duplicates)    **************************/

static void fill(Set s) {
    s.addAll(Arrays.asList("one two three four five six seven".split(" ")));
}

public static void testSetUniqueness(Set s) {
    // Strip qualifiers from class name:
    System.out.println(s.getClass().getName().replaceAll("\\w+\\.", ""));
    fill(s);
    fill(s);
    fill(s);
    System.out.println(s); // No duplicates!
    // Add another set to this one:
    s.addAll(s);
    s.add("one");
    s.add("one");
    s.add("one");
    System.out.println(s);
    // Look something up:
    System.out.println("s.contains(\"one\"): " + s.contains("one"));
}

/* ******************  Subset / Union / Intersection **********************/

public static <T> Set<T> union(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    tmp.addAll(setB);
    return tmp;
}

public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>();
    for (T x : setA)
        if (setB.contains(x))
            tmp.add(x);
    return tmp;
}

public static <T> Set<T> difference(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    tmp.removeAll(setB);
    return tmp;
}

public static <T> Set<T> symDifference(Set<T> setA, Set<T> setB) {
    Set<T> tmpA;
    Set<T> tmpB;

    tmpA = union(setA, setB);
    tmpB = intersection(setA, setB);
    return difference(tmpA, tmpB);
}

public static <T> boolean isSubset(Set<T> setA, Set<T> setB) {
    return setB.containsAll(setA);
}

public static <T> boolean isSuperset(Set<T> setA, Set<T> setB) {
    return setA.containsAll(setB);
}

private void subsetUnionIntersection() {
    TreeSet<Character> set1 = new TreeSet<Character>();
    TreeSet<Character> set2 = new TreeSet<Character>();

    set1.add('A');
    set1.add('B');
    set1.add('C');
    set1.add('D');

    set2.add('C');
    set2.add('D');
    set2.add('E');
    set2.add('F');

    System.out.println("set1: " + set1);
    System.out.println("set2: " + set2);

    System.out.println("Union: " + union(set1, set2));
    System.out.println("Intersection: " + intersection(set1, set2));
    System.out.println("Difference (set1 - set2): "
            + difference(set1, set2));
    System.out
            .println("Symmetric Difference: " + symDifference(set1, set2));

    TreeSet<Character> set3 = new TreeSet<Character>(set1);

    set3.remove('D');
    System.out.println("set3: " + set3);

    System.out.println("Is set1 a subset of set2? " + isSubset(set1, set3));
    System.out.println("Is set1 a superset of set2? "
            + isSuperset(set1, set3));
    System.out.println("Is set3 a subset of set1? " + isSubset(set3, set1));
    System.out.println("Is set3 a superset of set1? "
            + isSuperset(set3, set1));

}

/* ************************         ***************************/



/**
 * @param args
 */
public static void main(String[] args) {
    /*
     * Testing different types of sets
     */
    testSetUniqueness(new HashSet());
    testSetUniqueness(new TreeSet());
    testSetUniqueness(new LinkedHashSet());

    Test test = new Test();
    Test values[] = test.values();
    System.out.println("\nValues: " + values);
    for(Test t: values) {
        System.out.println("T: " + t.toString());
    }

    PlayingWithSets p = new PlayingWithSets();
    p.mergeSets();
    p.sortSets();
    p.subsetUnionIntersection();
}
于 2009-06-22T14:26:12.937 回答
1

遇到一个新概念,想出一个用例,实际写一些代码。

例如,如果我们了解Runnables 以及如何使用它们来制作新Thread的 s,请实际编写一些代码并尝试一下:

Thread t = new Thread(new Runnable() {
    public void run() {
        // Do something.
    }
});
t.start();

在学习新事物时,实际上没有什么可以替代实际尝试。

实际上,我相信通过 Stack Overflow 上的问题并尝试回答这些问题将是尝试应用所学概念的好方法。即使一个人实际上并没有发布答案,通过编写答案的过程本身也可以重新强化学到的概念。

于 2009-06-22T13:41:01.280 回答
0

真的,我认为这正是经验的来源。你必须坐下来编写一些程序来感受事物是如何真正融合在一起的。

于 2009-06-22T13:41:35.413 回答