Java ArrayList 帮助!

发布于 2024-08-12 16:05:28 字数 389 浏览 5 评论 0原文

我正在学习一点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 技术交流群。

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

发布评论

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

评论(5

满栀 2024-08-19 16:05:28

此代码假设所讨论的各种对象具有无参数构造函数。否则只需适当地粘贴参数即可:

ArrayList<Object> list = new ArrayList<Object>();
list.add(new Loan());
list.add(new Date());
list.add(new String());
list.add(new JFrame());
list.ad(new Circle());

for (Object obj : list)
{
    System.out.println(obj.toString());
}

This code assumes that the various objects in question have no-parameter constructors. Else just stick the parameters in appropriately:

ArrayList<Object> list = new ArrayList<Object>();
list.add(new Loan());
list.add(new Date());
list.add(new String());
list.add(new JFrame());
list.ad(new Circle());

for (Object obj : list)
{
    System.out.println(obj.toString());
}
挽清梦 2024-08-19 16:05:28
List<Object> list= new ArrayList<Object>();
list.add("A String");
list.add(new JFrame());
list.add(new YourCircleObject());
(...)
for(Object o:list)
 {
 System.out.println(o.toString());
 }
List<Object> list= new ArrayList<Object>();
list.add("A String");
list.add(new JFrame());
list.add(new YourCircleObject());
(...)
for(Object o:list)
 {
 System.out.println(o.toString());
 }
时常饿 2024-08-19 16:05:28

在不给您确切代码的情况下(您正在尝试学习 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.

独孤求败 2024-08-19 16:05:28

好吧,Java 库中不存在 LoanCircle 类,您必须定义自己的类。所有其他的都位于不同的包中,例如 java.util 或 javax.swing。如果 Eclipse 没有自动为我执行导入,我会使用 Google 来查找导入语句所需的包名称。

完成后,您可以使用 new 实例化所有这些。首先创建一个 ArrayList,然后执行

myList.add(new JFrame()) 之类的操作

将其他对象添加到列表中。

然后,使用 for 循环遍历列表并使用 System.out.println 输出元素。

Well, the Loan and Circle classes don't exist in the Java library, you'll have to define your own. All the others live in various packages such as java.util or javax.swing. If Eclipse didn't automatically do the imports 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 an ArrayList, then you do something like

myList.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 using System.out.println.

涙—继续流 2024-08-19 16:05:28
import java.util.*;

public class Exercise9_6 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Loan());
        list.add(new Date());
        list.add(new javax.swing.JFrame());

        for (int i = 0; i < list.size(); i++)
          System.out.println(list.get(i));
        }
    }
}
import java.util.*;

public class Exercise9_6 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Loan());
        list.add(new Date());
        list.add(new javax.swing.JFrame());

        for (int i = 0; i < list.size(); i++)
          System.out.println(list.get(i));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文