Jump to content
  • Advertisement
Sign in to follow this  
LAURENT*

An array of array of int type

This topic is 590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

it's not clear to me so 3 options :

Array of array of int

​std::vector< std::vector< int > >

Array of array of int*

std:vector< std::vector< int* > >

Array of array of array of int

std::vector< std::vector< std::vector< int > > >

If the size is fixed you can simply do :

int* array[10][10]

or

int array[10][10][10]
Edited by Alundra

Share this post


Link to post
Share on other sites

std is the namespace used for the standard library of c++.
Maybe are you doing :

using namespace std;

If the answer is yes, then I hardly recommend to not do.

Edited by Alundra

Share this post


Link to post
Share on other sites

std is the namespace used for the standard library of c++.
Maybe are you doing :

using namespace std;
If the answer is yes, then I hardly recommend to not do.


This topic is tagged "Java", so I don't know why we are talking about C++ idioms.

Share this post


Link to post
Share on other sites

In Java, you can create jagged arrays either as array objects or as containers of containers.  You still need to create each instance:

int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[10];
jaggedArray[2] = new int[17];

You can also do this with a collection, ArrayList is the preferred type. 

ArrayList< ArrayList<int> > collectionOfCollections = new ArrayList< ArrayList<int> >();
collectionOfCollections.add( new ArrayList<int>() );
collectionOfCollections.add( new ArrayList<int>() );

If you don't need a jagged array, it is usually better to have a flat array and access a position within it. That prevents the cost of multiple indirections needed for looking up within the first array, then looking up within the second array; instead you can easily compute the index and use a single indirection to find it.

int[] myArray = new int[Width * Height];
...
index = y*Width + x;  // Row major, walking the array goes through each x, then advances one y and through each x again
OR
index = x*Height + y; // Column major, walking the array goes through each y, then advances one x and through each Y again

You will want to replace those with named constants rather than numbers, and probably put the object creation inside loops rather than calling them individually, but that should be enough to get started.

Share this post


Link to post
Share on other sites
This topic is tagged "Java", so I don't know why we are talking about C++ idioms.

Good point, I simply clicked on the home page and answered.

Edited by Alundra

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!