5

I want to store ARRAY from 3 dimensional array into buildingCostIds, but it says I MUST have 3rd number.

public static int[, ,] buildingCost = { {{0,1,2},{5,5,5}}};

public static void addBuilding(int[] ids, int[] amounts, int buildingId)
{
    int[] buildingCostIds = buildingCost[buildingId, 0, *];
}

*I need third number here, but I don't want it because it will extract just number, I want whole array!

PROBLEM SOLVED, solution:

public static Array extractArray(int dim1, int dim2)
{
    int[] tempArray = { };

    for (int number=0;number<=2; number++)
    {
    tempArray[number] = buildingCost[dim1, dim2, number];
    }
    return tempArray;
}
4
  • Why does the question say C++ and the tag says C#? Commented Jul 27, 2013 at 9:11
  • whoops, first time here Commented Jul 27, 2013 at 9:12
  • No worries - well done for fixing it Commented Jul 27, 2013 at 9:14
  • 1
    If you have found a solution, please mark the appropriate answer as accepted (even if that means adding an answer with your solution yourself and marking it as accepted possibly after waiting for a while. Commented Jul 27, 2013 at 9:43

2 Answers 2

5

This might be simpler if you use an array-of-arrays, or jagged array [][][] like

int[][][] buildingCost = new int[2][][];

Then you can access buildingCost[buildingId, 0] which is an array of ints.

There is a related question here


EDIT

Be aware the "problem solved" you added to the question duplicates the data, so you may run out of memory if you keep doing this. Consider using a List of Lists of Lists and lazy evaluating what you need.

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

1 Comment

No, I have much data and I'm lazy to write all them down like that. Found a solution! I'll post it!
2

Try This

int[, ,] array3D = new int[3,3,3]

2 Comments

How does this help to access the last column?
lol Stack Overflow rendered it as the try and this keywords

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.