0

I created a 3d int array

public int[,,]  npcState =  new int[,,] {
    {
        {0,0}
    },{
        {1,9,1},{1,0,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {1,1,1},{10,10}
    },{
        {8,0},{0,0},{0,0},{0,0}
    },{
        {10,7},{1,1,1},{1,1,1},{1,1,1},{1,1,1}
    },{
        {2,2,2}
    },{
        {1,1,1} ,{1,1,1}
    },{
        {8,11},{0,0},{0,0},{0,0},{0,0}
    },{
        {0,1,1},{1,1,1}
    }
};

My questions are

1.) How to assign value at run time

2.)How to check array each rows and column using loop like

for(int i =0 ; i < firstDimensionalLength ; i ++){
  for(int j =0 ; j < secondDimensionalLength; j ++){
     for(int k =0 ; k < thirdDimensionalLength; k ++){
// print (npcState[i,j,k]); 
   }
  }
}

If it constant length for all dimensional , it is easy to find elements . But if it dynamic how to find each elements in particular positions

2
  • 2
    You can't define a 3D array like that. All inner arrays have to be the same length. You can only do that with jagged arrays. Commented Oct 10, 2016 at 8:55
  • 1
    Your code does not compile. Work on that first. Commented Oct 10, 2016 at 8:55

2 Answers 2

1

EDIT: as per commenter's suggestion I am adding a compiling version of the multi-dimensional array declaration:

public int[,,] npcState =  new int[,,] {
    {
       {2,2,2},{1,1,1},{1,1,1}
    },{
        {1,9,1},{1,0,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
       {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    },{
        {2,2,2},{1,1,1},{1,1,1}
    }
};

If you really want to use for loop, then you can access the length of the dimensions by using GetLength() method:

var firstDimensionalLength = npcState.GetLength(0);
var secondDimensionalLength = npcState.GetLength(1);
var thirdDimensionalLength = npcState.GetLength(2);

Source

Sign up to request clarification or add additional context in comments.

1 Comment

I would recommend to also add a compiling version of the initializing of such an array because the one in his question isn't valid
1

In case you want just to scan the entire array, try using foreach:

foreach (int item in npcState) {
  // print (item); 

  if (SomeCondition(item)) {
    ...
  }  
}

please notice, that the loop doesn't depend on array's dimensions (it will be the same for 1d, 2d, 3d etc. arrays)

Edit: if you want item's location (i.e. i, j, k indexes) you have to put

// In many cases you can put 0 instead of `npcState.GetLowerBound()` since 
// arrays are zero based by default
for (int i = npcState.GetLowerBound(0); i <= npcState.GetUpperBound(0); i++)
  for (int j = npcState.GetLowerBound(1); j <= npcState.GetUpperBound(1); ++j)
    for (int k = npcState.GetLowerBound(2); k <= npcState.GetUpperBound(2); ++k) {
      int item = npcState[i, j, k];
      ...
    }

Edit 2: Since the question has been edited and N-D array has been turned into jagged one the solution should have been changed as well:

  int[][][] npcState = new int[][][] {
    new int[][] {
      new int[] { 0, 0 } },
    new int[][] {
      new int[] { 1, 9, 1},
      new int[] { 1, 0, 1},
      new int[] { 1, 1, 1}, },
    new int[][] {
      new int[] { 2, 2, 2},
      new int[] { 1, 1, 1},
      new int[] { 1, 1, 1}, }, 
    // ...
  };

 // Array of array of array can be just flatten twice
 foreach (var item in npcState.SelectMany(line => line.SelectMany(row => row))) {
   ...
 }

Location preserved loop will be

 for (int i = 0; i < npcState.Length; ++i) 
   for (int j = 0; j < npcState[i].Length; ++j) 
     for (int k = 0; k < npcState[i][j].Length; ++k) {
       int item = npcState[i][j][k];
       ...   
     }

18 Comments

This code will give every elements , I need each elements in particular position
how to find particular element in particular position in array .
@Liju Thomas: in case you want not just item itself, but its location (i.e. i, j, k indexes) as well you have to scan dimensions: e.g. for (int i = npcState.GetLowerBound(0); i <= npcState.GetUpperBound(0); i++) for the 1st one (see my edit)
@Liju Thomas: What doesn't work, please? Do you have an exception? The code doesn't compile; the outcome doesn't match the expected one?
Result is (0,0,0) = 0 (0,0,1) = 0 (1,0,0) = 1 (1,0,1) = 9 . not getting third (1,0,2) . only getting two elements per each row
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.