[英]Can a vector reference a 2-D array in Java?
基本上,我想创建一个在运行时大小会增加的二维数组。 我可以使用Java中的向量来做到这一点吗?
这些对吗?
int [] [] x = new x [100] [100];
向量x = new Vector();
要将元素添加到数组中,
for i=0 to 99
for j=0 to 99
x.addElement(x[i] [j]);
本质上,这就像引用任何其他对象一样,只是在这里,我还必须指定索引号,对吗?
我是Java的新手。 因此,我将不胜感激!
在不知道需要优化的情况下,很难知道对您有什么建议。
例如,围绕地图的这个简单包装器是一个稀疏的2D矩阵,已优化用于设置和获取特定的索引(根本不需要“增长”),但不利于遍历所有索引。
public class SparseMatrix<T> {
private final Map<Coordinates, T> map = new HashMap<Coordinates, T>();
private final T defaultValue;
public SparseMatrix(T defaultValue) {
this.defaultValue = defaultValue;
}
private static class Coordinates {
private final int[] coordinates;
Coordinates(int... coordinates) {
this.coordinates = coordinates;
}
@Override
public boolean equals(Object o) {
return Arrays.equals(coordinates, ((Coordinates)o).coordinates);
}
@Override
public int hashCode() {
return Arrays.hashCode(coordinates);
}
}
public T get(int x, int y) {
T value = map.get(new Coordinates(x, y));
if ( value == null ) {
return defaultValue;
}
}
public T set(int x, int y, T val) {
return map.put(new Coordinates(x, y), val);
}
}
用法:
SparseMatrix<Integer> matrix = new SparseMatrix<Integer>(0);
matrix.set(3, 5, 7);
int seven = matrix.get(3, 5);
int zero = matrix.get(3, 6); //not set yet, uses default
它也可以很容易地适应N维。 当然,在生产代码中,您不会自己动手,而是使用性能更好的库。
您可以使用ArrayList
等效于动态数组。
ArrayList<ArrayList<Integer>> aDynamicArray = new ArrayList<ArrayList<Integer>>(2);
aDynamicArray.add(new ArrayList<Integer>());
aDynamicArray.add(new ArrayList<Integer>());
现在,您可以将您for
循环,如下所示:
int [] [] x = new x [2] [100];
for i=0 to 1
for j=0 to 99
aDynamicArray.get(i).add(x[i] [j]);
编辑:
如果我想访问x [2] [3],我将如何在arraylist中做到这一点?
x[2][3]
表示在第三行和第四列的值。 因此,Fisrt的代码行将更改如下:(以容纳3行)
ArrayList<ArrayList<Integer>> aDynamicArray = new ArrayList<ArrayList<Integer>>(3);
for(int i=0; i<3, i++)
{
aDynamicArray.add(new ArrayList<Integer>());
}
那么下面的行将使您可以访问x[2][3]
:
aDynamicArray.get(2).get(3);
不幸的是,Java既没有2D数组也没有2D增长向量。
它具有数组的数组或向量的向量。
在您的示例中,您创建了Object
的一维向量:
Vector x= new Vector();
类似于类型为Object[] x
数组的增长
所以,当你这样做
y.addElement(x[i][j]);
Java执行“装箱”,即
y.addElement(new Integer(x[i][j]));
由于您已将100x100元素添加到一维数组中,因此您需要自己计算位置
y.get(i*numcols+j)
因此,为避免所有这些情况,请使用以下示例中的向量vector。 该示例将新固定大小的数组复制到vector的vector中:
// creates fixed size 2D array with zeros
int [] [] x = new int [50][50];
// creates empty vector of vectors of integers
// y is of type Vector<Vector<Integer>>
// y.get(row) is of type Vector<Integer>>
// y.get(row).get(col) is of type Integer
Vector<Vector<Integer>> y = new Vector<Vector<Integer>>();
// set the size of vector of vectors (number of rows)
// each row will be null for now
y.setSize(x.length);
// enumerating rows
for(int row=0; row<x.length; ++row) {
log.info("row {}", row);
// assign empty vector for row
y.set(row, new Vector<Integer>());
// set row size (number of columns)
y.get(row).setSize(x[row].length);
// enumerating columns of current row
for(int col=0; col<x[row].length; ++col) {
// setting the value for a cell
y.get(row).set(col, x[row][col]);
}
}
在java中将是这样的:
int [][] x = new int [100] [100]
ArrayList <Integer> y = new ArrayList();
y.add(some number);
有关Arraylist的更多信息
Java让您有一个由不同数据类型组成的集合。 例如
ArrayList<Object> list = new ArrayList<Object>();
list.add(1);
list.add("Java");
list.add(3.14);
因此,您也可以使用ArrayList的ArrayList等等。
如果您知道数组的大小(前面提到的是99),则可以使用int[][] intA = new int[100][100]
,如果您对Java熟悉,则可以实现自己的Growing int[][]
通过监视添加元素时的数组大小。
假设您正在添加最后一个元素intA[99][99] = someInt
,此时您可以创建新的数组int[][] newIntA = new int[intA.length+25][100];
那么您需要将现有intA
数组的内容复制到newIntA
或者您可以使用ArrayList<ArrayList<Integer>>
添加值或使用ArrayList[]
选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.