Add an multidimensional array to a list

BlitzMax Forums/BlitzMax Beginners Area/Add an multidimensional array to a list

Takis76(Posted 2013) [#1]
Hello my friends,

I would like to ask, is it possible to add one array to a list.
For example I have my levels in an one 4D array, is it possible to have my levels in one list?

For example I have one array with name levels[40][40][4] and I able to create a list of levels and able to add delete and change the order of the list of level arrays.

I tried something like:

my_levels[40, 40, 4]:TList = CreateList()

And didn't work.

I tried something like:

Global all_levels_list:TList
Global the_level:String[40, 40, 4]
all_levels_list = CreateList()
all_levels_list.AddFirst(the_level[40, 40, 4]) <= This line compiles too

But produces exception access violation error
If we assume the list all_levels_list was created, how to add one array in the list? and then access the index of the list of arrays and then access each of the array dimension separately?

I don't know if is possible to add an array to a list.
An array is a table by itself and can be used as list but is not dynamic.

Lists can add elements , remove , sort , add first , append , move elements up and down.
Is it possible with an array to change the size of the array on runtime?

With some special code you can change the order of the contents of the array but the algorithm is complex and you will need and temporary arrays to copy the contents from the main array, then erase the main array and paste the contents from temporary array back to the main array and if the array is huge you will need another one huge temporary array too.

But with the lists it will be easier (I think).

Also with list you have and other advantages that with arrays you don't have, for example if you have inventory with items and you need to add an item and you don't need to have a predefined array, and the list occupies less memory or occupies the memory of the size of the list, with arrays you will need to have a preoccupied memory and sometime is waste of memory.

Any idea?

Thank you very much.


GW(Posted 2013) [#2]
To add an array to a linked list do
Mylist.Addlast( myarray )

to get it back out
mynewarray = String[,,]( Mylist.Last() )


Linked lists only hold objects of type 'Object'
Arrays are objects, but after getting it back out of the list you need to cast it back into the a string array type to use it.


Takis76(Posted 2013) [#3]
I didn't understood your example very much.
I tried this code:

Global all_levels_list:TList
Global the_level:String[40, 40, 4]
all_levels_list = CreateList()
all_levels_list.AddLast(the_level)
my_new_levels_list = String[40, 40, 4] (all_levels_list.Last())


There is an error in the last line.

Then I saw you put the String with comas only without the numbers

This code seems to work:

Global all_levels_list:TList
Global the_level:String[40, 40, 4]
Global the_level_new:String[40, 40, 4]
the_level[1, 1, 1] = "01"
the_level[1, 1, 2] = "02"
all_levels_list = CreateList()
all_levels_list.AddLast(the_level)
the_level_new = String[,,] (all_levels_list.Last())
Print the_level_new[1, 1, 2]


So you need 2 arrays.
and as I understood the "String[,,]" is the cast to string
If I would need to cast to integer I would write "Int[,,]"

It seems work. Thank you for here.
Everytime I check the last only?
What if I need to check the third element in the list or the first or move the elements in the list up and down?