学习 SCJP,以及如何从知识转向知识应用

发布于 2024-07-25 08:11:56 字数 255 浏览 3 评论 0原文

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

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

I've read a lot of the SCJP questions here, and all the tips and tricks from Sun and the Head First publishers, and I wonder if I am missing something.

I'm having trouble moving from knowledge about a component of Java to being able to answer a question that applies that knowledge. If you asked me a specific question about the language or API, I could answer it. However, when I am looking to apply that knowledge to answer the mock coding questions, it takes me forever to process the question, and I have trouble connecting the dots. It's like nothing clicks. Is there a process by which I can better draw conclusions about questions based upon what I know?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美男兮 2024-08-01 08:11:56

很简单:你使用它。

这听起来可能有些陈词滥调,但在编程中学习新东西,没有什么比尝试用它做点事情更重要的了。 就像编写代码一样。

如果它确实是新的,您可能会首先采用现有程序并对其进行修改以执行您想要的操作。 这通常会破坏它,您将在接下来的 2 小时内找出原因。 在这 2 小时内,您将学到更多有关程序结构(在该语言/框架中)的基础知识、如何将程序组合在一起等方面的知识,比从书本上阅读的时间长 5 倍。

我并不是说书籍毫无价值:远非如此。 但编程最终是一门实用的学科。

Simple: you use it.

That may sound trite but there is no substitute for learning something new in programming than trying to do something with it. As in writing code.

If it's really new, you might start by taking an existing program and modifying it to do something you want. This will usually break it and you'll spend the next 2 hours working out why. In that 2 hours you'll have learnt more about the basics of program structure (in that language/framework), how a program is put to together and so on than 5 times that long reading it from a book.

Not that I'm suggesting books are worthless: far from it. But programming is ultimately a pragmatic discipline.

痴情换悲伤 2024-08-01 08:11:56

当遇到新概念时,提出一个用例,并实际编写一些代码。

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

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

当学习新东西时,确实没有什么可以替代实际尝试。

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

When encountering a new concept, come up with a use case, and actually write some code.

For example, if we learn about Runnables and how they can be used to make new Threads, actually write some code and try it out:

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

When learning something new, there's really no substitute for actually trying things out.

Actually, I believe that going through questions on Stack Overflow and trying to answer them would be pretty good way to try to apply learned concepts. Even if one doesn't actually post an answer, going through the process of writing an answer would itself work to re-enforce the learned concept.

失眠症患者 2024-08-01 08:11:56

我是一个相当不错的程序员。 我读完了 SCJP 书的一半,当我参加 JavaOne 测试时(谢天谢地是免费的),我只得到了 60% 的分数。

我从这次考试中学到的是,他们会尽可能地欺骗你。 解决这个问题的唯一方法是开始尝试编写愚蠢的代码,看看会发生什么。

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();
     }
}

创建像上面这样的愚蠢示例,然后看看打印出什么内容。 然后尝试使用 Sets、TreeSets 和所有其他类型的 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();
}

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();
}
行雁书 2024-08-01 08:11:56

确实,我认为这正是经验发挥作用的地方。您必须坐下来编写一些程序,以了解事物如何真正组合在一起。

Really, I think this is just where experience comes in. You have to sit down and write some programs to get a feel for how things really fit together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文