what type of array should I use for this data structure

Hi there guys I know what I need to do here, but I’m not sure what would be the best to use for fast performance.

I need an array of Vector3 arrays. The Vector3 arrays will ALWAYS be in groups of 4. I have Googled al lot of jagged arrays examples, but all of them is with a set amount of rows and mine can vary.

Would someone please help me with a short code example, thanks in advance

anyone that can maybe help?

thanks

It’s not clear what you’re asking. You say it will always be in groups of four, but then you say the amount can vary. If they vary, then a jagged array is correct, if not, then use a 2D array.

–Eric

maybe this can help, the structure would look like this, obviously the values of the Vector3s will be random, I just used 1,2,3,4,5,6,7,8,9 for the example. As you can see, the columns are always 4 Vector3 values and the rows will vary. Hope this clarifies:

[0] { {1,2,3},{4,5,6},{7,8,9},{9,8,7} }
[1] { {1,2,3},{4,5,6},{7,8,9},{9,8,7} }
[2] { {1,2,3},{4,5,6},{7,8,9},{9,8,7} }
[3] { {1,2,3},{4,5,6},{7,8,9},{9,8,7} }
…etc…

Thanks

use a multi-dimensional array?

Yes, in that case use a multi-dimensional array (AKA rectangular array).

–Eric

Or an array of a custom struct/class ?

I know you can’t always go on what people say, but I noticed a number of guys saying that multidimentional arrays are slower than jagged. Is that true? And another thought: Would it be terrible if I use a dictionary with <int,Vector3[ ]> ? I just tried that now and it seems to work pretty nicely, but I don’t want to use it if it is bad practice…

Dictionaries are fine to use in my opinion.
They are a bit slower than using arrays of course.

Dont use a dictionary it is not the correct container for the job (as far as what have been described).
Use either a jagged array or you can simply use a normal 1 dimensional array.
Since you know that the inner “array” always has the same size in your case 4 you can just divide the array up into “structs” so that struct number 1 goes from index 0 to 3 struct number 2 from 4 to 7 and so on.

I dont think you can do it much quicker than that.

The problem with the whole concept of arrays, arraylists, lists and multidimensional arrays is memory space. When you have no defined limit, every time you change it, it will have to rebuild the memory table on the array. This makes all the methods far slower than a defined array.

If it were up to me, I would say… NO you can’t use over 65535 sets of data. Define my array to that maximum amount and simply have a pointer to the last used data block. Your memory is in tact, you can save and load the data as needed and convert it off to vertex data or whatever you are doing with it.

The problem with this method is… you suck up memory like a hog. Multiple objects like this will mean massive overhead.