Title: Java Array Example (with Video) - Examples Java Code Geeks - 2026
Open Graph Title: Java Array Example (with Video) - Examples Java Code Geeks - 2026
Description: Java array class of the java.util package contains several static methods that you can use to compare, sort, and search in arrays.
Open Graph Description: Java array class of the java.util package contains several static methods that you can use to compare, sort, and search in arrays.
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-array-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-array-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-array-example/"},"author":{"name":"Armando Flores","@id":"https://examples.javacodegeeks.com/#/schema/person/edd8d967774c2c27aac159acda5ca9fe"},"headline":"Java Array – java.util.Arrays Example (with Video)","datePublished":"2014-06-05T08:00:00+00:00","dateModified":"2022-07-06T10:39:56+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-array-example/"},"wordCount":805,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-array-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["Arrays"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-array-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-array-example/","url":"https://examples.javacodegeeks.com/java-array-example/","name":"Java Array Example (with Video) - Examples Java Code Geeks - 2026","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-array-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-array-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-06-05T08:00:00+00:00","dateModified":"2022-07-06T10:39:56+00:00","description":"Java array class of the java.util package contains several static methods that you can use to compare, sort, and search in arrays.","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-array-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-array-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-array-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-array-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"util","item":"https://examples.javacodegeeks.com/category/java-development/core-java/util/"},{"@type":"ListItem","position":5,"name":"Arrays","item":"https://examples.javacodegeeks.com/category/java-development/core-java/util/arrays/"},{"@type":"ListItem","position":6,"name":"Java Array – java.util.Arrays Example (with Video)"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/edd8d967774c2c27aac159acda5ca9fe","name":"Armando Flores","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2014/05/Armando-Flores-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2014/05/Armando-Flores-96x96.jpg","caption":"Armando Flores"},"description":"Armando graduated from from Electronics Engineer in the The Public University Of Puebla (BUAP). He also has a Masters degree in Computer Sciences from CINVESTAV. He has been using the Java language for Web Development for over a decade. He has been involved in a large number of projects focused on \"ad-hoc\" Web Application based on Java EE and Spring Framework.","sameAs":["http://www.javacodegeeks.com/"],"url":"https://examples.javacodegeeks.com/author/armando-flores/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"util","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/"}},{"@type":"ListItem","position":5,"item":{"name":"Arrays","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/arrays\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-06-05T11:00:00+03:00","datePublished":"2014-06-05T11:00:00+03:00","dateModified":"2022-07-06T13:39:56+03:00","headline":"Java Array – java.util.Arrays Example (with Video)","name":"Java Array – java.util.Arrays Example (with Video)","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/java-array-example\/","description":"In this example, we will explain the array definition and we will show the range of functionality provided by the Java arrays class: java.util.Arrays. This class of the java.util package contains seve","copyrightYear":"2014","articleSection":"Arrays","articleBody":"\nIn this example, we will explain the array definition and we will show the range of functionality provided by the Java arrays class: java.util.Arrays. This class of the java.util package contains several static methods that you can use to compare, sort, and search in arrays. In addition, you can use this class to assign a value to one or more elements of an array. This class is a member of the Collections Framework.\n\n\n\nAn array of integers (int[]) will be used as our base array in the following example to illustrate most of the methods provided by the java.util.Arrays class. But before diving into the practical examples, let us understand the different types that are available in the Java programming language.\n\n\n\nYou can also check this tutorial in the following video:\n\n\n\n Java Array Example How to use Arrays in Java - Video\n\n\n\nOne-Dimensional Arrays: The type that consists of the array type and name. The array type will determine the type of array and the elements will determine the type of element inside the array. Let us understand the declaration of One-D arrays with an example. 1 2 3 4 5 int[] arr1 \/\/ A one-dimensional array of integer elements. String[] arr2 \/\/ A one-dimensional array of string elements. float[] arr3 \/\/ A one-dimensional array of floating-point elements. Object[] arr4 \/\/ A one-dimensional array of object elements. This array can either consist of mixture of primitive type elements or secondary elements. Multi-Dimensional Arrays: It is an array of arrays where each element holds a reference of another array. These types of arrays are also called Jagged Arrays and are defined in the following manner. Let us understand the declaration of Two-D arrays with an example. 1 2 int[][] arry1 = new int[10][20]; \/\/a 2D array or matrix int[][][] arr2 = new int[10][20][10]; \/\/a 3D array Please note, these days the IT industry has obsolete the use of multi-dimensional arrays due to the extensive use of collections. Array Literals: These are used in a situation where the array size and variables are known. Let us understand the declaration of Array Literals with an example. 1 int[] intArray = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; \/\/ Declaring array literal In this, the array length is automatically determined by the number of elements.\n\n\n\n But let's examine the array definition, by using the following examples. \n\n\n\n1. Example of Java Arrays methods\n\n\n\nNow let us start with an example to understand the basic declaration and different methods in Arrays. But before digging deep let us look at different methods we will be using in this example.\n\n\n\n\n\n\n\n1.1 Arrays.toString() method\n\n\n\nThis method returns the String representation of the array enclosed in the square brackets ([]). Adjacent elements are separated by the comma character (i.e. a comma followed by a space). Let us understand this with a simple example.\n\n\n\nSnippet\n\n\n\n\n\n\n\n\n\n1\n2\n\n\n\nInteger[] integerArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 };\nSystem.out.printf(\"integerArray elements: %s\\n\", Arrays.toString(integerArray));\n\n\n\n\n\n\n\n\n\n\n1.2 Arrays.asList() method\n\n\n\nThis method returns a list backed by a given array. In other words, both the list and array refer to the same location. Let us understand this with a simple example.\n\n\n\nSnippet\n\n\n\n\n\n\n\n\n\n1\n\n\n\nList integerList = Arrays.asList(integerArray); \/\/ Returns a fixed-size list backed by the specified array.\n\n\n\n\n\n\n\n\n\n\n1.3 Arrays.sort() method\n\n\n\nThis method sorts the specified array into ascending numerical order. Let us understand this with a simple example.\n\n\n\nSnippet\n\n\n\n\n\n\n\n\n\n1\n\n\n\nArrays.sort(baseArray);\n\n\n\n\n\n\n\n\n\n\n1.4 Arrays.binarySearch() method\n\n\n\nThis method returns an integer value for the index of the specified key in the specified array. Return a negative number if the key is not found and for this method to work properly, the array must be sorted. Let us understand this with a simple example.\n\n\n\nSnippet\n\n\n\n\n\n\n\n\n\n1\n\n\n\nint idx = Arrays.binarySearch(baseArray, 21); \/\/ Searches the specified array of ints for the specified value using the binary search algorithm.\n\n\n\n\n\n\n\n\n\n\n1.5 Arrays.copyOf() method\n\n\n\nThis method copies the specified array, truncates or pads with zeros (if necessary) so the copy has the specified length. Let us understand this with a simple example.\n\n\n\nSnippet\n\n\n\n\n\n\n\n\n\n1\n\n\n\nint[] copyOfArray = Arrays.copyOf(baseArray, 11); \/\/ Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.\n\n\n\n\n\n\n\n\n\n\n1.6 Arrays.copyOfRange() method\n\n\n\nThis method copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. Let us understand this with a simple example.\n\n\n\nSnippet\n\n\n\n\n\n\n\n\n\n1\n\n\n\nint[] copyOfRangeArray = Arrays.copyOfRange(baseArray, 5, 8); \/\/ Copies the specified range of the specified array into a new array.\n\n\n\n\n\n\n\n\n\n\n1.7 Arrays.fill() method\n\n\n\nThis method fills all elements of the specified array with the specified value. Let us understand this with a simple example.\n\n\n\nSnippet\n\n\n\n\n\n\n\n\n\n1\n2\n3\n4\n\n\n\nint[] fillArray = new int[5]; \/\/ Assigns the specified int value to each element of the specified array of ints.\nSystem.out.printf(\"fillArray (before): %s\\n\", Arrays.toString(fillArray));\nArrays.fill(fillArray, 1);\nSystem.out.printf(\"fillArray (after): %s\", Arrays.toString(fillArray));\n\n\n\n\n\n\n\n\n\n\n1.8 Complete Example\n\n\n\nLet us consider the below example where we will illustrate all the methods explained above.\n\n\n\nJavaUtilArraysExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n\n\n\npackage com.javacodegeeks.examples;\n \nimport java.util.Arrays;\nimport java.util.List;\n \npublic class JavaUtilArraysExample {\n \n public static void main(String[] args) {\n \n Integer[] integerArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 }; \/\/ Base array for the example. It contains 9 elements.\n System.out.printf(\"integerArray size: %d\\n\", integerArray.length);\n System.out.printf(\"integerArray elements: %s\\n\", Arrays.toString(integerArray));\n \n List<Integer> integerList = Arrays.asList(integerArray); \/\/ Returns a fixed-size list backed by the specified array.\n System.out.printf(\"integerList size: %d\\n\", integerList.size());\n System.out.printf(\"integerList elements: \");\n for (Integer i : integerList) {\n System.out.printf(\"%d \", i);\n }\n System.out.printf(\"\\n\\n\");\n \n int[] baseArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 };\n System.out.printf(\"Unsorted baseArray: %s\\n\", Arrays.toString(baseArray));\n \n Arrays.sort(baseArray);\n System.out.printf(\"Sorted baseArray: %s\\n\", Arrays.toString(baseArray));\n \n int idx = Arrays.binarySearch(baseArray, 21); \/\/ Searches the specified array of ints for the specified value using the binary search algorithm.\n System.out.printf(\"Value \\\"21\\\" found at index: %d\\n\\n\", idx);\n \n System.out.printf(\"baseArray size: %d\\n\", baseArray.length);\n System.out.printf(\"baseArray elements: %s\\n\", Arrays.toString(baseArray));\n \n int[] copyOfArray = Arrays.copyOf(baseArray, 11); \/\/ Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.\n System.out.printf(\"copyOfArray size: %d\\n\", copyOfArray.length);\n System.out.printf(\"copyOfArray elements: %s\\n\\n\", Arrays.toString(copyOfArray));\n \n System.out.printf(\"baseArray: %s\\n\", Arrays.toString(baseArray)); \n int[] copyOfRangeArray = Arrays.copyOfRange(baseArray, 5, 8); \/\/ Copies the specified range of the specified array into a new array.\n System.out.printf(\"copyOfRangeArray: %s\\n\\n\", Arrays.toString(copyOfRangeArray));\n \n int[] fillArray = new int[5]; \/\/ Assigns the specified int value to each element of the specified array of ints.\n System.out.printf(\"fillArray (before): %s\\n\", Arrays.toString(fillArray));\n Arrays.fill(fillArray, 1);\n System.out.printf(\"fillArray (after): %s\", Arrays.toString(fillArray));\n }\n}\n\n\n\n\n\n\n\n\n\n\nIf everything goes well, we will get the following results in the console.\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n\n\n\nintegerArray size: 9\nintegerArray elements: [2, 4, 3, 7, 21, 9, 98, 76, 74]\nintegerList size: 9\nintegerList elements: 2 4 3 7 21 9 98 76 74 \nUnsorted baseArray: [2, 4, 3, 7, 21, 9, 98, 76, 74]\nSorted baseArray: [2, 3, 4, 7, 9, 21, 74, 76, 98]\nValue \"21\" found at index: 5\nbaseArray size: 9\nbaseArray elements: [2, 3, 4, 7, 9, 21, 74, 76, 98]\ncopyOfArray size: 11\ncopyOfArray elements: [2, 3, 4, 7, 9, 21, 74, 76, 98, 0, 0]\nbaseArray: [2, 3, 4, 7, 9, 21, 74, 76, 98]\ncopyOfRangeArray: [21, 74, 76]\nfillArray (before): [0, 0, 0, 0, 0]\nfillArray (after): [1, 1, 1, 1, 1]\n\n\n\n\n\n\n\n\n\n\n2. Some more methods of the Java Array class\n\n\n\nJava Arrays add some interesting method arguments to the existing sort() and fill() method i.e.\n\n\n\nArrays.sort(int[] a, int fromIndex, int endIndex): Sorts the specified range of the array into ascending order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is emptyArrays.fill(int[] a, int fromIndex, int endIndex): Fills elements of the specified array with the specified value from the fromIndex element, but not including the toIndex element\n\n\n\nLet us understand these modifications with an example.\n\n\n\nJavaUtilArraysMoreMethodsExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n\n\n\npackage com.javacodegeeks.examples;\nimport java.util.Arrays;\npublic class JavaUtilArraysMoreMethodsExample {\n public static void main(String[] args) {\n \/\/ Base array for the example. It contains 9 elements.\n int[] baseArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 };\n \n \/\/ Sorts the specified range of the array into ascending order.\n System.out.printf(\"Unsorted baseArray: %s\\n\", Arrays.toString(baseArray));\n \n Arrays.sort(baseArray, 1, 6);\n \n System.out.printf(\"Sorted baseArray: %s\\n\\n\", Arrays.toString(baseArray));\n \n \/\/ Assigns the specified int value to each element of the\n \/\/ specified range of the specified array of ints.\n int[] fillArray = new int[10]; \n \n System.out.printf(\"fillArray (before): %s\\n\", Arrays.toString(fillArray)); \n \n Arrays.fill(fillArray, 1, 7, 3);\n \n System.out.printf(\"fillArray (after): %s\", Arrays.toString(fillArray));\n }\n}\n\n\n\n\n\n\n\n\n\n\nIf everything goes well, we will get the following results in the console.\n\n\n\n\n\n\n\n\n\n1\n2\n3\n4\n\n\n\nUnsorted baseArray: [2, 4, 3, 7, 21, 9, 98, 76, 74]\nSorted baseArray: [2, 3, 4, 7, 9, 21, 98, 76, 74]\nfillArray (before): [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\nfillArray (after): [0, 3, 3, 3, 3, 3, 3, 0, 0, 0]\n\n\n\n\n\n\n\n\n\n\n3. More articles\n\n\n\nJava List ExampleJava Queue ExampleJava Stack ExampleArrayList Java Example \u2013 How to use arraylist\n\n\n\n4. Download the Source Code\n\n\n\nThat was an article explaining the array definition in Java through examples.\n\n\n\nDownloadYou can download the source code of this example from here: Java Array \u2013 java.util.Arrays Example\n\n\n\nLast updated on Jan. 25th, 2022\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-array-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Armando Flores","url":"https:\/\/examples.javacodegeeks.com\/author\/armando-flores\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-06-05T08:00:00+00:00 |
| article:modified_time | 2022-07-06T10:39:56+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Armando Flores |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Armando Flores |
| twitter:label2 | Est. reading time |
| twitter:data2 | 8 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1