[{"data":1,"prerenderedAt":2505},["ShallowReactive",2],{"blog-\u002Fblog\u002Fjava-arrays-declaration-utilities":3},{"id":4,"title":5,"body":6,"description":2492,"difficulty":2493,"extension":2494,"framework":2495,"frameworkSlug":76,"meta":2496,"navigation":545,"order":153,"path":2497,"qaPath":2498,"seo":2499,"stem":2500,"subtopic":41,"topic":2501,"topicSlug":2502,"updated":2503,"__hash__":2504},"blog\u002Fblog\u002Fjava-arrays-declaration-utilities.md","Java Arrays — Declaration, the Arrays Utility Class & Gotchas",{"type":7,"value":8,"toc":2477},"minimark",[9,14,43,47,71,250,264,268,309,427,453,457,467,638,671,675,712,821,824,828,843,1044,1061,1065,1109,1163,1187,1191,1238,1474,1492,1496,1533,1766,1775,1779,1838,2068,2086,2090,2113,2247,2266,2270,2306,2375,2392,2396,2473],[10,11,13],"h2",{"id":12},"arrays-are-the-foundation-everything-else-is-built-on","Arrays are the foundation everything else is built on",[15,16,17,18,22,23,22,26,29,30,34,35,38,39,42],"p",{},"The array is Java's most primitive container, and almost every richer data structure —\n",[19,20,21],"code",{},"ArrayList",", ",[19,24,25],{},"HashMap",[19,27,28],{},"StringBuilder"," — is an array underneath. That makes arrays a\nfavourite interview warm-up: the questions look easy, but they quietly probe whether you\nunderstand references, the heap, covariance, and the difference between a ",[31,32,33],"em",{},"field"," and a\n",[31,36,37],{},"method",". This guide walks through declaring arrays, the values they hold, the ",[19,40,41],{},"Arrays","\nutility class, and the handful of gotchas that separate someone who \"uses arrays\" from\nsomeone who understands them.",[10,44,46],{"id":45},"declaring-and-initializing-an-array","Declaring and initializing an array",[15,48,49,50,53,54,57,58,62,63,66,67,70],{},"An array is declared with a ",[19,51,52],{},"type[]"," and brought to life with ",[19,55,56],{},"new",", an ",[59,60,61],"strong",{},"array\ninitializer"," ",[19,64,65],{},"{ ... }",", or both at once. The brackets can legally sit on the variable\n(",[19,68,69],{},"int d[]",") as a leftover from C, but the convention is to put them on the type so the\ntype reads as \"array of int.\"",[72,73,78],"pre",{"className":74,"code":75,"language":76,"meta":77,"style":77},"language-java shiki shiki-themes github-light github-dark","int[] a = new int[3];          \u002F\u002F size only — Java fills it with {0, 0, 0}\nint[] b = new int[]{1, 2, 3};  \u002F\u002F size + values together\nint[] c = {1, 2, 3};           \u002F\u002F shorthand — only valid at the declaration\nint   d[] = {1, 2, 3};         \u002F\u002F legal C-style, but discouraged\n\u002F\u002F c = {4, 5, 6};              \u002F\u002F ERROR — bare {...} only works at declaration\nc = new int[]{4, 5, 6};        \u002F\u002F reassigning needs the full new int[]{...} form\n","java","",[19,79,80,116,151,180,208,217],{"__ignoreMap":77},[81,82,85,89,93,96,99,102,105,109,112],"span",{"class":83,"line":84},"line",1,[81,86,88],{"class":87},"szBVR","int",[81,90,92],{"class":91},"sVt8B","[] a ",[81,94,95],{"class":87},"=",[81,97,98],{"class":87}," new",[81,100,101],{"class":87}," int",[81,103,104],{"class":91},"[",[81,106,108],{"class":107},"sj4cs","3",[81,110,111],{"class":91},"];          ",[81,113,115],{"class":114},"sJ8bj","\u002F\u002F size only — Java fills it with {0, 0, 0}\n",[81,117,119,121,124,126,128,130,133,136,138,141,143,145,148],{"class":83,"line":118},2,[81,120,88],{"class":87},[81,122,123],{"class":91},"[] b ",[81,125,95],{"class":87},[81,127,98],{"class":87},[81,129,101],{"class":87},[81,131,132],{"class":91},"[]{",[81,134,135],{"class":107},"1",[81,137,22],{"class":91},[81,139,140],{"class":107},"2",[81,142,22],{"class":91},[81,144,108],{"class":107},[81,146,147],{"class":91},"};  ",[81,149,150],{"class":114},"\u002F\u002F size + values together\n",[81,152,154,156,159,161,164,166,168,170,172,174,177],{"class":83,"line":153},3,[81,155,88],{"class":87},[81,157,158],{"class":91},"[] c ",[81,160,95],{"class":87},[81,162,163],{"class":91}," {",[81,165,135],{"class":107},[81,167,22],{"class":91},[81,169,140],{"class":107},[81,171,22],{"class":91},[81,173,108],{"class":107},[81,175,176],{"class":91},"};           ",[81,178,179],{"class":114},"\u002F\u002F shorthand — only valid at the declaration\n",[81,181,183,185,188,190,192,194,196,198,200,202,205],{"class":83,"line":182},4,[81,184,88],{"class":87},[81,186,187],{"class":91},"   d[] ",[81,189,95],{"class":87},[81,191,163],{"class":91},[81,193,135],{"class":107},[81,195,22],{"class":91},[81,197,140],{"class":107},[81,199,22],{"class":91},[81,201,108],{"class":107},[81,203,204],{"class":91},"};         ",[81,206,207],{"class":114},"\u002F\u002F legal C-style, but discouraged\n",[81,209,211,214],{"class":83,"line":210},5,[81,212,213],{"class":114},"\u002F\u002F c = {4, 5, 6};",[81,215,216],{"class":114},"              \u002F\u002F ERROR — bare {...} only works at declaration\n",[81,218,220,223,225,227,229,231,234,236,239,241,244,247],{"class":83,"line":219},6,[81,221,222],{"class":91},"c ",[81,224,95],{"class":87},[81,226,98],{"class":87},[81,228,101],{"class":87},[81,230,132],{"class":91},[81,232,233],{"class":107},"4",[81,235,22],{"class":91},[81,237,238],{"class":107},"5",[81,240,22],{"class":91},[81,242,243],{"class":107},"6",[81,245,246],{"class":91},"};        ",[81,248,249],{"class":114},"\u002F\u002F reassigning needs the full new int[]{...} form\n",[15,251,252,253,255,256,259,260,263],{},"The key subtlety is that the bare ",[19,254,65],{}," shorthand is ",[31,257,258],{},"declaration-only",". Once the\nvariable exists you must use ",[19,261,262],{},"new int[]{...}"," to give it a fresh array. Either way, the\nlength is locked in the moment the array is created.",[10,265,267],{"id":266},"an-array-is-an-object-with-default-values","An array is an object, with default values",[15,269,270,271,274,275,278,279,282,283,286,287,290,291,294,295,297,298,301,302,304,305,308],{},"Even an array of primitives is a full ",[59,272,273],{},"object on the heap"," — the variable holds a\n",[31,276,277],{},"reference"," to it. That object has a runtime class (",[19,280,281],{},"int[].class",", printed as ",[19,284,285],{},"[I","),\ninherits from ",[19,288,289],{},"Object",", and can therefore be ",[19,292,293],{},"null",", be stored in an ",[19,296,289],{}," variable,\nand answer ",[19,299,300],{},"clone()",". Allocating with ",[19,303,56],{}," also ",[59,306,307],{},"zero-initializes"," every slot, so you\nnever read garbage. The default depends on the element type.",[72,310,312],{"className":74,"code":311,"language":76,"meta":77,"style":77},"int[]     nums  = new int[3];      \u002F\u002F {0, 0, 0}        — numeric default 0\nboolean[] flags = new boolean[2];  \u002F\u002F {false, false}   — boolean default false\nString[]  names = new String[2];   \u002F\u002F {null, null}     — references default null\nObject o = nums;                   \u002F\u002F arrays ARE Objects\nSystem.out.println(nums.getClass().getName()); \u002F\u002F \"[I\" — int array\n",[19,313,314,337,362,387,400],{"__ignoreMap":77},[81,315,316,318,321,323,325,327,329,331,334],{"class":83,"line":84},[81,317,88],{"class":87},[81,319,320],{"class":91},"[]     nums  ",[81,322,95],{"class":87},[81,324,98],{"class":87},[81,326,101],{"class":87},[81,328,104],{"class":91},[81,330,108],{"class":107},[81,332,333],{"class":91},"];      ",[81,335,336],{"class":114},"\u002F\u002F {0, 0, 0}        — numeric default 0\n",[81,338,339,342,345,347,349,352,354,356,359],{"class":83,"line":118},[81,340,341],{"class":87},"boolean",[81,343,344],{"class":91},"[] flags ",[81,346,95],{"class":87},[81,348,98],{"class":87},[81,350,351],{"class":87}," boolean",[81,353,104],{"class":91},[81,355,140],{"class":107},[81,357,358],{"class":91},"];  ",[81,360,361],{"class":114},"\u002F\u002F {false, false}   — boolean default false\n",[81,363,364,367,370,372,374,377,379,381,384],{"class":83,"line":153},[81,365,366],{"class":87},"String",[81,368,369],{"class":91},"[]  names ",[81,371,95],{"class":87},[81,373,98],{"class":87},[81,375,376],{"class":87}," String",[81,378,104],{"class":91},[81,380,140],{"class":107},[81,382,383],{"class":91},"];   ",[81,385,386],{"class":114},"\u002F\u002F {null, null}     — references default null\n",[81,388,389,392,394,397],{"class":83,"line":182},[81,390,391],{"class":91},"Object o ",[81,393,95],{"class":87},[81,395,396],{"class":91}," nums;                   ",[81,398,399],{"class":114},"\u002F\u002F arrays ARE Objects\n",[81,401,402,405,409,412,415,418,421,424],{"class":83,"line":210},[81,403,404],{"class":91},"System.out.",[81,406,408],{"class":407},"sScJk","println",[81,410,411],{"class":91},"(nums.",[81,413,414],{"class":407},"getClass",[81,416,417],{"class":91},"().",[81,419,420],{"class":407},"getName",[81,422,423],{"class":91},"()); ",[81,425,426],{"class":114},"\u002F\u002F \"[I\" — int array\n",[15,428,429,430,433,434,437,438,441,442,445,446,448,449,452],{},"This differs from ",[59,431,432],{},"local variables",", which get no default and must be assigned before\nuse. Array slots, like instance fields, are always initialized. Note the consequence for\nobject arrays: ",[19,435,436],{},"new String[2]"," creates the ",[31,439,440],{},"array"," but ",[59,443,444],{},"not"," the strings — every slot\nis ",[19,447,293],{}," until you populate it, so calling a method on an unfilled slot throws\n",[19,450,451],{},"NullPointerException",".",[10,454,456],{"id":455},"fixed-length-and-length-vs-length-vs-size","Fixed length, and length vs length() vs size()",[15,458,459,460,463,464,466],{},"An array's ",[59,461,462],{},"length is fixed at creation"," and can never change. To \"grow\" one you\nallocate a new, larger array and copy the elements across — which is exactly what\n",[19,465,21],{}," does for you internally. The size itself is exposed through a field, and\nthis is where three look-alike members trip people up.",[72,468,470],{"className":74,"code":469,"language":76,"meta":77,"style":77},"int[] arr = {1, 2, 3};\nString s  = \"hello\";\nList\u003CInteger> list = List.of(1, 2);\n\narr.length    \u002F\u002F 3 — a FIELD on arrays (no parentheses)\ns.length()    \u002F\u002F 5 — a METHOD on String\nlist.size()   \u002F\u002F 2 — a METHOD on collections\n\nint[] bigger = Arrays.copyOf(arr, arr.length + 1); \u002F\u002F {1, 2, 3, 0} — the \"grow\" idiom\n\u002F\u002F arr.length()  \u002F\u002F compile error\n\u002F\u002F s.length      \u002F\u002F compile error\n",[19,471,472,496,510,541,547,555,569,584,589,620,629],{"__ignoreMap":77},[81,473,474,476,479,481,483,485,487,489,491,493],{"class":83,"line":84},[81,475,88],{"class":87},[81,477,478],{"class":91},"[] arr ",[81,480,95],{"class":87},[81,482,163],{"class":91},[81,484,135],{"class":107},[81,486,22],{"class":91},[81,488,140],{"class":107},[81,490,22],{"class":91},[81,492,108],{"class":107},[81,494,495],{"class":91},"};\n",[81,497,498,501,503,507],{"class":83,"line":118},[81,499,500],{"class":91},"String s  ",[81,502,95],{"class":87},[81,504,506],{"class":505},"sZZnC"," \"hello\"",[81,508,509],{"class":91},";\n",[81,511,512,515,518,521,523,526,529,532,534,536,538],{"class":83,"line":153},[81,513,514],{"class":91},"List\u003C",[81,516,517],{"class":87},"Integer",[81,519,520],{"class":91},"> list ",[81,522,95],{"class":87},[81,524,525],{"class":91}," List.",[81,527,528],{"class":407},"of",[81,530,531],{"class":91},"(",[81,533,135],{"class":107},[81,535,22],{"class":91},[81,537,140],{"class":107},[81,539,540],{"class":91},");\n",[81,542,543],{"class":83,"line":182},[81,544,546],{"emptyLinePlaceholder":545},true,"\n",[81,548,549,552],{"class":83,"line":210},[81,550,551],{"class":91},"arr.length    ",[81,553,554],{"class":114},"\u002F\u002F 3 — a FIELD on arrays (no parentheses)\n",[81,556,557,560,563,566],{"class":83,"line":219},[81,558,559],{"class":91},"s.",[81,561,562],{"class":407},"length",[81,564,565],{"class":91},"()    ",[81,567,568],{"class":114},"\u002F\u002F 5 — a METHOD on String\n",[81,570,572,575,578,581],{"class":83,"line":571},7,[81,573,574],{"class":91},"list.",[81,576,577],{"class":407},"size",[81,579,580],{"class":91},"()   ",[81,582,583],{"class":114},"\u002F\u002F 2 — a METHOD on collections\n",[81,585,587],{"class":83,"line":586},8,[81,588,546],{"emptyLinePlaceholder":545},[81,590,592,594,597,599,602,605,608,611,614,617],{"class":83,"line":591},9,[81,593,88],{"class":87},[81,595,596],{"class":91},"[] bigger ",[81,598,95],{"class":87},[81,600,601],{"class":91}," Arrays.",[81,603,604],{"class":407},"copyOf",[81,606,607],{"class":91},"(arr, arr.length ",[81,609,610],{"class":87},"+",[81,612,613],{"class":107}," 1",[81,615,616],{"class":91},"); ",[81,618,619],{"class":114},"\u002F\u002F {1, 2, 3, 0} — the \"grow\" idiom\n",[81,621,623,626],{"class":83,"line":622},10,[81,624,625],{"class":114},"\u002F\u002F arr.length()",[81,627,628],{"class":114},"  \u002F\u002F compile error\n",[81,630,632,635],{"class":83,"line":631},11,[81,633,634],{"class":114},"\u002F\u002F s.length",[81,636,637],{"class":114},"      \u002F\u002F compile error\n",[15,639,640,641,62,643,22,645,647,648,651,653,654,657,658,657,661,664,665,62,668,670],{},"Memorize the split: arrays have a ",[19,642,562],{},[59,644,33],{},[19,646,366],{}," has a ",[19,649,650],{},"length()",[59,652,37],{},", and ",[19,655,656],{},"List","\u002F",[19,659,660],{},"Set",[19,662,663],{},"Map"," have a ",[19,666,667],{},"size()",[59,669,37],{},". Writing the wrong one is a\ncompile error, not a runtime surprise.",[10,672,674],{"id":673},"bounds-checking-and-invalid-sizes","Bounds checking and invalid sizes",[15,676,677,678,681,682,685,686,691,692,695,696,699,700,702,703,705,706,711],{},"Java checks every array access. A valid index runs from ",[19,679,680],{},"0"," to ",[19,683,684],{},"length - 1","; anything\noutside throws ",[59,687,688],{},[19,689,690],{},"ArrayIndexOutOfBoundsException"," at runtime. There is ",[59,693,694],{},"no negative\nindexing"," — ",[19,697,698],{},"a[-1]"," is an error, not \"the last element.\" Separately, the ",[31,701,577],{}," you pass\nto ",[19,704,56],{}," is evaluated at runtime, so a computed negative length throws\n",[59,707,708],{},[19,709,710],{},"NegativeArraySizeException"," (zero is fine — it gives a valid empty array).",[72,713,715],{"className":74,"code":714,"language":76,"meta":77,"style":77},"int[] a = {10, 20, 30};\na[2];                 \u002F\u002F 30 — last element, index length - 1\n\u002F\u002F a[3];              \u002F\u002F ArrayIndexOutOfBoundsException\n\u002F\u002F a[-1];             \u002F\u002F also AIOOBE — no negative indexing in Java\n\nint[] empty = new int[0];   \u002F\u002F legal — empty array, length 0\nint n = -1;\n\u002F\u002F int[] bad = new int[n];  \u002F\u002F NegativeArraySizeException at runtime\n",[19,716,717,742,755,763,771,775,797,813],{"__ignoreMap":77},[81,718,719,721,723,725,727,730,732,735,737,740],{"class":83,"line":84},[81,720,88],{"class":87},[81,722,92],{"class":91},[81,724,95],{"class":87},[81,726,163],{"class":91},[81,728,729],{"class":107},"10",[81,731,22],{"class":91},[81,733,734],{"class":107},"20",[81,736,22],{"class":91},[81,738,739],{"class":107},"30",[81,741,495],{"class":91},[81,743,744,747,749,752],{"class":83,"line":118},[81,745,746],{"class":91},"a[",[81,748,140],{"class":107},[81,750,751],{"class":91},"];                 ",[81,753,754],{"class":114},"\u002F\u002F 30 — last element, index length - 1\n",[81,756,757,760],{"class":83,"line":153},[81,758,759],{"class":114},"\u002F\u002F a[3];",[81,761,762],{"class":114},"              \u002F\u002F ArrayIndexOutOfBoundsException\n",[81,764,765,768],{"class":83,"line":182},[81,766,767],{"class":114},"\u002F\u002F a[-1];",[81,769,770],{"class":114},"             \u002F\u002F also AIOOBE — no negative indexing in Java\n",[81,772,773],{"class":83,"line":210},[81,774,546],{"emptyLinePlaceholder":545},[81,776,777,779,782,784,786,788,790,792,794],{"class":83,"line":219},[81,778,88],{"class":87},[81,780,781],{"class":91},"[] empty ",[81,783,95],{"class":87},[81,785,98],{"class":87},[81,787,101],{"class":87},[81,789,104],{"class":91},[81,791,680],{"class":107},[81,793,383],{"class":91},[81,795,796],{"class":114},"\u002F\u002F legal — empty array, length 0\n",[81,798,799,801,804,806,809,811],{"class":83,"line":571},[81,800,88],{"class":87},[81,802,803],{"class":91}," n ",[81,805,95],{"class":87},[81,807,808],{"class":87}," -",[81,810,135],{"class":107},[81,812,509],{"class":91},[81,814,815,818],{"class":83,"line":586},[81,816,817],{"class":114},"\u002F\u002F int[] bad = new int[n];",[81,819,820],{"class":114},"  \u002F\u002F NegativeArraySizeException at runtime\n",[15,822,823],{},"These runtime checks are what make Java memory-safe with arrays — you cannot walk off the\nend into arbitrary memory the way C lets you.",[10,825,827],{"id":826},"multidimensional-and-jagged-arrays","Multidimensional and jagged arrays",[15,829,830,831,834,835,838,839,842],{},"Java has no true 2D array; ",[19,832,833],{},"int[][]"," is an ",[59,836,837],{},"array of arrays",". The outer array holds\nreferences to inner arrays, each a separate heap object. Because the rows are independent,\nthey need not all be the same length — an array whose rows differ is called a ",[59,840,841],{},"jagged","\n(or ragged) array, and it is fully legal.",[72,844,846],{"className":74,"code":845,"language":76,"meta":77,"style":77},"int[][] grid = new int[2][3];     \u002F\u002F 2 rows, each an int[3]\ngrid.length;       \u002F\u002F 2 — number of rows\ngrid[0].length;    \u002F\u002F 3 — columns in row 0\n\nint[][] jagged = new int[3][];    \u002F\u002F 3 rows, none allocated yet (each null)\njagged[0] = new int[]{1};         \u002F\u002F length 1\njagged[1] = new int[]{2, 3};      \u002F\u002F length 2\njagged[2] = new int[]{4, 5, 6};   \u002F\u002F length 3\nfor (int[] row : jagged)          \u002F\u002F each row is itself an int[]\n  System.out.println(row.length); \u002F\u002F 1, 2, 3\n",[19,847,848,876,884,897,901,924,949,977,1009,1031],{"__ignoreMap":77},[81,849,850,852,855,857,859,861,863,865,868,870,873],{"class":83,"line":84},[81,851,88],{"class":87},[81,853,854],{"class":91},"[][] grid ",[81,856,95],{"class":87},[81,858,98],{"class":87},[81,860,101],{"class":87},[81,862,104],{"class":91},[81,864,140],{"class":107},[81,866,867],{"class":91},"][",[81,869,108],{"class":107},[81,871,872],{"class":91},"];     ",[81,874,875],{"class":114},"\u002F\u002F 2 rows, each an int[3]\n",[81,877,878,881],{"class":83,"line":118},[81,879,880],{"class":91},"grid.length;       ",[81,882,883],{"class":114},"\u002F\u002F 2 — number of rows\n",[81,885,886,889,891,894],{"class":83,"line":153},[81,887,888],{"class":91},"grid[",[81,890,680],{"class":107},[81,892,893],{"class":91},"].length;    ",[81,895,896],{"class":114},"\u002F\u002F 3 — columns in row 0\n",[81,898,899],{"class":83,"line":182},[81,900,546],{"emptyLinePlaceholder":545},[81,902,903,905,908,910,912,914,916,918,921],{"class":83,"line":210},[81,904,88],{"class":87},[81,906,907],{"class":91},"[][] jagged ",[81,909,95],{"class":87},[81,911,98],{"class":87},[81,913,101],{"class":87},[81,915,104],{"class":91},[81,917,108],{"class":107},[81,919,920],{"class":91},"][];    ",[81,922,923],{"class":114},"\u002F\u002F 3 rows, none allocated yet (each null)\n",[81,925,926,929,931,934,936,938,940,942,944,946],{"class":83,"line":219},[81,927,928],{"class":91},"jagged[",[81,930,680],{"class":107},[81,932,933],{"class":91},"] ",[81,935,95],{"class":87},[81,937,98],{"class":87},[81,939,101],{"class":87},[81,941,132],{"class":91},[81,943,135],{"class":107},[81,945,204],{"class":91},[81,947,948],{"class":114},"\u002F\u002F length 1\n",[81,950,951,953,955,957,959,961,963,965,967,969,971,974],{"class":83,"line":571},[81,952,928],{"class":91},[81,954,135],{"class":107},[81,956,933],{"class":91},[81,958,95],{"class":87},[81,960,98],{"class":87},[81,962,101],{"class":87},[81,964,132],{"class":91},[81,966,140],{"class":107},[81,968,22],{"class":91},[81,970,108],{"class":107},[81,972,973],{"class":91},"};      ",[81,975,976],{"class":114},"\u002F\u002F length 2\n",[81,978,979,981,983,985,987,989,991,993,995,997,999,1001,1003,1006],{"class":83,"line":586},[81,980,928],{"class":91},[81,982,140],{"class":107},[81,984,933],{"class":91},[81,986,95],{"class":87},[81,988,98],{"class":87},[81,990,101],{"class":87},[81,992,132],{"class":91},[81,994,233],{"class":107},[81,996,22],{"class":91},[81,998,238],{"class":107},[81,1000,22],{"class":91},[81,1002,243],{"class":107},[81,1004,1005],{"class":91},"};   ",[81,1007,1008],{"class":114},"\u002F\u002F length 3\n",[81,1010,1011,1014,1017,1019,1022,1025,1028],{"class":83,"line":591},[81,1012,1013],{"class":87},"for",[81,1015,1016],{"class":91}," (",[81,1018,88],{"class":87},[81,1020,1021],{"class":91},"[] row ",[81,1023,1024],{"class":87},":",[81,1026,1027],{"class":91}," jagged)          ",[81,1029,1030],{"class":114},"\u002F\u002F each row is itself an int[]\n",[81,1032,1033,1036,1038,1041],{"class":83,"line":622},[81,1034,1035],{"class":91},"  System.out.",[81,1037,408],{"class":407},[81,1039,1040],{"class":91},"(row.length); ",[81,1042,1043],{"class":114},"\u002F\u002F 1, 2, 3\n",[15,1045,1046,1047,1050,1051,1053,1054,1057,1058,452],{},"Always iterate inner rows with ",[19,1048,1049],{},"row.length"," rather than a hard-coded column count, since\nrows can differ and an unallocated row stays ",[19,1052,293],{},". Accessing ",[19,1055,1056],{},"grid[i][j]"," is two pointer\nhops — outer array to row, row to element — which is why dense numeric code sometimes\nflattens a 2D grid into a single ",[19,1059,1060],{},"int[]",[10,1062,1064],{"id":1063},"array-covariance-and-arraystoreexception","Array covariance and ArrayStoreException",[15,1066,1067,1068,1071,1072,1075,1076,1079,1080,1083,1084,1087,1088,1091,1092,1094,1095,1098,1099,1102,1103,1108],{},"Java arrays are ",[59,1069,1070],{},"covariant",": if ",[19,1073,1074],{},"Sub extends Super",", then ",[19,1077,1078],{},"Sub[]"," is a subtype of\n",[19,1081,1082],{},"Super[]",", so you can assign a ",[19,1085,1086],{},"String[]"," to an ",[19,1089,1090],{},"Object[]"," variable. Convenient — but\n",[31,1093,444],{}," type-safe at compile time. To keep that loophole from corrupting memory, the JVM\nchecks at ",[59,1096,1097],{},"runtime"," that every value you store matches the array's ",[31,1100,1101],{},"actual"," element\ntype, and throws ",[59,1104,1105],{},[19,1106,1107],{},"ArrayStoreException"," when it doesn't.",[72,1110,1112],{"className":74,"code":1111,"language":76,"meta":77,"style":77},"Object[] arr = new String[2];   \u002F\u002F legal — covariance; actual type is String[]\narr[0] = \"ok\";                  \u002F\u002F fine, it really is a String[]\n\u002F\u002F arr[1] = 42;                 \u002F\u002F compiles, but ArrayStoreException at runtime\n",[19,1113,1114,1135,1155],{"__ignoreMap":77},[81,1115,1116,1118,1120,1122,1124,1126,1128,1130,1132],{"class":83,"line":84},[81,1117,289],{"class":87},[81,1119,478],{"class":91},[81,1121,95],{"class":87},[81,1123,98],{"class":87},[81,1125,376],{"class":87},[81,1127,104],{"class":91},[81,1129,140],{"class":107},[81,1131,383],{"class":91},[81,1133,1134],{"class":114},"\u002F\u002F legal — covariance; actual type is String[]\n",[81,1136,1137,1140,1142,1144,1146,1149,1152],{"class":83,"line":118},[81,1138,1139],{"class":91},"arr[",[81,1141,680],{"class":107},[81,1143,933],{"class":91},[81,1145,95],{"class":87},[81,1147,1148],{"class":505}," \"ok\"",[81,1150,1151],{"class":91},";                  ",[81,1153,1154],{"class":114},"\u002F\u002F fine, it really is a String[]\n",[81,1156,1157,1160],{"class":83,"line":153},[81,1158,1159],{"class":114},"\u002F\u002F arr[1] = 42;",[81,1161,1162],{"class":114},"                 \u002F\u002F compiles, but ArrayStoreException at runtime\n",[15,1164,1165,1166,1169,1170,695,1173,1176,1177,1179,1180,1183,1184,1186],{},"Contrast this with ",[59,1167,1168],{},"generics",", which are deliberately ",[31,1171,1172],{},"invariant",[19,1174,1175],{},"List\u003CString>"," is\n",[59,1178,444],{}," a ",[19,1181,1182],{},"List\u003CObject>",". Generics chose compile-time safety, so no runtime store check is\nneeded; arrays chose flexibility and pay for it with ",[19,1185,1107],{},". Knowing why\nthe two differ is a frequent senior-level follow-up.",[10,1188,1190],{"id":1189},"the-arrays-utility-class-searching-comparing-printing","The Arrays utility class: searching, comparing, printing",[15,1192,1193,1196,1197,1200,1201,1204,1205,1208,1209,1212,1213,1216,1217,1220,1221,657,1224,1227,1228,1231,1232,1237],{},[19,1194,1195],{},"java.util.Arrays"," is the toolbox you reach for constantly. ",[19,1198,1199],{},"sort"," orders in place\n(primitive arrays use a dual-pivot quicksort; object arrays use a stable merge sort and\naccept a ",[19,1202,1203],{},"Comparator","), and ",[19,1206,1207],{},"parallelSort"," does the same across the common ForkJoinPool\nfor large arrays. ",[19,1210,1211],{},"binarySearch"," is O(log n) but ",[59,1214,1215],{},"requires a sorted array"," — on a miss\nit returns ",[19,1218,1219],{},"-(insertionPoint) - 1",". Crucially, the plain ",[19,1222,1223],{},"equals",[19,1225,1226],{},"toString"," are\n",[31,1229,1230],{},"shallow",", so nested arrays need the ",[59,1233,1234],{},[19,1235,1236],{},"deep"," variants.",[72,1239,1241],{"className":74,"code":1240,"language":76,"meta":77,"style":77},"int[] nums = {3, 1, 2};\nArrays.sort(nums);                       \u002F\u002F {1, 2, 3}\nString[] words = {\"banana\", \"apple\"};\nArrays.sort(words, Comparator.reverseOrder()); \u002F\u002F Comparator only on object arrays\n\nint idx = Arrays.binarySearch(nums, 2);  \u002F\u002F 1 (must be sorted first!)\nint miss = Arrays.binarySearch(nums, 5); \u002F\u002F negative; insert at -miss - 1\n\nint[][] m = {{1, 2}, {3, 4}}, n = {{1, 2}, {3, 4}};\nArrays.equals(m, n);      \u002F\u002F false — compares inner int[] REFERENCES\nArrays.deepEquals(m, n);  \u002F\u002F true  — recurses into rows\nArrays.toString(nums);    \u002F\u002F \"[1, 2, 3]\"   (vs nums.toString() -> \"[I@1b6d…\")\nArrays.deepToString(m);   \u002F\u002F \"[[1, 2], [3, 4]]\"\n",[19,1242,1243,1266,1279,1300,1317,1321,1345,1367,1371,1422,1434,1447,1460],{"__ignoreMap":77},[81,1244,1245,1247,1250,1252,1254,1256,1258,1260,1262,1264],{"class":83,"line":84},[81,1246,88],{"class":87},[81,1248,1249],{"class":91},"[] nums ",[81,1251,95],{"class":87},[81,1253,163],{"class":91},[81,1255,108],{"class":107},[81,1257,22],{"class":91},[81,1259,135],{"class":107},[81,1261,22],{"class":91},[81,1263,140],{"class":107},[81,1265,495],{"class":91},[81,1267,1268,1271,1273,1276],{"class":83,"line":118},[81,1269,1270],{"class":91},"Arrays.",[81,1272,1199],{"class":407},[81,1274,1275],{"class":91},"(nums);                       ",[81,1277,1278],{"class":114},"\u002F\u002F {1, 2, 3}\n",[81,1280,1281,1283,1286,1288,1290,1293,1295,1298],{"class":83,"line":153},[81,1282,366],{"class":87},[81,1284,1285],{"class":91},"[] words ",[81,1287,95],{"class":87},[81,1289,163],{"class":91},[81,1291,1292],{"class":505},"\"banana\"",[81,1294,22],{"class":91},[81,1296,1297],{"class":505},"\"apple\"",[81,1299,495],{"class":91},[81,1301,1302,1304,1306,1309,1312,1314],{"class":83,"line":182},[81,1303,1270],{"class":91},[81,1305,1199],{"class":407},[81,1307,1308],{"class":91},"(words, Comparator.",[81,1310,1311],{"class":407},"reverseOrder",[81,1313,423],{"class":91},[81,1315,1316],{"class":114},"\u002F\u002F Comparator only on object arrays\n",[81,1318,1319],{"class":83,"line":210},[81,1320,546],{"emptyLinePlaceholder":545},[81,1322,1323,1325,1328,1330,1332,1334,1337,1339,1342],{"class":83,"line":219},[81,1324,88],{"class":87},[81,1326,1327],{"class":91}," idx ",[81,1329,95],{"class":87},[81,1331,601],{"class":91},[81,1333,1211],{"class":407},[81,1335,1336],{"class":91},"(nums, ",[81,1338,140],{"class":107},[81,1340,1341],{"class":91},");  ",[81,1343,1344],{"class":114},"\u002F\u002F 1 (must be sorted first!)\n",[81,1346,1347,1349,1352,1354,1356,1358,1360,1362,1364],{"class":83,"line":571},[81,1348,88],{"class":87},[81,1350,1351],{"class":91}," miss ",[81,1353,95],{"class":87},[81,1355,601],{"class":91},[81,1357,1211],{"class":407},[81,1359,1336],{"class":91},[81,1361,238],{"class":107},[81,1363,616],{"class":91},[81,1365,1366],{"class":114},"\u002F\u002F negative; insert at -miss - 1\n",[81,1368,1369],{"class":83,"line":586},[81,1370,546],{"emptyLinePlaceholder":545},[81,1372,1373,1375,1378,1380,1383,1385,1387,1389,1392,1394,1396,1398,1401,1403,1405,1407,1409,1411,1413,1415,1417,1419],{"class":83,"line":591},[81,1374,88],{"class":87},[81,1376,1377],{"class":91},"[][] m ",[81,1379,95],{"class":87},[81,1381,1382],{"class":91}," {{",[81,1384,135],{"class":107},[81,1386,22],{"class":91},[81,1388,140],{"class":107},[81,1390,1391],{"class":91},"}, {",[81,1393,108],{"class":107},[81,1395,22],{"class":91},[81,1397,233],{"class":107},[81,1399,1400],{"class":91},"}}, n ",[81,1402,95],{"class":87},[81,1404,1382],{"class":91},[81,1406,135],{"class":107},[81,1408,22],{"class":91},[81,1410,140],{"class":107},[81,1412,1391],{"class":91},[81,1414,108],{"class":107},[81,1416,22],{"class":91},[81,1418,233],{"class":107},[81,1420,1421],{"class":91},"}};\n",[81,1423,1424,1426,1428,1431],{"class":83,"line":622},[81,1425,1270],{"class":91},[81,1427,1223],{"class":407},[81,1429,1430],{"class":91},"(m, n);      ",[81,1432,1433],{"class":114},"\u002F\u002F false — compares inner int[] REFERENCES\n",[81,1435,1436,1438,1441,1444],{"class":83,"line":631},[81,1437,1270],{"class":91},[81,1439,1440],{"class":407},"deepEquals",[81,1442,1443],{"class":91},"(m, n);  ",[81,1445,1446],{"class":114},"\u002F\u002F true  — recurses into rows\n",[81,1448,1450,1452,1454,1457],{"class":83,"line":1449},12,[81,1451,1270],{"class":91},[81,1453,1226],{"class":407},[81,1455,1456],{"class":91},"(nums);    ",[81,1458,1459],{"class":114},"\u002F\u002F \"[1, 2, 3]\"   (vs nums.toString() -> \"[I@1b6d…\")\n",[81,1461,1463,1465,1468,1471],{"class":83,"line":1462},13,[81,1464,1270],{"class":91},[81,1466,1467],{"class":407},"deepToString",[81,1469,1470],{"class":91},"(m);   ",[81,1472,1473],{"class":114},"\u002F\u002F \"[[1, 2], [3, 4]]\"\n",[15,1475,1476,1477,1480,1481,1483,1484,1487,1488,1491],{},"Two rules: ",[19,1478,1479],{},"Arrays.sort"," on primitives can't take a ",[19,1482,1203],{}," (box to ",[19,1485,1486],{},"Integer[]"," if\nyou need custom ordering of numbers), and use the ",[19,1489,1490],{},"deep*"," methods for any multidimensional\nor nested array — the shallow ones compare and print inner arrays by reference, which is\nalmost never what you want.",[10,1493,1495],{"id":1494},"copying-arrays-copyof-arraycopy-and-shallow-clone","Copying arrays: copyOf, arraycopy, and shallow clone",[15,1497,1498,1499,657,1502,1505,1506,1508,1509,1512,1513,1516,1517,1519,1520,1522,1523,1525,1526,1528,1529,1532],{},"There are three ways to copy. ",[19,1500,1501],{},"Arrays.copyOf",[19,1503,1504],{},"copyOfRange"," return a ",[59,1507,56],{}," array,\ntruncating or zero-padding as needed — ",[19,1510,1511],{},"copyOf(a, a.length + n)"," is the canonical \"grow\"\nidiom. ",[19,1514,1515],{},"System.arraycopy"," is a native bulk copy into an array you already allocated, and\nit's the fast path that ",[19,1518,604],{}," and ",[19,1521,21],{}," use internally. And ",[19,1524,300],{}," makes a\n",[59,1527,1230],{}," copy — fine for primitives, but for object or 2D arrays it copies the\n",[31,1530,1531],{},"references",", so both arrays share the same inner objects.",[72,1534,1536],{"className":74,"code":1535,"language":76,"meta":77,"style":77},"int[] a = {1, 2, 3};\nArrays.copyOf(a, 5);           \u002F\u002F {1, 2, 3, 0, 0} — padded with defaults\nArrays.copyOfRange(a, 1, 3);   \u002F\u002F {2, 3} — [from, to), to is exclusive\n\nint[] dest = new int[5];\nSystem.arraycopy(a, 0, dest, 1, 3); \u002F\u002F {0, 1, 2, 3, 0} — into existing array\n\nint[][] grid = {{1, 2}, {3, 4}};\nint[][] copy = grid.clone();   \u002F\u002F SHALLOW — rows are shared\ncopy[0][0] = 99;               \u002F\u002F grid[0][0] is now 99 too!\nfor (int i = 0; i \u003C grid.length; i++) copy[i] = grid[i].clone(); \u002F\u002F deep-copy rows\n",[19,1537,1538,1560,1577,1597,1601,1621,1647,1651,1677,1698,1722],{"__ignoreMap":77},[81,1539,1540,1542,1544,1546,1548,1550,1552,1554,1556,1558],{"class":83,"line":84},[81,1541,88],{"class":87},[81,1543,92],{"class":91},[81,1545,95],{"class":87},[81,1547,163],{"class":91},[81,1549,135],{"class":107},[81,1551,22],{"class":91},[81,1553,140],{"class":107},[81,1555,22],{"class":91},[81,1557,108],{"class":107},[81,1559,495],{"class":91},[81,1561,1562,1564,1566,1569,1571,1574],{"class":83,"line":118},[81,1563,1270],{"class":91},[81,1565,604],{"class":407},[81,1567,1568],{"class":91},"(a, ",[81,1570,238],{"class":107},[81,1572,1573],{"class":91},");           ",[81,1575,1576],{"class":114},"\u002F\u002F {1, 2, 3, 0, 0} — padded with defaults\n",[81,1578,1579,1581,1583,1585,1587,1589,1591,1594],{"class":83,"line":153},[81,1580,1270],{"class":91},[81,1582,1504],{"class":407},[81,1584,1568],{"class":91},[81,1586,135],{"class":107},[81,1588,22],{"class":91},[81,1590,108],{"class":107},[81,1592,1593],{"class":91},");   ",[81,1595,1596],{"class":114},"\u002F\u002F {2, 3} — [from, to), to is exclusive\n",[81,1598,1599],{"class":83,"line":182},[81,1600,546],{"emptyLinePlaceholder":545},[81,1602,1603,1605,1608,1610,1612,1614,1616,1618],{"class":83,"line":210},[81,1604,88],{"class":87},[81,1606,1607],{"class":91},"[] dest ",[81,1609,95],{"class":87},[81,1611,98],{"class":87},[81,1613,101],{"class":87},[81,1615,104],{"class":91},[81,1617,238],{"class":107},[81,1619,1620],{"class":91},"];\n",[81,1622,1623,1626,1629,1631,1633,1636,1638,1640,1642,1644],{"class":83,"line":219},[81,1624,1625],{"class":91},"System.",[81,1627,1628],{"class":407},"arraycopy",[81,1630,1568],{"class":91},[81,1632,680],{"class":107},[81,1634,1635],{"class":91},", dest, ",[81,1637,135],{"class":107},[81,1639,22],{"class":91},[81,1641,108],{"class":107},[81,1643,616],{"class":91},[81,1645,1646],{"class":114},"\u002F\u002F {0, 1, 2, 3, 0} — into existing array\n",[81,1648,1649],{"class":83,"line":571},[81,1650,546],{"emptyLinePlaceholder":545},[81,1652,1653,1655,1657,1659,1661,1663,1665,1667,1669,1671,1673,1675],{"class":83,"line":586},[81,1654,88],{"class":87},[81,1656,854],{"class":91},[81,1658,95],{"class":87},[81,1660,1382],{"class":91},[81,1662,135],{"class":107},[81,1664,22],{"class":91},[81,1666,140],{"class":107},[81,1668,1391],{"class":91},[81,1670,108],{"class":107},[81,1672,22],{"class":91},[81,1674,233],{"class":107},[81,1676,1421],{"class":91},[81,1678,1679,1681,1684,1686,1689,1692,1695],{"class":83,"line":591},[81,1680,88],{"class":87},[81,1682,1683],{"class":91},"[][] copy ",[81,1685,95],{"class":87},[81,1687,1688],{"class":91}," grid.",[81,1690,1691],{"class":407},"clone",[81,1693,1694],{"class":91},"();   ",[81,1696,1697],{"class":114},"\u002F\u002F SHALLOW — rows are shared\n",[81,1699,1700,1703,1705,1707,1709,1711,1713,1716,1719],{"class":83,"line":622},[81,1701,1702],{"class":91},"copy[",[81,1704,680],{"class":107},[81,1706,867],{"class":91},[81,1708,680],{"class":107},[81,1710,933],{"class":91},[81,1712,95],{"class":87},[81,1714,1715],{"class":107}," 99",[81,1717,1718],{"class":91},";               ",[81,1720,1721],{"class":114},"\u002F\u002F grid[0][0] is now 99 too!\n",[81,1723,1724,1726,1728,1730,1733,1735,1738,1741,1744,1747,1750,1753,1755,1758,1760,1763],{"class":83,"line":631},[81,1725,1013],{"class":87},[81,1727,1016],{"class":91},[81,1729,88],{"class":87},[81,1731,1732],{"class":91}," i ",[81,1734,95],{"class":87},[81,1736,1737],{"class":107}," 0",[81,1739,1740],{"class":91},"; i ",[81,1742,1743],{"class":87},"\u003C",[81,1745,1746],{"class":91}," grid.length; i",[81,1748,1749],{"class":87},"++",[81,1751,1752],{"class":91},") copy[i] ",[81,1754,95],{"class":87},[81,1756,1757],{"class":91}," grid[i].",[81,1759,1691],{"class":407},[81,1761,1762],{"class":91},"(); ",[81,1764,1765],{"class":114},"\u002F\u002F deep-copy rows\n",[15,1767,1768,1769,1771,1772,1774],{},"Reach for ",[19,1770,604],{}," when you just want a fresh array and let it allocate; use\n",[19,1773,1515],{}," when copying into a destination you already have or inserting a range;\nand remember that \"copy\" never means \"deep copy\" unless you clone each level yourself.",[10,1776,1778],{"id":1777},"bridging-arrays-lists-and-streams-and-the-aslist-trap","Bridging arrays, Lists, and Streams — and the asList trap",[15,1780,1781,1782,1785,1786,1788,1789,1792,1793,1796,1797,1800,1801,1803,1804,1807,1808,657,1811,1814,1815,1818,1819,1821,1822,1825,1826,1829,1830,1833,1834,1837],{},"Arrays convert cleanly to ",[19,1783,1784],{},"Stream","s and ",[19,1787,656],{},"s, but ",[19,1790,1791],{},"Arrays.asList"," hides two traps. It\nreturns a ",[59,1794,1795],{},"fixed-size list backed by the array"," — a ",[31,1798,1799],{},"view",", not an ",[19,1802,21],{}," — so\n",[19,1805,1806],{},"set"," works (and writes through to the array) while ",[19,1809,1810],{},"add",[19,1812,1813],{},"remove"," throw\n",[19,1816,1817],{},"UnsupportedOperationException",". Worse, passing a primitive ",[19,1820,1060],{}," gives a one-element\n",[19,1823,1824],{},"List\u003Cint[]>",", not a ",[19,1827,1828],{},"List\u003CInteger>",". For streaming, prefer ",[19,1831,1832],{},"Arrays.stream"," over\n",[19,1835,1836],{},"Stream.of"," for the same reason.",[72,1839,1841],{"className":74,"code":1840,"language":76,"meta":77,"style":77},"Integer[] arr = {1, 2, 3};\nList\u003CInteger> view = Arrays.asList(arr);\nview.set(0, 99);     \u002F\u002F OK — also changes arr[0]\n\u002F\u002F view.add(4);      \u002F\u002F UnsupportedOperationException — fixed size\nList\u003CInteger> real = new ArrayList\u003C>(Arrays.asList(arr)); \u002F\u002F mutable copy\n\nint[] prims = {1, 2, 3};\nIntStream is = Arrays.stream(prims);   \u002F\u002F IntStream — no boxing\nint[] doubled = is.map(x -> x * 2).toArray();\n\u002F\u002F Stream.of(prims) would give a Stream\u003Cint[]> of ONE element — wrong\nString[] back = real.stream().map(String::valueOf).toArray(String[]::new);\n",[19,1842,1843,1865,1884,1906,1914,1938,1942,1965,1983,2022,2027],{"__ignoreMap":77},[81,1844,1845,1847,1849,1851,1853,1855,1857,1859,1861,1863],{"class":83,"line":84},[81,1846,517],{"class":87},[81,1848,478],{"class":91},[81,1850,95],{"class":87},[81,1852,163],{"class":91},[81,1854,135],{"class":107},[81,1856,22],{"class":91},[81,1858,140],{"class":107},[81,1860,22],{"class":91},[81,1862,108],{"class":107},[81,1864,495],{"class":91},[81,1866,1867,1869,1871,1874,1876,1878,1881],{"class":83,"line":118},[81,1868,514],{"class":91},[81,1870,517],{"class":87},[81,1872,1873],{"class":91},"> view ",[81,1875,95],{"class":87},[81,1877,601],{"class":91},[81,1879,1880],{"class":407},"asList",[81,1882,1883],{"class":91},"(arr);\n",[81,1885,1886,1889,1891,1893,1895,1897,1900,1903],{"class":83,"line":153},[81,1887,1888],{"class":91},"view.",[81,1890,1806],{"class":407},[81,1892,531],{"class":91},[81,1894,680],{"class":107},[81,1896,22],{"class":91},[81,1898,1899],{"class":107},"99",[81,1901,1902],{"class":91},");     ",[81,1904,1905],{"class":114},"\u002F\u002F OK — also changes arr[0]\n",[81,1907,1908,1911],{"class":83,"line":182},[81,1909,1910],{"class":114},"\u002F\u002F view.add(4);",[81,1912,1913],{"class":114},"      \u002F\u002F UnsupportedOperationException — fixed size\n",[81,1915,1916,1918,1920,1923,1925,1927,1930,1932,1935],{"class":83,"line":210},[81,1917,514],{"class":91},[81,1919,517],{"class":87},[81,1921,1922],{"class":91},"> real ",[81,1924,95],{"class":87},[81,1926,98],{"class":87},[81,1928,1929],{"class":91}," ArrayList\u003C>(Arrays.",[81,1931,1880],{"class":407},[81,1933,1934],{"class":91},"(arr)); ",[81,1936,1937],{"class":114},"\u002F\u002F mutable copy\n",[81,1939,1940],{"class":83,"line":219},[81,1941,546],{"emptyLinePlaceholder":545},[81,1943,1944,1946,1949,1951,1953,1955,1957,1959,1961,1963],{"class":83,"line":571},[81,1945,88],{"class":87},[81,1947,1948],{"class":91},"[] prims ",[81,1950,95],{"class":87},[81,1952,163],{"class":91},[81,1954,135],{"class":107},[81,1956,22],{"class":91},[81,1958,140],{"class":107},[81,1960,22],{"class":91},[81,1962,108],{"class":107},[81,1964,495],{"class":91},[81,1966,1967,1970,1972,1974,1977,1980],{"class":83,"line":586},[81,1968,1969],{"class":91},"IntStream is ",[81,1971,95],{"class":87},[81,1973,601],{"class":91},[81,1975,1976],{"class":407},"stream",[81,1978,1979],{"class":91},"(prims);   ",[81,1981,1982],{"class":114},"\u002F\u002F IntStream — no boxing\n",[81,1984,1985,1987,1990,1992,1995,1998,2001,2004,2007,2010,2013,2016,2019],{"class":83,"line":591},[81,1986,88],{"class":87},[81,1988,1989],{"class":91},"[] doubled ",[81,1991,95],{"class":87},[81,1993,1994],{"class":91}," is.",[81,1996,1997],{"class":407},"map",[81,1999,2000],{"class":91},"(x ",[81,2002,2003],{"class":87},"->",[81,2005,2006],{"class":91}," x ",[81,2008,2009],{"class":87},"*",[81,2011,2012],{"class":107}," 2",[81,2014,2015],{"class":91},").",[81,2017,2018],{"class":407},"toArray",[81,2020,2021],{"class":91},"();\n",[81,2023,2024],{"class":83,"line":622},[81,2025,2026],{"class":114},"\u002F\u002F Stream.of(prims) would give a Stream\u003Cint[]> of ONE element — wrong\n",[81,2028,2029,2031,2034,2036,2039,2041,2043,2045,2048,2051,2054,2056,2058,2060,2063,2066],{"class":83,"line":631},[81,2030,366],{"class":87},[81,2032,2033],{"class":91},"[] back ",[81,2035,95],{"class":87},[81,2037,2038],{"class":91}," real.",[81,2040,1976],{"class":407},[81,2042,417],{"class":91},[81,2044,1997],{"class":407},[81,2046,2047],{"class":91},"(String",[81,2049,2050],{"class":87},"::",[81,2052,2053],{"class":91},"valueOf).",[81,2055,2018],{"class":407},[81,2057,531],{"class":91},[81,2059,366],{"class":87},[81,2061,2062],{"class":91},"[]",[81,2064,2065],{"class":87},"::new",[81,2067,540],{"class":91},[15,2069,2070,2071,2074,2075,2078,2079,2082,2083,452],{},"When you need a genuinely resizable list, wrap with ",[19,2072,2073],{},"new ArrayList\u003C>(...)","; and use\n",[19,2076,2077],{},"Arrays.stream(int[])"," to get a primitive ",[19,2080,2081],{},"IntStream"," rather than a boxed\n",[19,2084,2085],{},"Stream\u003CInteger>",[10,2087,2089],{"id":2088},"varargs-are-just-arrays","Varargs are just arrays",[15,2091,2092,2093,2096,2097,2100,2101,2104,2105,2108,2109,2112],{},"A varargs parameter (",[19,2094,2095],{},"Type... args",") ",[59,2098,2099],{},"is an array"," — the compiler packages the supplied\narguments into a ",[19,2102,2103],{},"Type[]",". Inside the method it behaves like any array, with ",[19,2106,2107],{},".length"," and\nindexing, which is why varargs must be the ",[31,2110,2111],{},"last"," parameter and why an existing array can\nbe passed straight through.",[72,2114,2116],{"className":74,"code":2115,"language":76,"meta":77,"style":77},"static int sum(int... nums) {       \u002F\u002F nums is really an int[]\n  int total = 0;\n  for (int n : nums) total += n;    \u002F\u002F iterate like any array\n  return total;\n}\nsum(1, 2, 3);          \u002F\u002F compiler builds new int[]{1, 2, 3}\nsum(new int[]{1, 2});  \u002F\u002F an existing array works directly\nsum();                 \u002F\u002F empty array, length 0 — never null\n",[19,2117,2118,2138,2152,2177,2185,2190,2213,2237],{"__ignoreMap":77},[81,2119,2120,2123,2125,2128,2130,2132,2135],{"class":83,"line":84},[81,2121,2122],{"class":87},"static",[81,2124,101],{"class":87},[81,2126,2127],{"class":407}," sum",[81,2129,531],{"class":91},[81,2131,88],{"class":87},[81,2133,2134],{"class":91},"... nums) {       ",[81,2136,2137],{"class":114},"\u002F\u002F nums is really an int[]\n",[81,2139,2140,2143,2146,2148,2150],{"class":83,"line":118},[81,2141,2142],{"class":87},"  int",[81,2144,2145],{"class":91}," total ",[81,2147,95],{"class":87},[81,2149,1737],{"class":107},[81,2151,509],{"class":91},[81,2153,2154,2157,2159,2161,2163,2165,2168,2171,2174],{"class":83,"line":153},[81,2155,2156],{"class":87},"  for",[81,2158,1016],{"class":91},[81,2160,88],{"class":87},[81,2162,803],{"class":91},[81,2164,1024],{"class":87},[81,2166,2167],{"class":91}," nums) total ",[81,2169,2170],{"class":87},"+=",[81,2172,2173],{"class":91}," n;    ",[81,2175,2176],{"class":114},"\u002F\u002F iterate like any array\n",[81,2178,2179,2182],{"class":83,"line":182},[81,2180,2181],{"class":87},"  return",[81,2183,2184],{"class":91}," total;\n",[81,2186,2187],{"class":83,"line":210},[81,2188,2189],{"class":91},"}\n",[81,2191,2192,2195,2197,2199,2201,2203,2205,2207,2210],{"class":83,"line":219},[81,2193,2194],{"class":407},"sum",[81,2196,531],{"class":91},[81,2198,135],{"class":107},[81,2200,22],{"class":91},[81,2202,140],{"class":107},[81,2204,22],{"class":91},[81,2206,108],{"class":107},[81,2208,2209],{"class":91},");          ",[81,2211,2212],{"class":114},"\u002F\u002F compiler builds new int[]{1, 2, 3}\n",[81,2214,2215,2217,2219,2221,2223,2225,2227,2229,2231,2234],{"class":83,"line":571},[81,2216,2194],{"class":407},[81,2218,531],{"class":91},[81,2220,56],{"class":87},[81,2222,101],{"class":87},[81,2224,132],{"class":91},[81,2226,135],{"class":107},[81,2228,22],{"class":91},[81,2230,140],{"class":107},[81,2232,2233],{"class":91},"});  ",[81,2235,2236],{"class":114},"\u002F\u002F an existing array works directly\n",[81,2238,2239,2241,2244],{"class":83,"line":586},[81,2240,2194],{"class":407},[81,2242,2243],{"class":91},"();                 ",[81,2245,2246],{"class":114},"\u002F\u002F empty array, length 0 — never null\n",[15,2248,2249,2250,2253,2254,2256,2257,2259,2260,2262,2263,2265],{},"This connection explains the earlier ",[19,2251,2252],{},"Arrays.asList(1, 2, 3)"," behaviour: ",[19,2255,1880],{}," is itself\na varargs method, so the boxed ",[19,2258,517],{}," literals collapse into an ",[19,2261,1486],{}," while a raw\n",[19,2264,1060],{}," becomes a single element.",[10,2267,2269],{"id":2268},"array-vs-arraylist-when-to-choose-which","Array vs ArrayList: when to choose which",[15,2271,2272,2273,2275,2276,2279,2280,2283,2284,2287,2288,2290,2291,2293,2294,2297,2298,657,2301,657,2303,2305],{},"Arrays and ",[19,2274,21],{}," solve overlapping problems with different trade-offs. An array is\n",[59,2277,2278],{},"fixed size",", holds primitives ",[31,2281,2282],{},"or"," objects with no boxing, exposes ",[19,2285,2286],{},"a[i]"," and the\n",[19,2289,2107],{}," field, and is covariant (runtime-checked). An ",[19,2292,21],{}," is ",[59,2295,2296],{},"dynamic",", holds\nobjects only (boxing primitives), uses ",[19,2299,2300],{},"get",[19,2302,1806],{},[19,2304,667],{},", and is generic and invariant\n(compile-time checked) — at the cost of boxing and occasional resize.",[72,2307,2309],{"className":74,"code":2308,"language":76,"meta":77,"style":77},"int[] arr = new int[3];                 \u002F\u002F fixed, primitive, fast, no boxing\nList\u003CInteger> list = new ArrayList\u003C>(); \u002F\u002F resizable, boxes ints\nlist.add(1); list.add(2);               \u002F\u002F grows automatically — backed by an array\n",[19,2310,2311,2332,2350],{"__ignoreMap":77},[81,2312,2313,2315,2317,2319,2321,2323,2325,2327,2329],{"class":83,"line":84},[81,2314,88],{"class":87},[81,2316,478],{"class":91},[81,2318,95],{"class":87},[81,2320,98],{"class":87},[81,2322,101],{"class":87},[81,2324,104],{"class":91},[81,2326,108],{"class":107},[81,2328,751],{"class":91},[81,2330,2331],{"class":114},"\u002F\u002F fixed, primitive, fast, no boxing\n",[81,2333,2334,2336,2338,2340,2342,2344,2347],{"class":83,"line":118},[81,2335,514],{"class":91},[81,2337,517],{"class":87},[81,2339,520],{"class":91},[81,2341,95],{"class":87},[81,2343,98],{"class":87},[81,2345,2346],{"class":91}," ArrayList\u003C>(); ",[81,2348,2349],{"class":114},"\u002F\u002F resizable, boxes ints\n",[81,2351,2352,2354,2356,2358,2360,2363,2365,2367,2369,2372],{"class":83,"line":153},[81,2353,574],{"class":91},[81,2355,1810],{"class":407},[81,2357,531],{"class":91},[81,2359,135],{"class":107},[81,2361,2362],{"class":91},"); list.",[81,2364,1810],{"class":407},[81,2366,531],{"class":91},[81,2368,140],{"class":107},[81,2370,2371],{"class":91},");               ",[81,2373,2374],{"class":114},"\u002F\u002F grows automatically — backed by an array\n",[15,2376,2377,2378,2380,2381,2385,2386,2388,2389,2391],{},"Choose an ",[59,2379,440],{}," for fixed-size, primitive-heavy, performance-critical code; choose an\n",[59,2382,2383],{},[19,2384,21],{}," when the size varies or you want the rich ",[19,2387,656],{}," API. It's worth saying out\nloud in an interview that ",[19,2390,21],{}," is itself backed by a resizing array — the two aren't\nrivals so much as different layers of the same idea.",[10,2393,2395],{"id":2394},"recap","Recap",[15,2397,2398,2399,2401,2402,2405,2406,2409,2410,2412,2413,1519,2415,2417,2418,2420,2421,2423,2424,2426,2427,2429,2430,2434,2435,2439,2440,657,2442,2444,2445,657,2447,2449,2450,2454,2455,657,2457,2459,2460,2462,2463,2466,2467,2469,2470,2472],{},"A Java array is an ",[59,2400,273],{}," with a ",[59,2403,2404],{},"fixed length"," decided at creation and\nslots that are ",[59,2407,2408],{},"zero-initialized"," by default. Keep the trio straight — ",[19,2411,562],{}," is a\nfield, ",[19,2414,650],{},[19,2416,667],{}," are methods. Multidimensional arrays are arrays of arrays,\nwhich makes ",[59,2419,841],{}," rows natural and means ",[19,2422,300],{}," is only ever ",[59,2425,1230],{},". Arrays\nare ",[59,2428,1070],{},", so an incompatible store throws ",[59,2431,2432],{},[19,2433,1107],{}," at runtime —\nthe price generics avoid by being invariant. Lean on the ",[59,2436,2437],{},[19,2438,41],{}," class for\n",[19,2441,1199],{},[19,2443,1211],{}," (sort first!), ",[19,2446,1223],{},[19,2448,1226],{}," for flat arrays and the ",[59,2451,2452],{},[19,2453,1236],{},"\nvariants for nested ones, and ",[19,2456,604],{},[19,2458,1515],{}," for copying. Mind the\n",[19,2461,1791],{}," traps, remember ",[59,2464,2465],{},"varargs are arrays",", and reach for ",[19,2468,21],{}," whenever\nthe size needs to change. Rule of thumb: use a raw array for fixed, primitive, hot-path\ndata and an ",[19,2471,21],{}," for everything that grows.",[2474,2475,2476],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":77,"searchDepth":118,"depth":118,"links":2478},[2479,2480,2481,2482,2483,2484,2485,2486,2487,2488,2489,2490,2491],{"id":12,"depth":118,"text":13},{"id":45,"depth":118,"text":46},{"id":266,"depth":118,"text":267},{"id":455,"depth":118,"text":456},{"id":673,"depth":118,"text":674},{"id":826,"depth":118,"text":827},{"id":1063,"depth":118,"text":1064},{"id":1189,"depth":118,"text":1190},{"id":1494,"depth":118,"text":1495},{"id":1777,"depth":118,"text":1778},{"id":2088,"depth":118,"text":2089},{"id":2268,"depth":118,"text":2269},{"id":2394,"depth":118,"text":2395},"A practical guide to Java arrays — declaring and initializing them, default values and fixed length, length vs length() vs size(), jagged arrays, array covariance and ArrayStoreException, the Arrays utility class, copying, and array vs ArrayList.","easy","md","Java",{},"\u002Fblog\u002Fjava-arrays-declaration-utilities","\u002Fjava\u002Ffundamentals\u002Farrays",{"title":5,"description":2492},"blog\u002Fjava-arrays-declaration-utilities","Fundamentals","fundamentals","2026-06-20","roO4kgWUVmBXSQUrCtx5_JgFRwLTbeeuU-o-I5DsYqM",1782244090004]