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.
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];
// 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.