繁体   English   中英

Java-泛型类型的向量的多维数组

[英]Java - multidimensional array of vectors of generic type

想象一下,如果可以的话,想象一个由1x1x1砖块组成的10x10x10立方体。 每个积木必须可以通过x,y,z坐标访问。 对于每个积木,我还需要存储拥有“砖块”的人的姓名列表。

由于效率是绝对必要的,因此我提出了以下想法-向量的3d数组。 注意-我做了一个存储名称和其他信息的类(称为人员)

//declaration
    protected Vector<person>[][][] position;

我认为然后必须将内存分配给指针位置 我已经试过了

position = new Vector<person>[10][10][10]; 

但是遇到错误“无法创建向量的通用数组”,我只熟悉C ++,而Java对我来说是新的。 我了解Java不喜欢用泛型声明数组吗? 有谁知道我如何解决这个问题?

干杯

不需要那么复杂! 您知道数组的大小(10:10:10),因此不需要为砖块准备矢量或其他东西。 尝试使用对象数组:

Class Brick {
 public Brick(int x, int y, int z){

   this.x=x;
   this.y=y;
   this.z=z;

   owners = new ArrayList <String> ();
 }

 List<String> owners;

 int x, y, z;  //every brick "knows" its position - you might not need it

}

用于创建数组的代码:

Public Class Main {
  .....

 Brick Cube[][][] = new Brick[10][10][10];
 for (int x=0; x < 10; x++)
   for(int y=0; y < 10; y++)
     for(int z=0; z < 10; z++)
     {
       Cube[x][y][z] = new Brick(x, y, z);
     }

//adding an owner to a brick:
 Cube[0][0][0].owners.add("Owner");
.....
}

牢记OOP-这使事情变得容易得多!

TODO:添加获取器/设置器

如果您确实想使用List结构而不是数组,那么这足以让您入门。 这是基于@FrustratedWithFormsDes所说的,但是我包括了初始化代码,因为很难正确地使用语法。

public class Person {
}

class PeopleStorage {

    ArrayList<ArrayList<ArrayList<Person>>> data;

    public PeopleStorage(int size) {

    this.data = new ArrayList<ArrayList<ArrayList<Person>>>(size);

    for (int i = 0; i < size; i++) {
        ArrayList<ArrayList<Person>> inner = new ArrayList<ArrayList<Person>>(
            size);
        data.add(inner);
        for (int j = 0; j < size; j++) {
        ArrayList<Person> inner2 = new ArrayList<Person>(size);
        inner.add(inner2);
        for (int k = 0; k < size; k++) {
            inner2.add(new Person());
        }
        }
    }
    }

     public Person get(int index1, int index2, int index3)
     {
      //check indices against size, throw IllegalArgumentException here
     return data.get(index1).get(index2).get(index3);
     }

     public void set(Person person, int index1, int index2, int index3)
     {
     //check indices against size, throw IllegalArgumentException here
     data.get(index1).get(index2).set(index3, person);
     }
}

我认为您将要使用ArrayList而不是向量。 我对此有些生疏,但据我回忆,将数组和通用容器混合并不是一个好主意。

您可以尝试以下方法:

position = new ArrayList(10)<ArrayList(10)<ArrayList(10)<Person>>; 

但是阅读起来很丑。 访问数据是这样的:

person = position.get(1).get(3).get(6); //get someone at x=1, y=3, z=6

请注意,我现在没有机会编译它并查看它是否确实有效,因此您将需要阅读ArrayList上的文档,但是我认为这是您可以使用的方向...


更新:jhominal指出了一些我忘记的new ,我手头没有Java编译器来验证这一点,但是如果您尝试此解决方案,请也看一下他说的话。

您尝试做的事情在Java中是不可能的,因为没有运算符重载(例如在C ++中)。 简而言之,Java中的[]运算符仅为数组定义,不能重载。 因此,访问Vector / ArrayList中的元素的唯一方法是通过get()方法(或通过Iterator或其他方法,但是您会得到图片)。 类似地,您不能以这种方式定义多维ArrayList。 您应该执行的操作是ArrayList<ArrayList<ArrayList<Person>>> people = new ArrayList<ArrayList<ArrayList<Person>>>() ,然后使用嵌套循环或您希望的任何方式初始化所有内部数组。 它看起来有点丑陋,而且由于ArrayList / Vector是一个动态集合,例如在第一个数组上添加新元素,还需要您初始化两个嵌套数组。 因此,从设计角度来讲,为该类编写某种形式的包装器以隔离其中的逻辑可能会更好。

Java中ArrayList和Vector之间的区别在于,vector是同步的(因此速度较慢),并且是默认的增长方式(Vector的大小加倍,而ArrayList增长50%)。 否则,它们在功能和操作复杂性上几乎是相同的。

通过链接,它可能会回答有关通用数组类型的问题。

注意:这更多是为了解释为什么泛型数组在Java中不起作用,而不是您实际问题的答案。

有效的Java 2nd Edition处理通用数组在第5章119页( PDF )。

为什么创建通用数组是非法的? 因为它不是类型安全的。 如果合法,则编译器在其他正确程序中生成的强制转换在运行时可能会失败,并带有ClassCastException 这将违反通用类型系统提供的基本保证。

这是因为Java泛型仅在编译时具有类型,并插入适当的强制转换,因此特定操作在运行时起作用。

此问题最简单的解决方案可能是Ed.C提供的解决方案。

我也可以使用Ed.C的对象答案,但这似乎可行:

@SuppressWarnings("unchecked")
protected Vector<Person>[][][] position = new Vector[10][10][10];

即,您跳过右侧的菱形。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM