【问题标题】:how to create a matrix of array lists contains with objects in JAVA (eclipse)?如何在 JAVA (eclipse) 中创建包含对象的数组列表矩阵?
【发布时间】:2017-05-23 05:16:07
【问题描述】:

我需要创建一个代表棋盘的矩阵。 矩阵中的每个地方都需要是某种包含对象(游戏的各种参与者、玩家和其他对象)的列表。 我需要这样做,因为有时两个对象可能位于矩阵的同一位置。 但是eclipse不同意创建一个“通用数组”,所以我不能用所有对象初始化矩阵。我该怎么办?

【问题讨论】:

  • Eclipse 只是一个开发环境,用于以多种不同的编程语言编写程序。你问的是哪种编程语言?向我们展示您的代码。
  • 语言是Java。

标签: java eclipse generics arraylist 2d


【解决方案1】:

你的问题有点含糊。 如果您想要列表方法,您可能想要一个 Actor 列表列表,其中 Actor 是其他类(例如玩家和“其他对象”)的基类(或接口) .然后将列表视为相同位置的演员的列、行和列表。

例子:

List<List<List<Actor>>> columns = new ArrayList<List<List<Actor>>>();
List<List<Actor>>> rows = new ArrayList<List<Actor>>();
List<Actor> actors = new ArrayList<Actor>
//Then pupulate the columns with rows and the rows with lists of actors

我不建议这样做,因为这种方法很快就会失控。
而是创建您自己的图板表示(可能有基础列表或地图)。然后你可能会得到类似的结果:

Board board = new Board(10,10);
Actor whiteQueen = new Queen();
Actor blackKing = new King();
board.addActor(8,2, whiteQueen);
board.addActor(8,2, blackKing);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    相关资源
    最近更新 更多