我可以请人验证我的 SCJP 考试收藏吗

发布于 2024-11-27 13:19:51 字数 1433 浏览 3 评论 0原文

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

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

发布评论

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

评论(2

残龙傲雪 2024-12-04 13:19:51

对于初学者,您应该重构您的代码。基本上,无论你在哪里使用“复制粘贴”,都不要这样做。

创建一个像这样的方法:

private static void fill(Collection c) {
    MyItem Eight = new MyItem("Eight");
    c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three"));
    c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine"));
    c.remove(3); c.add(new MyItem("Seven"));
    System.out.println(c);//output?
}

然后,不要使用您拥有的方法,而是执行以下操作:

class MyVector{
    public static void main(String[] args){
        Vector c = new Vector();
        fill(c);
    }
}

并对您拥有的所有集合执行此操作。

接下来,对你的地图做类似的事情:

private static void fill(Map<?,?> map) {
    MyItem Eight = new MyItem("Eight");
    map.put(5, new MyItem("Five")); map.put(1, new MyItem("One")); map.put(8, Eight); map.put(3, new MyItem("Three"));
    map.put(4, new MyItem("Four")); map.put(1, new MyItem("1")); map.put(8, Eight); map.put(9, new MyItem("Nine"));
    map.remove(3); map.put(7, new MyItem("Seven"));
    System.out.println(map);//output?
}

你的代码将缩小,变得可读,甚至有一天变得可用。

For starters, you should refactor your code. Basically, everywhere you used "copy paste", don't.

Create a method like this:

private static void fill(Collection c) {
    MyItem Eight = new MyItem("Eight");
    c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three"));
    c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine"));
    c.remove(3); c.add(new MyItem("Seven"));
    System.out.println(c);//output?
}

Then instead of the method you have, do this:

class MyVector{
    public static void main(String[] args){
        Vector c = new Vector();
        fill(c);
    }
}

And do that for all the collections you have.

Next, do a similar thing for your maps:

private static void fill(Map<?,?> map) {
    MyItem Eight = new MyItem("Eight");
    map.put(5, new MyItem("Five")); map.put(1, new MyItem("One")); map.put(8, Eight); map.put(3, new MyItem("Three"));
    map.put(4, new MyItem("Four")); map.put(1, new MyItem("1")); map.put(8, Eight); map.put(9, new MyItem("Nine"));
    map.remove(3); map.put(7, new MyItem("Seven"));
    System.out.println(map);//output?
}

Your code will shrink, be readable and may even one day become usable.

云裳 2024-12-04 13:19:51

不是与您的实现有关的答案,而是有助于记住课程。我尝试将此作为评论,但缺乏格式确实很糟糕。

我会将它们分成几类以记住它们并理解其实现。

我是这样看待他们的:
列表、地图、集合:这些指示您如何访问数据
Linked、Hash、Tree、Array 和 Heap:内部存储结构

以及两个同步遗留类:
Hashtable:遗留同步哈希图
Vector:传统的同步ArrayList

列表是一组有序的值
集合是一组无序的唯一值
映射是键->值映射(通常称为字典)。

数组是按顺序保存整个列表的一大块内存。移动/删除中间项的成本很高,并且要扩展它,您通常必须复制它(并非总是如此)。最小的结构。

链接是每个元素都包含指向下一个元素的指针。这是昂贵且尴尬的,但如果您需要在中间插入或删除元素,它是最快的!一些排序算法在这种结构上效果更好。它是有序的,但引用特定索引的成本很高。

哈希是获取每个元素并为其计算唯一编号(哈希)的地方,然后您可以使用这些哈希来快速查找元素。本质上是未排序的,查找值的速度非常快,特别是如果您的键不是数字或有序的。这是一个很好的结构,有很多用途。

LinkedHash:结合两者(从字面上看,链接和哈希结构都在 LinkedHashMap 中使用)LinkedList 使其有序,哈希使其快速查找!内存开销加倍,创建时间变慢。

树:每当添加新条目时都会固有排序。我认为这些是唯一本质上排序的集合。它通过保留内部树进行排序,并且只需进行另一个结构的比较次数的一小部分即可将其按正确的顺序插入。

PriorityQueue(Heap):堆是一个非常酷的结构——基本上,通过最少的处理,它总是会非常快地给你它的最小/最大值(就像直接读取一样),并且添加也非常快。

抱歉,答案太长且间接,但我认为这对阅读该主题的人很有用。

Not an answer to do with your implementation, rather help on memorizing the classes. I tried this as a comment but lack of formatting really hurt.

I would break them into categories to remember them, and understand the implementations.

I think of them this way:
List, Map, Set: These indicate how you can access your data
Linked, Hash, Tree, Array and Heap: The internal storage structure

And the two synchronized legacy classes:
Hashtable: legacy synchronized hashmap
Vector: legacy synchronized ArrayList

A list is an ordered group of values
a set is an unordered group of unique values
a map is a key->value mapping (Often called Dictionary)

Array is a large block of memory holding the entire list in order. Moving/deleting a middle item is expensive and to expand it you often have to copy it (Not always). Smallest structure.

Linked is where each element contains a pointer to the next. This is expensive and awkward, but if you need to insert or delete an element in the midle it's the fastest! Some sorting algorithms work better on this kind of structure. It's ordered but Referencing a specific index is expensive.

Hash is where you take each element and calculate a unique number (hash) for it, then you can use those hashes to very quickly look up an element. Inherently unsorted, very fast to look up a value, especially if your keys are not numeric or ordered. This is a good structure, lots of uses.

LinkedHash: Combines both (Literally, both link and hash structures are used inside a LinkedHashMap) The LinkedList makes it ordered, the hash makes it quick to look up! Double the memory overhead and slower creation times.

Tree: Inherently sorted whenever you add a new entry. I think these are the only inherently sorted collections. It sorts by keeping an internal tree and only having to make a fraction of the number of comparisons of another structure to insert it in the correct order.

PriorityQueue(Heap): A heap is a really cool structure--Basically with minimal resorting it will always give you it's smallest/largest value extremely quickly (Like just a straight read) and adding is also extremely fast.

Sorry about the too long and indirect answer, but I think it would be useful to people reading this topic.

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