简体   繁体   English

关于Java中的集合,枚举和迭代器的困惑

[英]Confusion about collections, enumeration, and iterator in Java

My textbook barely talks about enumeration in Java, and the videos that I've watched don't explain much. 我的教科书几乎没有谈到Java中的枚举,而我所看过的视频并没有解释太多。 So from what I'm understanding, enumeration is like a whole different class where you can store constants in. Can someone expand to me about the constants and perhaps show me better examples? 所以根据我的理解,枚举就像一个完全不同的类,你可以存储常量。有人可以向我展示常量,也许可以向我展示更好的例子吗? Like I understand what constants are after seeing the examples such as colors, directions, and in the previous videos it was people, while in the enum version of one my projects during the school year, it was command words. 就像我在看到颜色,方向之类的例子之后了解常量是什么,并且在之前的视频中是人,而在学年期间我的项目的枚举版中,它是命令词。 But I don't 100% understand the concept or how to use. 但我不是100%理解这个概念或如何使用。

  1. Also, what's the point of an Enumeration when you can just make a collection? 另外,当你可以制作一个集合时,枚举的重点是什么? Like for instance, the last video i saw, the video maker made enums of people in the format of name(String description, int age), and that's how he defined his constructor and he had get and set methods. 例如,我看到的最后一个视频,视频制作者以name(String description,int age)的格式制作了人物的枚举,这就是他如何定义他的构造函数并且他已经获得并设置了方法。 What's the advantage of doing this rather than just creating a person object in the same exact way and them creating a collection and storing person objects in there? 这样做的好处是什么,而不仅仅是以相同的方式创建一个人物对象,他们创建一个集合并在那里存储人物对象?

  2. I went to look up the above, and after seeing this thread: Difference between Java Enumeration and Iterator An iterator is something that will let me loop through a collection, and all this time I thought enumeration was something like a different class. 我查看了上面的内容,看了这个帖子后: Java Enumeration和Iterator之间的区别迭代器是让我遍历集合的东西,而且这一次我认为枚举就像是一个不同的类。 But in the thread they're comparing them. 但在线程中他们正在比较它们。 Enumeration is just something like an iterator, but without the remove method. 枚举就像迭代器,但没有remove方法。 Is this enumeration something different than what I was talking about above? 这个枚举与我上面谈到的有什么不同吗?

You are confused between several different classes. 您在几个不同的类之间感到困惑。

  1. enum
  2. Enumeration
  3. Iterator

An enum is an enumerated constant , ie a constant that can take several defined values such as enum枚举常量 ,即可以采用多个定义值的常量,例如

public enum Gender {
    MALE,
    FEMALE;
}

It is designed to provide type safety. 它旨在提供类型安全。

An Enumeration is a now deprecated part of the Collection s API - forget about this. EnumerationCollection s API的一个现已弃用的部分 - 忘了这一点。 It is superseded by Iterator . 它被Iterator取代。

An Iterator is an implementation of the Iterator Pattern as described by the Gang of Four. Iterator是四人帮所描述的迭代器模式的实现。

For why to use an Iterator rather than a Collection maybe my answer here will help. 对于为什么要使用一个Iterator ,而不是Collection ,也许我的答案在这里会有所帮助。

As for enums of people in the format of name(String description, int age), and that's how he defined his constructor and he had get and set methods . 对于name(String description,int age)格式的人的枚举,这就是他如何定义他的构造函数,并且他已经获取并设置了方法 This is a big no-no. 这是一个很大的禁忌。

An enum should be a constant so should not have setter methods. enum应该是常量,所以不应该有setter方法。 An enum is a set of defined values like in my example above. enum是一组定义的值,如上例所示。

If you want a Collection of people then a Person class in a Collection<Person> would be the correct solution. 如果你想要一个peopleCollection ,那么Collection<Person>Person类将是正确的解决方案。

So, in summary. 所以,总结一下。 Use an enum for constant values; 使用enum作为常量值; use a Collection for, well, collections of things. 使用Collection来收集东西。 And do not use an Enumeration - forget it exists at all. 并且不要使用Enumeration - 忘记它存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM