{"id":5290,"date":"2021-03-23T17:02:51","date_gmt":"2021-03-23T17:02:51","guid":{"rendered":"https:\/\/ded9.com\/?p=5290"},"modified":"2025-10-25T09:46:00","modified_gmt":"2025-10-25T09:46:00","slug":"the-basics-of-arrays-in-java","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/the-basics-of-arrays-in-java\/","title":{"rendered":"Understanding Arrays in Java: A Beginner&#8217;s Guide"},"content":{"rendered":"<p><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><span style=\"font-size: 12pt;\">In this tutorial, you will learn how to work with\u00a0<\/span><span style=\"font-size: 12pt;\">arrays in Java:<\/span><span style=\"font-size: 12pt;\"> Define, initialize, and access elements with examples.<\/span><\/span><\/p>\n<p>An array is a container that holds data (values) of a single type. For example, you can create an array having 100 int values.<\/p>\n<p>An array is a basic structure in Java that allows you to store and access large amounts of values easily.<\/p>\n<h1><span style=\"font-size: 18pt;\">How to define an <a href=\"https:\/\/www.geeksforgeeks.org\/arrays-in-java\/\" target=\"_blank\" rel=\"noopener\">array<\/a>?<\/span><\/h1>\n<p>Here is how to define an array in <a href=\"https:\/\/ded9.com\/what-is-java-runtime-environment\/\">Java<\/a>:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">dataType [] arrayName;<\/pre>\n<\/div>\n<ul>\n<li>dataType can be a raw data type, such as int, char, Double, byte, etc., or an object<\/li>\n<li>arrayName is the identifier.<\/li>\n<\/ul>\n<p>Let us repeat the above example.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">double [] data;<\/pre>\n<\/div>\n<p>Here, data is an array that can hold values \u200b\u200bof type Double.<\/p>\n<h2><span style=\"font-size: 14pt;\">But how many elements can the data array hold?<\/span><\/h2>\n<p>That was a good question!\u00a0We have not defined it yet.\u00a0The next step is to allocate memory for the array elements.<\/p>\n<blockquote>\n<p dir=\"ltr\">data = new Double [10];<\/p>\n<\/blockquote>\n<p>The data array&#8217;s length is 10. This means it can hold 10 elements (in this case, 10 double values).<\/p>\n<p>Note that once the array length is defined, it cannot be changed during the program.<\/p>\n<p>Let&#8217;s take another example:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">int [] age;\r\n\r\nage = new int [5];<\/pre>\n<\/div>\n<p>Here, the age array can hold five values \u200b\u200bof type int.<\/p>\n<p>In one command, the memory of an array can be declared and allocated. You can replace the above two sentences with a single sentence.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">int [] age = new int [5];<\/pre>\n<\/div>\n<h1><span style=\"font-size: 18pt;\">Array Index in Java<\/span><\/h1>\n<p>You can access elements of an array using indexes.\u00a0Consider the previous example.<\/p>\n<blockquote>\n<p dir=\"ltr\">int [] age = new int [5];<\/p>\n<\/blockquote>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50104\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-java-array-elements-jpg.jpeg\" alt=\"Array Index in Java\" width=\"600\" height=\"158\" data-ll-status=\"loaded\" \/><\/p>\n<p>The first element of the array is [age [0]], the second element is [age [1], and &#8230;<\/p>\n<p>If the array length is n, the last element will be [arrayName] n-1.\u00a0Since the length of the age array is 5, the last element of the array in the example above is age.<\/p>\n<p>The default initial value of the elements of an array is 0 for all numeric types and false for booleans. Consider the following example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class ArrayExample {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int [] age = new int [5];<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">System.out.println (age [0]);<\/li>\n<li dir=\"ltr\">System.out.println (age [1]);<\/li>\n<li dir=\"ltr\">System.out.println (age [2]);<\/li>\n<li dir=\"ltr\">System.out.println (age [3]);<\/li>\n<li dir=\"ltr\">System.out.println (age [4]);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h3>Output<\/h3>\n<blockquote>\n<p dir=\"ltr\">\u06f0<\/p>\n<p dir=\"ltr\">\u06f0<\/p>\n<p dir=\"ltr\">\u06f0<\/p>\n<p dir=\"ltr\">\u06f0<\/p>\n<p dir=\"ltr\">\u06f0<\/p>\n<\/blockquote>\n<h4>There is a better way to access the elements of an array using a loop structure (usually, a for loop is used).<\/h4>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class ArrayExample {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int [] age = new int [5];<\/li>\n<li dir=\"ltr\">for (int i = 0; i &lt;5; ++ i) {<\/li>\n<li dir=\"ltr\">System.out.println (age [i]);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h1><span style=\"font-size: 18pt;\">How do I initialize arrays in Java?<\/span><\/h1>\n<p>In Java, you can set arrays when defining or later define or modify values according to your needs.<\/p>\n<h2><span style=\"font-size: 14pt;\"><strong>Initialize the array when defining<\/strong><\/span><\/h2>\n<p>Here&#8217;s how to set an array when defining.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">int [] age = {12, 4, 5, 2, 5};<\/pre>\n<\/div>\n<p>This expression creates an array and values \u200b\u200bit when defining it.<\/p>\n<p>The array&#8217;s length is determined by the number of values \u200b\u200bseparated by commas. In this example, it is 5.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-50105\" title=\"Arrays\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-initialize-array-during-declar.jpeg\" alt=\"This expression creates an array and values \u200b\u200bit when defining it.\" width=\"380\" height=\"94\" data-ll-status=\"loaded\" \/><\/p>\n<h3>Let&#8217;s write a simple program to print the elements of this array.<\/h3>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class ArrayExample {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int [] age = {12, 4, 5, 2, 5};<\/li>\n<li dir=\"ltr\">for (int i = 0; i &lt;5; ++ i) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cElement at index\u201d + i + \u201d:\u201d + age [i]);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>The output of the above code is equal to:<\/p>\n<blockquote>\n<p dir=\"ltr\">Element at index 0:12<\/p>\n<p dir=\"ltr\">Element at index 1: 4<\/p>\n<p dir=\"ltr\">Element at index 2: 5<\/p>\n<p dir=\"ltr\">Element at index 3: 2<\/p>\n<p dir=\"ltr\">Element at index 4: 5<\/p>\n<\/blockquote>\n<h1><span style=\"font-size: 18pt;\">How to access array elements?<\/span><\/h1>\n<p>Using its numeric list, you can easily access and modify array elements.\u00a0Let&#8217;s give an example.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class ArrayExample {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int [] age = new int [5];<\/li>\n<li dir=\"ltr\">\/\/ insert 14 to third element<\/li>\n<li dir=\"ltr\">age [2] = 14;<\/li>\n<li dir=\"ltr\">\/\/ insert 34 to first element<\/li>\n<li dir=\"ltr\">age [0] = 34;<\/li>\n<li dir=\"ltr\">for (int i = 0; i &lt;5; ++ i) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cElement at index\u201d + i + \u201d:\u201d + age [i]);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4><strong>Output<\/strong><\/h4>\n<blockquote>\n<p dir=\"ltr\">Element at index 0:34<\/p>\n<p dir=\"ltr\">Element at index 1: 0<\/p>\n<p dir=\"ltr\">Element at index 2:14<\/p>\n<p dir=\"ltr\">Element at index 3: 0<\/p>\n<p dir=\"ltr\">Element at index 4: 0<\/p>\n<\/blockquote>\n<h3><strong>Example: Arrays in Java<\/strong><\/h3>\n<p>The following program calculates the sum and average of the values \u200b\u200bstored in an int array.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class SumAverage {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int [] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};<\/li>\n<li dir=\"ltr\">int sum = 0;<\/li>\n<li dir=\"ltr\">Double average;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">for (int number: numbers) {<\/li>\n<li dir=\"ltr\">sum + = number;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int arrayLength = numbers.length;<\/li>\n<li dir=\"ltr\">\/\/ Change sum and arrayLength to double as average is in double<\/li>\n<li dir=\"ltr\">average = ((double) sum \/ (double) arrayLength);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cSum =\u201d + sum);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cAverage =\u201d + average);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h3><strong>Output<\/strong><\/h3>\n<blockquote>\n<p dir=\"ltr\">Sum = 36<\/p>\n<p dir=\"ltr\">Average = 3.6<\/p>\n<\/blockquote>\n<ul>\n<li>Each loop is used to access each array element.<\/li>\n<li>To calculate the mean, the sum of the integers and the array length are doubled because the mean is double. This is called data type conversion.<\/li>\n<li>The length property is used to get the array length. Here, numbers. Length returns the length of the array of numbers.<\/li>\n<\/ul>\n<h2>Multidimensional arrays<\/h2>\n<p>The arrays we have mentioned so far are called one-dimensional arrays. In Java, you can define an array of multidimensional arrays. Here is an example of how to define and initialize a multidimensional array.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Double [] [] matrix = {{1.2, 4.3, 4.0},\r\n\r\n{4 \u066b 1, -1 \u066b 1}\r\n\r\n};<\/pre>\n<\/div>\n<p>Here, the matrix is \u200b\u200ba 2D array.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is an array in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>An array in Java is a data structure that stores multiple values of the same type in a single variable, accessible via indices.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you declare an array in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the syntax dataType[] arrayName; or dataType arrayName[]; followed by initialization, e.g., arrayName = new dataType[size];.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can arrays in Java hold different data types?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No, arrays in Java are homogeneous; they can only store elements of the same data type.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to work with\u00a0arrays in Java: Define, initialize, and access elements with examples. An array is a container that holds data (values) of a single type. For example, you can create an array having 100 int values. An array is a basic structure in Java that allows you to [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5291,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5290"}],"version-history":[{"count":6,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5290\/revisions"}],"predecessor-version":[{"id":263899,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5290\/revisions\/263899"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/5291"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}