1

How can I size my columns dynamically to support a possible ragged array?

int[][] x;
x = new int[3][] //makes 3 rows
col = 1;
for( int i = 0; i < x.length; i++){
  x = new int[i][col]
  col++;  }

Would the above code assign each col length?

Thank you in advance for any help.

0

2 Answers 2

2

since you are re-assigning x, what you are doing is creating the entire 2D array each loop, which is wrong.

You need to do inside your loop:

x[i] = new int[col];
Sign up to request clarification or add additional context in comments.

1 Comment

that is exactly what I mean to do!! Thank you so much for the Syntax!!
0
// create the single reference
int[][] x;

// create the array of references 
x = new int[3][] //makes 3 rows

int col = 1;
for( int i = 0; i < x.length; i++){
    // this create the second level of arrays // answer
    x[i] = new int[col];
    col++;  
}

More on 2D Arrays. - https://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm

Comments

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.