1

I have this array here:

float[, ,] vectors;

int pos = 0;

void Start() {
   vectors = new float[,,] { {
       { 0, 1, 1 }, 
       { 0, 2, 2 } }
    };        
}

This works. I fill the array with numbers.

Now I want to add some values again to a given position. But how?

This are not working:

vectors[pos] = new float[,,] { { { 33, 44, 55 } } };     

or

vectors[pos] = { { { 33, 44, 55 } } };     

I searched, but not found the right answer.

EDIT: I want something like this:

[0]+
   [0] {1, 2, 3},
   [1] {4, 5, 6}

[1]+
   [0] {11, 22, 33},
   [1] {44, 55, 66},
   [2] {77, 88, 99}

...
etc.

Now, e.g. I want add values {10,10,10} to pos = 0. But how?

Johnny
  • 612
  • 3
  • 13
  • 32
  • 2
    you are declaring it as a multidimensional array, but using it as an array of arrays [of arrays]. [see the differences](http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays) – kennyzx Oct 29 '14 at 13:47
  • 1
    You might want to use a Vector class and put them into a generic list instead of a multi-dimentional array. – juharr Oct 29 '14 at 13:48
  • How can I solve it with an array? I want a three-dimensional array. Is this not possible? – Johnny Oct 29 '14 at 13:53
  • Are you saying you need two dimensions to identify a given vector and the last dimension is just to index the 3 values of a vector? – juharr Oct 29 '14 at 13:57
  • @juharr. I edited the question. Please look. – Johnny Oct 29 '14 at 14:06

3 Answers3

2

If you want to add values I suggest using generic lists instead of arrays. And you should create your own Vector class or find one that is suitable to your needs like this.

public class Vector
{
    public float X { get; private set; }
    public float Y { get; private set; }
    public float Z { get; private set; }

    public Vector(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }
}

Then you can do the following

var vectors = new List<List<Vector>>
{
    new List<Vector>{
        new Vector(0, 1, 1),
        new Vector(0, 2, 2)
    }
};

vectors[0].Add(new Vector(33,44,55));

And your vectors will contain

[0]
    [0] {0, 1, 1}
    [1] {0, 2, 2}
    [2] {33, 44, 55}

Note that if you need to add to the first dimention you have to do this.

vectors.Add(new List<Vector>());

vectors[1].Add(new Vector(1, 2, 3));

And now you have

[0]
    [0] {0, 1, 1}
    [1] {0, 2, 2}
    [2] {33, 44, 55}
[1]
    [0] {1, 2, 3}
juharr
  • 31,741
  • 4
  • 58
  • 93
  • the reason it works is because it is not using static arrays which are contiguous blocks of memory and cannot be changed after assignment for that reason, they are a better way to represent what you are doing. the answer I provided does essentially the same thing, although List will have a capacity it provides by default and so is a little more efficient. This answer also has the advantage of giving you a strong type to address your coordinates. Allthough if its just points I'd argue a struct is better. – krystan honour Oct 29 '14 at 14:42
0

You should determine the other positions within the array, you are just specifying one. If your problem cannot be solved within lists you can try array of arrays as follows

float [][][] x = new float [n][m][];

// initialize the third dimension until m is reached
x[0] = new float {1,2,3,4,5}; // specify whatever value you want
x[1] = new float {3,2,4};
x[2] = new float [3];

// etc until m is reached
// do the same for the n dimension
Median Hilal
  • 1,483
  • 9
  • 17
0

This will work for you, the array is assigned, you cannot change it you must expand the array as is.

float[, ,] vectors;

int pos = 0;

vectors = new float[,,]
{
    { 
        { 0, 1, 2 }, { 0, 3, 4 }
    }
};        


vectors =  new float[,,]
    {
        {
            {vectors[0,0,0], vectors[0,0,1], vectors[0,0,2]}, { vectors[0,1,0], vectors[0,1,1], vectors[0,1,2] }, { 33,44,55}
        }
    };
krystan honour
  • 6,523
  • 3
  • 36
  • 63
  • This works. I know. But I can't add new values to the position that I want. This is only for changing the values. – Johnny Oct 29 '14 at 14:12
  • Are you saying you want more entries, because that can't be done without dynamically expanding the array – krystan honour Oct 29 '14 at 14:13