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

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

问题描述

我的教科书几乎没有谈到Java中的枚举,我看过的视频并没有解释太多。所以从我的理解,枚举就像一个完全不同的类
你可以存储常量。有人可以扩展到我的常量,也许
显示我更好的例子?就像我明白,在看到例子颜色,方向的例子
之后,在以前的视频中,人们是什么常数,而在学年期间的枚举
版本的一个我的项目,这是命令话。但我不是100%
了解概念或使用方法。


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


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



解决方案

你在几个不同的类之间感到困惑。 b
$ b


  1. 枚举

  2. 枚举

  3. 迭代器

一个枚举是一个枚举常量,即一个可以采用若干定义值的常量,例如

  public enum Gender {
MALE,
FEMALE;
}

它旨在提供类型安全。



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



一个迭代器是为了使用迭代器而不是为了使用 Iterator 一个集合也许我的回答这里将有所帮助。



对于名字(String description,int age)的格式的枚举,这是他定义他的构造函数的方式,他已经得到并设置了方法 。这是一个大的no-no。



一个枚举应该是一个常量,所以不应该有 setter 方法。一个枚举是一组定义的值,就像我上面的例子一样。



如果你想要一个收藏然后一个个人集合< Person> 中的类将是正确的解决方案。



所以,总结一下。对于常量值使用枚举使用集合,以及收集的东西。不要使用枚举 - 忘记它存在。


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

  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. 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. enum
  2. Enumeration
  3. Iterator

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

public enum Gender {
    MALE,
    FEMALE;
}

It is designed to provide type safety.

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

An Iterator is an implementation of the Iterator Pattern as described by the Gang of Four.

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

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.

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.

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

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天全站免登陆