简体   繁体   English

Java:具有类似于ArrayList的方法/功能的Bidimensional Array

[英]Java: Bidimensional Array with methods/capabilities similar to ArrayList

I want to create an XY array of integers (or whatever type), but I want to use methods like "add", "remove", "contains", "indexOf" similar to ArrayList class. 我想创建一个整数的XY数组(或任何类型),但我想使用类似于ArrayList类的“add”,“remove”,“contains”,“indexOf”等方法。

Is there any existing class with these capabilities? 有没有这些功能的现有课程?

PS: I don't want to create an ArrayList of ArrayList PS:我不想创建ArrayList的ArrayList

No, AFAIK there isn't any class like this. 不,AFAIK没有这样的类。 But Implementing one should be fairly easy: 但实现一个应该相当容易:

class BiDimensionalArray<T>{
  Object[][] backupArray;
  int lengthX;
  int lengthY;

  public BiDimensionalArray(int lengthX, int lengthY) {
    backupArray = new Object[lengthX][lengthY];
    this.lengthX = lengthX;
    this.lengthY = lengthY;
  }

  public void set(int x, int y, T value){
    backupArray[x][y] = value;
  }

  public T get(int x, int y){
    return (T) backupArray[x][y];
  }

  public void addX(T[] valuesY) {
    Object[][] newArray = new Object[lengthX+1][lengthY];
    System.arraycopy(backupArray, 0, newArray, 0, lengthX);
    newArray[lengthX]=valuesY;
    backupArray = newArray;
    lengthX = lengthX+1;
  }
}

Note: The Typeparameter isn't used internally, because there is no such thing as new T[][] 注意:Typeparameter不在内部使用,因为没有new T[][]这样的东西

EDITS EDITS
Added addX Method for demonstration 添加了addX方法进行演示
Fixed compile-errors 修复了编译错误

From your description, I would suggest you to try using JAMA. 根据您的描述,我建议您尝试使用JAMA。
You can also create your own implementation for an XY Matrix. 您还可以为XY矩阵创建自己的实现。 However, for doing this, you will have to decide what exactly you want from the implementation. 但是,为此,您必须确定实现的确切要求。
If your Matrix is not of fixed size, then you can use something like the 3-tuple format for storing matrices. 如果您的Matrix不是固定大小,那么您可以使用类似3元组格式的内容来存储矩阵。 (This representation is efficient only if your matrix is sparse). (只有当矩阵稀疏时,此表示才有效)。 Internally, you will use three ArrayLists; 在内部,您将使用三个ArrayLists; one for storing the row number, second for storing the column number and the third for storing the actual value. 一个用于存储行号,第二个用于存储列号,第三个用于存储实际值。
Accordingly, you will write the add(int row, int column, int value) method, which takes care of things like keeping the ArrayLists sorted by row number, then by column number, etc. to increase the efficiency of random accesses. 因此,您将编写add(int row, int column, int value)方法,该方法负责保持ArrayLists按行号排序,然后按列号等排序,以提高随机访问的效率。
With this representation, you can implement all the methods like remove() , contains() , that are available for ArrayList. 使用此表示,您可以实现可用于ArrayList的所有方法,如remove()contains()

There are no native matrix types in the standard Java libraries. 标准Java库中没有本机矩阵类型。 That being said, it's fairly easy to create one. 话虽如此,创建一个相当容易。 The methods are trivial to implement and you can back it with an array, a List or whatever. 这些方法实现起来很简单,您可以使用数组, List或其他方法对其进行备份。

public class Matrix<T> {
  private final List<T> values;
  private final int rows;

  public Matrix(int x, int y) {
    this.rows = x;
    values = new ArrayList<T>(x * y);
  ]

  public int get(int x, int y) {
    return values.get(x * rows + y);
  }

  public boolean contains(T t) {
    return values.contains(t);
  }

  // etc
}

查看JAMA ,它来自Mathworks和NIST。

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

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