Jagged Java arrays of int -
i understand x
jagged array in
int x[][] = {{0,1,2,3,4},{0,1,2},{0,1,2,3}};
but given array a
like
int a[] = {10,3,47,4,8};
is a
jagged array?
the overall cs answer
the term "jagged" (i've seen "ragged") array refers a multi-dimension array (>1) each element array. thus
int[] = {1,2,3};
is not jagged array,
int[][] = {{1,2,3,4}, {5,6,7}};
is. however, counterintuitively,
int[][] = {{1,2,3,4}, {5,6,7,8}};
is jagged array, though "looks even" if draw out:
int[][] = { {1,2,3,4}, {5,6,7,8} };
this because other coding languages (such c#) distinguish between multi-dimensional arrays , jagged arrays. in languages, "jagged" not description of current structure of array in question, type of object entirely. see difference between 2 here
the java answer
unlike other languages, java allows single arrays of given type. type int[][]
"an array of int[]
". java doesn't support true multi-dimensional arrays, has jagged arrays
because of this, term "jagged" array takes on different meaning in conventional java-speak. arrays of dimension > 1 jagged, term "jagged" comes mean array of dimension > 1 sub arrays of differing length.thus following array "jagged" because first , second sub-arrays of unequal length:
int[][] = {{1,2,3,4}, {5,6,7}}
as equivalent to:
{ {1, 2, 3, 4}, //length 4 {5, 6, 7} //length 3 }
this array considered jagged:
{ {1, 2, 3, 4}, //length 4 {11, 12, 13, 14}, //length 4 {21, 22, 23, 24}, //length 4 {31, 32, 33, 34}, //length 4 {5, 6, 7} //length 3 }
even though following jagged array in technical sense, not conventionally referred such:
int[][] = { {1,2,3,4}, {5,6,7,8} }
similarly, because "jagged"ness requires 2 sub-arrays of unequal length, 1-d array can't jagged has no sub-arrays compare.