开发者

How to access elements within multi-dimensional vector?

I need to create a variable-sized two-dimensional coordinate system. What I've been able to come up with so far is:

Vecto开发者_高级运维r<Coordinate> board = new Vector();

for ( int count = 0; count < num_rows; count++ ) {
  board.add(new Vector(num_cols));
}

How do I access the elements within this multi-dimensional vector? I have tried doing board[row][col] but this didn't seem to work.

I'm familiar with using Vectors in C++, but can't seem to figure out how to do this in Java.


http://download.oracle.com/javase/6/docs/api/java/util/Vector.html

You need to use .get(index_number) so that becomes board.get(row).get(col)


I don't get how you are adding a Vector into a Vector of Coordinates. You might try something like List<List<Coordinate>> board. Then use board.get(1).get(2) to get a position.

What you really might try is the Guava Table. http://docs.guava-libraries.googlecode.com/git-history/release09/javadoc/index.html

Then it would be:

Table<Integer, Integer, Coordinate> board;
board.put(1, 2, new Coordinate());


A vector in Java is more like a list than an array. To access the element at position 0 in vector v, use:

v.elementAt(0)

or

v.get(0)

Check the documentation


I recommend using two-dimensional array:

Coordinate[][] space = new Coordinate[width][height];
...
Coordinate valuableInfo = space[x][y];  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜