0

First. I'm sorry if the question the question is badly formulated because I don't really understand the question myself.

Hello, I'm currently helping a friend with his practice tasks about 2-Dimensional Arrays.

The following task is: Expand the array with the missing columns. Fill the column with two values each.

Now my question is how? Is it even possible to put 2 values in one?

For a better understanding, see this code:

static int[][] BSP = { 
    new int[] { 1, 2, 3, 4 },
    new int[] { 1, 2, 3 } ,
    new int[] { 1, 2, 3, 4, 5, 6 }
};

Now we should expand the array to the task I mentioned above. I understand the Task like this: Image

Instead of one value there should be 2. Can you help me or did I misunderstood the question.

Wyck
  • 10,311
  • 6
  • 39
  • 60
Tibbi
  • 23
  • 5
  • 1
    As difficult as it is for you to understand this question, it's even harder for us. We don't have the context of the course and the other practice questions. What "missing columns" is it talking about? Arrays don't have columns. I think your friend needs to ask their teacher, other classmates, or the author of the practice tasks for help. – StriplingWarrior Mar 30 '22 at 17:38
  • Can you show us the ACTUAL description of the problem maybe?... – Idle_Mind Mar 30 '22 at 17:43
  • the term 'expand' is rarely used outside of UI components. The term 'Add' would be more common when talking about collections. You should also be aware of the difference between [Multidimensional and jagged arrays](https://stackoverflow.com/questions/4648914/why-we-have-both-jagged-array-and-multidimensional-array). If you just say '2D array' I would assume the former, since it better represent a table with rows and columns. – JonasH Mar 30 '22 at 18:31

1 Answers1

0

When you are going to expand (or shrink) try to use list List<T> instead of array T[]. We want to extend lines while keeping columns intact, so we can use List<T[]> - list of arrays:

static List<int[]> BSP = new List<int[]>() {
  new int[] { 1, 2, 3, 4 }, 
  new int[] { 1, 2, 3 }, 
  new int[] { 1, 2, 3, 4, 5, 6 },
};

then you can just Add to extend (and RemoveAt to shrink):

// 4th row with 2 values: 12 and 21
BSP.Add(new int[] { 12, 21 });
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215