我想阅读带有对象的 ArrayList 的示例。(Java)

发布于 2024-12-06 22:08:52 字数 555 浏览 0 评论 0原文

我创建了两个类,我试图调用一个构造函数(带参数)和一个方法。我发现如果我只使用一个对象就很容易。

我的目标:

  1. 使用 3 个对象调用相同的方法。
  2. 使用ArrayList调用该方法3次。

我的作业: 我谷歌了一下。我碰巧以对 ArrayList 的解释及其某些示例作为结束。我没有找到我认为需要的示例,即将 ArrayList 与对象一起使用(如我的引文)。

public class DrawGraphics
{
    BouncingBox box;

    /** Initializes this class for drawing. */
    public DrawGraphics()
    {

            box = new BouncingBox(200, 50, Color.green);

            box.setMovementVector(1, 1);
    }
//..................
//................
}

感谢那些试图提供帮助的人。

I have created two classes and I am trying to invoke a constructor(with argument) and a method. I find it easy if I just use an object.

My Goal :

  1. To invoke the same method by using 3 objects.
  2. Use ArrayList to invoke the method 3 times.

My Homework:
I did google a bit. I happen to end with explanation of ArrayList and certain examples of it. I did not find examples which I think I need i.e. using ArrayList with Objects(like my citation).

public class DrawGraphics
{
    BouncingBox box;

    /** Initializes this class for drawing. */
    public DrawGraphics()
    {

            box = new BouncingBox(200, 50, Color.green);

            box.setMovementVector(1, 1);
    }
//..................
//................
}

Thank you for those who try to help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

暗喜 2024-12-13 22:08:52

继续您的示例,这可能会让您了解列表的用途:

// Let's create an ArrayList that will contain the bouncingboxes
List<BouncingBox> boxList = new ArrayList<BouncingBox>();    

// Let's create 5 of them and add them to the end of the List
for (int ii=0;ii<5;ii++) {
    boxList.add(new BouncingBox(200, 50, Color.green));
}

// Iterate over the List we just created with the enhanced for - the method will
// be called on all objects in the List.
for (BouncingBox box : boxList) {
    box.setMovementVector(1, 1);
}

这是您正在寻找的吗?

Continuing with your example this might give you an idea of what Lists can be used for:

// Let's create an ArrayList that will contain the bouncingboxes
List<BouncingBox> boxList = new ArrayList<BouncingBox>();    

// Let's create 5 of them and add them to the end of the List
for (int ii=0;ii<5;ii++) {
    boxList.add(new BouncingBox(200, 50, Color.green));
}

// Iterate over the List we just created with the enhanced for - the method will
// be called on all objects in the List.
for (BouncingBox box : boxList) {
    box.setMovementVector(1, 1);
}

Is this what you were looking for?

烟雨凡馨 2024-12-13 22:08:52

我确信这对原始发帖人没有帮助,除非他们接管课程,但是:

此作业的详细信息可在 MIT“开放课程软件”上找到,作业的目标是通过修改提供的代码(一个数组列表)来创建 3 个不同的对象不需要,就此而言不需要数组,您只需通过添加类似的代码即可创建附加框,

即:

box = new BouncingBox(200, 50, Color.green);
box.setMovementVector(1, 1);

box2 = new BouncingBox(100, 100, Color.cyan);
box2.setMovementVector(2,-1);

等等...

同一类中的第二个函数也需要修改,它将看起来像这样:

   public void draw(Graphics surface) {
        surface.drawLine(50, 50, 250, 250);
        box.draw(surface);
        box2.draw(surface);
        box3.draw(surface);
    }

注意如果你真的对编程感兴趣或爱好,但不明白你在课堂上做什么,最好寻求帮助。简单地在网上复制答案并不能拯救你在现实世界中的处境。

I'm sure this wont help the original poster unless they are taking the class over but:

The details of this assignment are available on MIT "opencourseware" the goal of the assignment is to create 3 different objects by modifying provided code, an array list is not needed, an array is not needed for that matter, you simple create additional boxes by adding similar code

ie:

box = new BouncingBox(200, 50, Color.green);
box.setMovementVector(1, 1);

box2 = new BouncingBox(100, 100, Color.cyan);
box2.setMovementVector(2,-1);

etc...

the second function in that same class needs to be modified as well, it will look something like this:

   public void draw(Graphics surface) {
        surface.drawLine(50, 50, 250, 250);
        box.draw(surface);
        box2.draw(surface);
        box3.draw(surface);
    }

note that if you're actually interested in programming as a career or hobby, but don't understand what you're doing in class, it's a good idea to ask for help. Simply copying the answers online will not save you in the real world.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文