1

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

4
  • Please try and keep your posts to one question - are you asking how to translate an ArrayList to C++, or what ArrayList al = this.gridList[a][b]; does? Commented Mar 15, 2013 at 22:12
  • @PhilipKendall OP wants to know how to translate a two dimension array of Java ArrayList class to C++. Commented Mar 15, 2013 at 22:13
  • 2
    You don't have an array list. You have an array of arrays of ArrayList. i.e. you have 300 * 150 references to ArrayList (all being null). a and b are allow getting one of these refereces, the one at the coordinates a, b in the matrix of ArrayLists. new ArrayList[a][b] creates a new array (of length a) or arrays (of length b) of ArrayList. Commented Mar 15, 2013 at 22:14
  • Sorry for asking too many questions at once. As mentioned below my question is how to translate the part ArrayList al = this.gridList[a][b] into C++? I can see later in the Java code that "al" is used in a loop like this (Integer)al.get(i)).intValue(); Commented Mar 15, 2013 at 22:25

3 Answers 3

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

How do you in your C++ example later retrieve array at offset a and offset b ? that is the part I get lost in
@user1185305 const auto& v = gridList[a][b]. Bear in mind that here you need to know the dimensions (150 and 300) at compile time. You basically can't have a generic direct translation from java to c++. You have to specify clearly what you want.
yes, I assumed here that the dimensions are constant. Otherwise a more classic C-style array would be necessary (or vector).
1

If your ArrayList is holding something of type 'Foo', then:

std::vector<Foo*> gridList[300][150];
std::vector al = this->gridList[a][b];

3 Comments

std::vector al = this->gridList[a][b]; Doesn't seem to compile. Any suggestions??
It compiles but I get SIGABRT when I run.
You need to specify the type that the vector holds - I used 'Foo' as an example.
1

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).

1 Comment

To be honest I don't know what I need. I just want to know how to translate those lines of Java code into C++ so that I can port the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.