混淆在Java中的集合,枚举和迭代器 [英] Confusion about collections, enumeration, and iterator in Java

查看:225
本文介绍了混淆在Java中的集合,枚举和迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教科书很少谈论Java中的枚举,我观看的视频没有解释太多。所以从我的理解,枚举就像一个完全不同的类
你可以存储常量。有人可以扩展到我的常量,也许
给我更好的例子?像我理解什么常数是看到的例子
如颜色,方向,在以前的视频中是人,而在一个我的项目在学年期间的枚举
版本,它是命令话。但我不会100%
了解这个概念或如何使用。

My textbook barely talks about enumeration in Java, and the videos that I've watched don't explain much. 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.


  1. 一个枚举当你可以只是做一个集合?像
    实例,我看到的最后一个视频,视频制作器以
    name(String description,int age)的格式枚举人们,这就是他如何定义他的构造函数,他有
    获取和设置方法。这样做的优点是什么,而不只是创建一个人
    对象以相同的方式,他们创建一个集合和存储人物对象在
    有?

  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. 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?

我去查看上面的内容,看到这个线程后:
Java枚举和迭代器之间的区别
迭代器是让我循环遍历一个集合,所有这一次I
认为枚举类似一个不同的类。但在线程中他们是
比较它们。枚举就像一个迭代器,但没有remove
方法。这个枚举和我上面说的不一样吗?

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. But in the thread they're comparing them. Enumeration is just something like an iterator, but without the remove method. Is this enumeration something different than what I was talking about above?


推荐答案

您在几个不同的类别之间感到困惑。

You are confused between several different classes.


  1. 枚举

  2. 枚举

  3. Iterator

  1. enum
  2. Enumeration
  3. Iterator


$ b b

An 枚举是一个枚举常量,即可以接受几个定义的值的常量,例如

An enum is an enumerated constant, i.e. a constant that can take several defined values such as

public enum Gender {
    MALE,
    FEMALE;
}

它旨在提供类型安全。

枚举 Collection 的API现已弃用的部分 - 忘记了这一点。它被 Iterator 取代。

An Enumeration is a now deprecated part of the Collections API - forget about this. It is superseded by Iterator.

一个迭代器

为什么要使用迭代器,而不是a Collection 也许我的回答这里会有所帮助。

For why to use an Iterator rather than a Collection maybe my answer here will help.

对于以name(String description,int age)格式的枚举的人,这就是他如何定义他的构造函数,他有get和set方法 。这是一个很大的不。

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. This is a big no-no.

一个枚举应该是一个常数,所以不应该有 setter 方法。 枚举是一组定义的值,如在上面的示例中。

An enum should be a constant so should not have setter methods. An enum is a set of defined values like in my example above.

如果您想要集合 集合< Person> 中的类将是正确的解决方案。

If you want a Collection of people then a Person class in a Collection<Person> would be the correct solution.

所以,总结。对于常量值使用枚举;使用 Collection 来表示事物的集合。不要使用枚举 - 根本不存在。

So, in summary. Use an enum for constant values; use a Collection for, well, collections of things. And do not use an Enumeration - forget it exists at all.

这篇关于混淆在Java中的集合,枚举和迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆