Does anybody have an alternative method/data structure to get the functionality I am seeking? All 1000 elements of the array fill with the same values no matter the index I choose.
I am having troubling implementing a solution using duplicate. Could you describe a solution that essentially gives me a 2-dimensional array of a predefined size (say 1000) where all internal arrays are empty?
There is no function in your shown code in this post or the first, that passes anything by value.
You are using a class level array variable and sizing it, filling it with empty arrays, and in other function you are appending values to it.
This line creates an array of 1000 elements each of which is null (1 empty array): neighbors.resize(1000)
In this command you are filling the array with empty arrays (1000 individual arrays): neighbors.fill([])
Is that what you actually want?
This works, you just need to add your code in the other function, also your 'first, second and middle ID’s will need to be created as variables to do anything. Not really sure why you’d want to pass and empty array of arrays, would seem easier to just create and array where you need it…
If you only want to fill it with a single value then .fill(0) will give you an array full of zero’s, though this would be the default behaviour anyway.
I would like neighbors to be a 2-dimensional array initialized with empty subarrays so that I can append to them. The problem with
neighbors.fill([])
is that in my otherFunction(), [0], [1], [2], [3]… all append to the same array. In other words, I would like the subarray of index middleID to have size=2 and subarrays indices “first” and “second” to each have size=1
Lets say first = 0 nieghbors[0] is an array so when you do this: neighbors[first].append(middleID)
you change the element in neighbors from an array to the value of middleID
If you do this: neighbors[0][first].append(middleID)
then you would make the first element of the first array contained in neighbors the value of middleID
This confusion can be avoided by type-ing your variables: var neighbors:Array[Array]
This should give you an error if you try to give the first dimension an element that is not an array.
It looks like you are creating an array of structured data. Each element is going to be a group of information that is going to be consistent.
array[0] = first name, second name, middle id1, middle id2
array[1] = first name, second name, middle id1, middle id2
array[2] = first name, second name, middle id1, middle id2
etc
This structured data is a good candidate for a class:
class_name FullName
var fistName:String
var secondName:String
var middleID1:String
var middleID2:String
...
var neighbors:Array[FullName]