Reputation: 895
I have an ArrayList
ArrayList[][] gridList = new ArrayList[300][150];
// Where a and b are some values in the range of the ArrayList.
ArrayList al = this.gridList[a][b];
How could this be translated to C++ ?? I've tried using std::vector or std::map, std::array. I just can't get it to work.
ArrayList al = this.gridList[a][b];
What does that line actually do?
Does it create an array of size ??
ArrayList al = new ArrayList[a][b]
or maybe it copies the values of "a" and "b" in to the new ArrayList "al" ?
Please help
Upvotes: 1
Views: 1108
Reputation: 14730
Your code doesn't do what you think it does.
ArrayList[][] gridList = new ArrayList[300][150];
This first line allocates an array
of array
of ArrayList
.
ArrayList al = this.gridList[a][b];
This second line retrieves the ArrayList
at offset b
in the array
at offset a
in the array
gridList. You should be aware that your code doesn't initialize either arrays.
The equivalent type in C++ could be:
#include <vector>
#include <array>
std::array< std::array< std::vector<T>, 150>, 300> gridList;
where T
is the type of the element stored in the vectors. Note that Java prior to generics only allowed to define ArrayList without specifying the element type, which is pretty much what your code does. In C++, this parameter is mandatory. The above variable definition will instantiate it for the current scope. you will need to use a new
statement for a dynamic value (as in Java), and probably wrap it with a smart pointer.
To access an element of the grid, you can use the []
operator:
vector v = gridList[a][b];
Beware that this will trigger a full copy of the vector
content in the grid at position < a,b > into v
. As suggested, a more efficient way would be to write:
auto const &al = gridList[a][b];
Again, the memory model used by Java is very dynamic, so if you want code closer in behaviour to the Java version, you would probably have something like:
#include<memory>
typedef std::vector<int> i_vector;
typedef std::shared_ptr<i_vector> i_vector_ptr;
typedef std::array< std::array< i_vector_ptr>, 150>, 300> vector_grid;
typedef std::shared_ptr<vector_grid> vector_grid_ptr;
vector_grid_ptr gridList;
i_vector_ptr al = (*gridList)[a][b];
with type T
being int
, and each component of the grid type clearly defined. You still have to allocate the grid and each element (ie. i_vector
here).
Upvotes: 2
Reputation: 5163
Something like this may work (if you need container class):
struct Item
{ ...
};
typedef std::vector<Item> ArrayList;
// Single row.
struct ArrayListVector : public std::vector<ArrayList>
{
ArrayListVector() { resize(150); }
};
// Whole matrix
struct ArrayListMatrix : public std::vector<ArrayListVector>
{
ArrayListMatrix() { resize(300); }
};
...
ArrayListMatrix gridList; //< yes, it is 300 x 150
ArrayList &a = gridList[a][b]; //< or, you can make a copy
gridList[b][a] = a; //< assign an entry
Or do you need templates?
However there is a simple option, that doesn't need templates, classes, etc:
ArrayList array[300][150]; //< array is allocated (on stack, or statically).
Upvotes: 1
Reputation: 6542
If your ArrayList is holding something of type 'Foo', then:
std::vector<Foo*> gridList[300][150];
std::vector al = this->gridList[a][b];
Upvotes: 1