Java ArrayList 帮助!
我正在学习一点java,我在谷歌图书上的一本java教科书上发现了这个问题,我已经研究了一段时间了,出于某种原因,这些看起来应该很简单。有人感到无聊并想向我展示这在 Java 代码中应该是什么样子吗?
(Using ArrayList) Write a program that creates an ArrayList, adds a Loan
object, a Date object, a string, a JFrame object, and a Circle object to the list,
and uses a loop to display all the elements in the list by invoking the object’s
toString() method.
I am working on learning java a little, and i found this question in a java text book on Google books, I have been working on it for a while, and for some reason these seems like it should be simple. Anyone bored and would like to show me what this is suppose to look like in Java code??
(Using ArrayList) Write a program that creates an ArrayList, adds a Loan
object, a Date object, a string, a JFrame object, and a Circle object to the list,
and uses a loop to display all the elements in the list by invoking the object’s
toString() method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此代码假设所讨论的各种对象具有无参数构造函数。否则只需适当地粘贴参数即可:
This code assumes that the various objects in question have no-parameter constructors. Else just stick the parameters in appropriately:
在不给您确切代码的情况下(您正在尝试学习 Java,对吧?),本练习的目标是向您展示 Java 中的每种类型的对象都从根基类 Object 扩展而来。您可以对任何 Object 实例执行某些操作,无论它的具体实现是什么(例如 toString())。
此外,该练习还向您介绍 Collections API 以及如何构建异构对象的集合。花一点时间查看Collections API 文档。
Without giving you the exact code (you are trying to learn Java, right?) the goal of the exercise is to show you that every type of object in Java extends from the root base class Object. There are certain things that you can do on any instance of Object, no matter what it's concrete implementation (such as toString() for instance).
Additionally the exercise is also teaching you about the Collections API and how you can build collections of heterogeneous objects. Spend a little time looking at the Collections API documentation.
好吧,Java 库中不存在
Loan
和Circle
类,您必须定义自己的类。所有其他的都位于不同的包中,例如 java.util 或 javax.swing。如果 Eclipse 没有自动为我执行导入,我会使用 Google 来查找导入语句所需的包名称。完成后,您可以使用
new
实例化所有这些。首先创建一个 ArrayList,然后执行myList.add(new JFrame()) 之类的操作
将其他对象添加到列表中。
然后,使用
for
循环遍历列表并使用System.out.println
输出元素。Well, the
Loan
andCircle
classes don't exist in the Java library, you'll have to define your own. All the others live in various packages such asjava.util
orjavax.swing
. If Eclipse didn't automatically do theimport
s for me, I'd use Google to find the package names I need for the import statements.That done, you can instantiate all of them using
new
. First you create anArrayList
, then you do something likemyList.add(new JFrame())
to add those other objects to the list.
Then you use a
for
loop to run through the list and output the elements usingSystem.out.println
.