logo

Java-Arrays

Normalerweise ist ein Array eine Sammlung ähnlicher Elementtypen mit zusammenhängendem Speicherort.

Java-Array ist ein Objekt, das Elemente eines ähnlichen Datentyps enthält. Darüber hinaus werden die Elemente eines Arrays an einem zusammenhängenden Speicherort gespeichert. Es handelt sich um eine Datenstruktur, in der wir ähnliche Elemente speichern. Wir können nur einen festen Satz von Elementen in einem Java-Array speichern.

Array in Java ist indexbasiert, das erste Element des Arrays wird am 0. Index gespeichert, das 2. Element wird am 1. Index gespeichert und so weiter.

Im Gegensatz zu C/C++ können wir die Länge des Arrays mithilfe des Längenelements ermitteln. In C/C++ müssen wir den Operator sizeof verwenden.

In Java ist ein Array ein Objekt einer dynamisch generierten Klasse. Das Java-Array erbt die Object-Klasse und implementiert die serialisierbaren und klonbaren Schnittstellen. Wir können in Java primitive Werte oder Objekte in einem Array speichern. Wie C/C++ können wir auch in Java ein- oder mehrdimensionale Arrays erstellen.

Darüber hinaus bietet Java die Funktion anonymer Arrays, die in C/C++ nicht verfügbar ist.

Java-Array

Vorteile

    Code-Optimierung:Dadurch wird der Code optimiert, wir können die Daten effizient abrufen oder sortieren.Zufälliger Zugriff:Wir können alle Daten abrufen, die sich an einer Indexposition befinden.

Nachteile

    Größenbeschränkung:Wir können nur die feste Größe von Elementen im Array speichern. Zur Laufzeit vergrößert es sich nicht. Um dieses Problem zu lösen, wird in Java ein Sammlungsframework verwendet, das automatisch wächst.

Arten von Arrays in Java

Es gibt zwei Arten von Arrays.

  • Eindimensionales Array
  • Mehrdimensionales Array

Eindimensionales Array in Java

Syntax zum Deklarieren eines Arrays in Java

aws Rotverschiebung
 dataType[] arr; (or) dataType []arr; (or) dataType arr[]; 

Instanziierung eines Arrays in Java

 arrayRefVar=new datatype[size]; 

Beispiel eines Java-Arrays

Sehen wir uns das einfache Beispiel eines Java-Arrays an, in dem wir ein Array deklarieren, instanziieren, initialisieren und durchlaufen werden.

 //Java Program to illustrate how to declare, instantiate, initialize //and traverse the Java array. class Testarray{ public static void main(String args[]){ int a[]=new int[5];//declaration and instantiation a[0]=10;//initialization a[1]=20; a[2]=70; a[3]=40; a[4]=50; //traversing array for(int i=0;i <a.length;i++) length is the property of array system.out.println(a[i]); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 10 20 70 40 50 </pre> <hr> <h2>Declaration, Instantiation and Initialization of Java Array</h2> <p>We can declare, instantiate and initialize the java array together by:</p> <pre> int a[]={33,3,4,5};//declaration, instantiation and initialization </pre> <p>Let&apos;s see the simple example to print this array.</p> <pre> //Java Program to illustrate the use of declaration, instantiation //and initialization of Java array in a single line class Testarray1{ public static void main(String args[]){ int a[]={33,3,4,5};//declaration, instantiation and initialization //printing array for(int i=0;i <a.length;i++) length is the property of array system.out.println(a[i]); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 33 3 4 5 </pre> <h2>For-each Loop for Java Array</h2> <p>We can also print the Java array using <strong> <a href="/java-each-loop-enhanced">for-each loop</a> </strong> . The Java for-each loop prints the array elements one by one. It holds an array element in a variable, then executes the body of the loop.</p> <p>The syntax of the for-each loop is given below:</p> <pre> for(data_type variable:array){ //body of the loop } </pre> <p>Let us see the example of print the elements of Java array using the for-each loop.</p> <pre> //Java Program to print the array elements using for-each loop class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; //printing array using for-each loop for(int i:arr) System.out.println(i); }} </pre> <p>Output:</p> <pre> 33 3 4 5 </pre> <hr> <h2>Passing Array to a Method in Java</h2> <p>We can pass the java array to method so that we can reuse the same logic on any array.</p> <p>Let&apos;s see the simple example to get the minimum number of an array using a method.</p> <pre> //Java Program to demonstrate the way of passing an array //to method. class Testarray2{ //creating a method which receives an array as a parameter static void min(int arr[]){ int min=arr[0]; for(int i=1;iarr[i]) min=arr[i]; System.out.println(min); } public static void main(String args[]){ int a[]={33,3,4,5};//declaring and initializing an array min(a);//passing array to method }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> 3 </pre> <h2>Anonymous Array in Java</h2> <p>Java supports the feature of an anonymous array, so you don&apos;t need to declare the array while passing an array to the method.</p> <pre> //Java Program to demonstrate the way of passing an anonymous array //to method. public class TestAnonymousArray{ //creating a method which receives an array as a parameter static void printArray(int arr[]){ for(int i=0;i <arr.length;i++) system.out.println(arr[i]); } public static void main(string args[]){ printarray(new int[]{10,22,44,66}); passing anonymous array to method }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 10 22 44 66 </pre> <h2>Returning Array from the Method</h2> <p>We can also return an array from the method in Java.</p> <pre> //Java Program to return an array from the method class TestReturnArray{ //creating method which returns an array static int[] get(){ return new int[]{10,30,50,90,60}; } public static void main(String args[]){ //calling method which returns an array int arr[]=get(); //printing the values of an array for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 10 30 50 90 60 </pre> <h2>ArrayIndexOutOfBoundsException</h2> <p>The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if length of the array in negative, equal to the array size or greater than the array size while traversing the array.</p> <pre> //Java Program to demonstrate the case of //ArrayIndexOutOfBoundsException in a Java Array. public class TestArrayException{ public static void main(String args[]){ int arr[]={50,60,70,80}; for(int i=0;i<=arr.length;i++){ system.out.println(arr[i]); } }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> Exception in thread &apos;main&apos; java.lang.ArrayIndexOutOfBoundsException: 4 at TestArrayException.main(TestArrayException.java:5) 50 60 70 80 </pre> <hr> <h2>Multidimensional Array in Java</h2> <p>In such case, data is stored in row and column based index (also known as matrix form).</p> <p> <strong>Syntax to Declare Multidimensional Array in Java</strong> </p> <pre> dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; </pre> <p> <strong>Example to instantiate Multidimensional Array in Java</strong> </p> <pre> int[][] arr=new int[3][3];//3 row and 3 column </pre> <p> <strong>Example to initialize Multidimensional Array in Java</strong> </p> <pre> arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; </pre> <h3>Example of Multidimensional Java Array</h3> <p>Let&apos;s see the simple example to declare, instantiate, initialize and print the 2Dimensional array.</p> <pre> //Java Program to illustrate the use of multidimensional array class Testarray3{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" system.out.print(arr[i][j]+' '); } system.out.println(); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 1 2 3 2 4 5 4 4 5 </pre> <h2>Jagged Array in Java</h2> <p>If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is an array of arrays with different number of columns.</p> <pre> //Java Program to illustrate the jagged array class TestJaggedArray{ public static void main(String[] args){ //declaring a 2D array with odd columns int arr[][] = new int[3][]; arr[0] = new int[3]; arr[1] = new int[4]; arr[2] = new int[2]; //initializing a jagged array int count = 0; for (int i=0; i <arr.length; i++) for(int j="0;" <arr[i].length; j++) arr[i][j]="count++;" printing the data of a jagged array for (int i="0;" <arr.length; i++){ j++){ system.out.print(arr[i][j]+' '); } system.out.println(); new line < pre> <span> Test it Now </span> <p>Output:</p> <pre> 0 1 2 3 4 5 6 7 8 </pre> <hr> <h2>What is the class name of Java array?</h2> <p>In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.</p> <pre> //Java Program to get the class name of array in Java class Testarray4{ public static void main(String args[]){ //declaration and initialization of array int arr[]={4,4,5}; //getting the class name of Java array Class c=arr.getClass(); String name=c.getName(); //printing the class name of Java array System.out.println(name); }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> I </pre> <hr> <h2>Copying a Java Array</h2> <p>We can copy an array to another by the arraycopy() method of System class.</p> <p> <strong>Syntax of arraycopy method</strong> </p> <pre> public static void arraycopy( Object src, int srcPos,Object dest, int destPos, int length ) </pre> <h3>Example of Copying an Array in Java</h3> <pre> //Java Program to copy a source array into a destination array in Java class TestArrayCopyDemo { public static void main(String[] args) { //declaring a source array char[] copyFrom = { &apos;d&apos;, &apos;e&apos;, &apos;c&apos;, &apos;a&apos;, &apos;f&apos;, &apos;f&apos;, &apos;e&apos;, &apos;i&apos;, &apos;n&apos;, &apos;a&apos;, &apos;t&apos;, &apos;e&apos;, &apos;d&apos; }; //declaring a destination array char[] copyTo = new char[7]; //copying array using System.arraycopy() method System.arraycopy(copyFrom, 2, copyTo, 0, 7); //printing the destination array System.out.println(String.valueOf(copyTo)); } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> caffein </pre> <hr> <h2>Cloning an Array in Java</h2> <p>Since, Java array implements the Cloneable interface, we can create the clone of the Java array. If we create the clone of a single-dimensional array, it creates the deep copy of the Java array. It means, it will copy the actual value. But, if we create the clone of a multidimensional array, it creates the shallow copy of the Java array which means it copies the references.</p> <pre> //Java Program to clone the array class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; System.out.println(&apos;Printing original array:&apos;); for(int i:arr) System.out.println(i); System.out.println(&apos;Printing clone of the array:&apos;); int carr[]=arr.clone(); for(int i:carr) System.out.println(i); System.out.println(&apos;Are both equal?&apos;); System.out.println(arr==carr); }} </pre> <p>Output:</p> <pre> Printing original array: 33 3 4 5 Printing clone of the array: 33 3 4 5 Are both equal? false </pre> <h2>Addition of 2 Matrices in Java</h2> <p>Let&apos;s see a simple example that adds two matrices.</p> <pre> //Java Program to demonstrate the addition of two matrices in Java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="a[i][j]+b[i][j];" system.out.print(c[i][j]+' '); } system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 2 6 8 6 8 10 </pre> <h2>Multiplication of 2 Matrices in Java</h2> <p>In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.</p> <img src="//techcodeview.com/img/java-object-class/39/java-arrays.webp" alt="Matrix Multiplication in Java"> <p>Let&apos;s see a simple example to multiply two matrices of 3 rows and 3 columns.</p> <pre> //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+' '); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){></pre></2;i++){></pre></arr.length;></pre></3;i++){></pre></=arr.length;i++){></pre></arr.length;i++)></pre></arr.length;i++)></pre></a.length;i++)></pre></a.length;i++)>

Deklaration, Instanziierung und Initialisierung von Java Array

Wir können das Java-Array gemeinsam deklarieren, instanziieren und initialisieren, indem wir:

 int a[]={33,3,4,5};//declaration, instantiation and initialization 

Sehen wir uns das einfache Beispiel zum Drucken dieses Arrays an.

 //Java Program to illustrate the use of declaration, instantiation //and initialization of Java array in a single line class Testarray1{ public static void main(String args[]){ int a[]={33,3,4,5};//declaration, instantiation and initialization //printing array for(int i=0;i <a.length;i++) length is the property of array system.out.println(a[i]); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 33 3 4 5 </pre> <h2>For-each Loop for Java Array</h2> <p>We can also print the Java array using <strong> <a href="/java-each-loop-enhanced">for-each loop</a> </strong> . The Java for-each loop prints the array elements one by one. It holds an array element in a variable, then executes the body of the loop.</p> <p>The syntax of the for-each loop is given below:</p> <pre> for(data_type variable:array){ //body of the loop } </pre> <p>Let us see the example of print the elements of Java array using the for-each loop.</p> <pre> //Java Program to print the array elements using for-each loop class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; //printing array using for-each loop for(int i:arr) System.out.println(i); }} </pre> <p>Output:</p> <pre> 33 3 4 5 </pre> <hr> <h2>Passing Array to a Method in Java</h2> <p>We can pass the java array to method so that we can reuse the same logic on any array.</p> <p>Let&apos;s see the simple example to get the minimum number of an array using a method.</p> <pre> //Java Program to demonstrate the way of passing an array //to method. class Testarray2{ //creating a method which receives an array as a parameter static void min(int arr[]){ int min=arr[0]; for(int i=1;iarr[i]) min=arr[i]; System.out.println(min); } public static void main(String args[]){ int a[]={33,3,4,5};//declaring and initializing an array min(a);//passing array to method }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> 3 </pre> <h2>Anonymous Array in Java</h2> <p>Java supports the feature of an anonymous array, so you don&apos;t need to declare the array while passing an array to the method.</p> <pre> //Java Program to demonstrate the way of passing an anonymous array //to method. public class TestAnonymousArray{ //creating a method which receives an array as a parameter static void printArray(int arr[]){ for(int i=0;i <arr.length;i++) system.out.println(arr[i]); } public static void main(string args[]){ printarray(new int[]{10,22,44,66}); passing anonymous array to method }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 10 22 44 66 </pre> <h2>Returning Array from the Method</h2> <p>We can also return an array from the method in Java.</p> <pre> //Java Program to return an array from the method class TestReturnArray{ //creating method which returns an array static int[] get(){ return new int[]{10,30,50,90,60}; } public static void main(String args[]){ //calling method which returns an array int arr[]=get(); //printing the values of an array for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 10 30 50 90 60 </pre> <h2>ArrayIndexOutOfBoundsException</h2> <p>The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if length of the array in negative, equal to the array size or greater than the array size while traversing the array.</p> <pre> //Java Program to demonstrate the case of //ArrayIndexOutOfBoundsException in a Java Array. public class TestArrayException{ public static void main(String args[]){ int arr[]={50,60,70,80}; for(int i=0;i<=arr.length;i++){ system.out.println(arr[i]); } }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> Exception in thread &apos;main&apos; java.lang.ArrayIndexOutOfBoundsException: 4 at TestArrayException.main(TestArrayException.java:5) 50 60 70 80 </pre> <hr> <h2>Multidimensional Array in Java</h2> <p>In such case, data is stored in row and column based index (also known as matrix form).</p> <p> <strong>Syntax to Declare Multidimensional Array in Java</strong> </p> <pre> dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; </pre> <p> <strong>Example to instantiate Multidimensional Array in Java</strong> </p> <pre> int[][] arr=new int[3][3];//3 row and 3 column </pre> <p> <strong>Example to initialize Multidimensional Array in Java</strong> </p> <pre> arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; </pre> <h3>Example of Multidimensional Java Array</h3> <p>Let&apos;s see the simple example to declare, instantiate, initialize and print the 2Dimensional array.</p> <pre> //Java Program to illustrate the use of multidimensional array class Testarray3{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" system.out.print(arr[i][j]+\' \'); } system.out.println(); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 1 2 3 2 4 5 4 4 5 </pre> <h2>Jagged Array in Java</h2> <p>If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is an array of arrays with different number of columns.</p> <pre> //Java Program to illustrate the jagged array class TestJaggedArray{ public static void main(String[] args){ //declaring a 2D array with odd columns int arr[][] = new int[3][]; arr[0] = new int[3]; arr[1] = new int[4]; arr[2] = new int[2]; //initializing a jagged array int count = 0; for (int i=0; i <arr.length; i++) for(int j="0;" <arr[i].length; j++) arr[i][j]="count++;" printing the data of a jagged array for (int i="0;" <arr.length; i++){ j++){ system.out.print(arr[i][j]+\' \'); } system.out.println(); new line < pre> <span> Test it Now </span> <p>Output:</p> <pre> 0 1 2 3 4 5 6 7 8 </pre> <hr> <h2>What is the class name of Java array?</h2> <p>In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.</p> <pre> //Java Program to get the class name of array in Java class Testarray4{ public static void main(String args[]){ //declaration and initialization of array int arr[]={4,4,5}; //getting the class name of Java array Class c=arr.getClass(); String name=c.getName(); //printing the class name of Java array System.out.println(name); }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> I </pre> <hr> <h2>Copying a Java Array</h2> <p>We can copy an array to another by the arraycopy() method of System class.</p> <p> <strong>Syntax of arraycopy method</strong> </p> <pre> public static void arraycopy( Object src, int srcPos,Object dest, int destPos, int length ) </pre> <h3>Example of Copying an Array in Java</h3> <pre> //Java Program to copy a source array into a destination array in Java class TestArrayCopyDemo { public static void main(String[] args) { //declaring a source array char[] copyFrom = { &apos;d&apos;, &apos;e&apos;, &apos;c&apos;, &apos;a&apos;, &apos;f&apos;, &apos;f&apos;, &apos;e&apos;, &apos;i&apos;, &apos;n&apos;, &apos;a&apos;, &apos;t&apos;, &apos;e&apos;, &apos;d&apos; }; //declaring a destination array char[] copyTo = new char[7]; //copying array using System.arraycopy() method System.arraycopy(copyFrom, 2, copyTo, 0, 7); //printing the destination array System.out.println(String.valueOf(copyTo)); } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> caffein </pre> <hr> <h2>Cloning an Array in Java</h2> <p>Since, Java array implements the Cloneable interface, we can create the clone of the Java array. If we create the clone of a single-dimensional array, it creates the deep copy of the Java array. It means, it will copy the actual value. But, if we create the clone of a multidimensional array, it creates the shallow copy of the Java array which means it copies the references.</p> <pre> //Java Program to clone the array class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; System.out.println(&apos;Printing original array:&apos;); for(int i:arr) System.out.println(i); System.out.println(&apos;Printing clone of the array:&apos;); int carr[]=arr.clone(); for(int i:carr) System.out.println(i); System.out.println(&apos;Are both equal?&apos;); System.out.println(arr==carr); }} </pre> <p>Output:</p> <pre> Printing original array: 33 3 4 5 Printing clone of the array: 33 3 4 5 Are both equal? false </pre> <h2>Addition of 2 Matrices in Java</h2> <p>Let&apos;s see a simple example that adds two matrices.</p> <pre> //Java Program to demonstrate the addition of two matrices in Java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="a[i][j]+b[i][j];" system.out.print(c[i][j]+\' \'); } system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 2 6 8 6 8 10 </pre> <h2>Multiplication of 2 Matrices in Java</h2> <p>In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.</p> <img src="//techcodeview.com/img/java-object-class/39/java-arrays.webp" alt="Matrix Multiplication in Java"> <p>Let&apos;s see a simple example to multiply two matrices of 3 rows and 3 columns.</p> <pre> //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+\' \'); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){></pre></2;i++){></pre></arr.length;></pre></3;i++){></pre></=arr.length;i++){></pre></arr.length;i++)></pre></arr.length;i++)></pre></a.length;i++)>

For-each-Schleife für Java-Array

Wir können das Java-Array auch mit drucken for-each-Schleife . Die Java-for-each-Schleife druckt die Array-Elemente einzeln aus. Es speichert ein Array-Element in einer Variablen und führt dann den Schleifenkörper aus.

Die Syntax der for-each-Schleife ist unten angegeben:

 for(data_type variable:array){ //body of the loop } 

Sehen wir uns das Beispiel zum Drucken der Elemente eines Java-Arrays mithilfe der for-each-Schleife an.

 //Java Program to print the array elements using for-each loop class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; //printing array using for-each loop for(int i:arr) System.out.println(i); }} 

Ausgabe:

 33 3 4 5 

Übergeben eines Arrays an eine Methode in Java

Wir können das Java-Array an die Methode übergeben, sodass wir dieselbe Logik in jedem Array wiederverwenden können.

Sehen wir uns das einfache Beispiel an, um mithilfe einer Methode die Mindestanzahl eines Arrays zu ermitteln.

 //Java Program to demonstrate the way of passing an array //to method. class Testarray2{ //creating a method which receives an array as a parameter static void min(int arr[]){ int min=arr[0]; for(int i=1;iarr[i]) min=arr[i]; System.out.println(min); } public static void main(String args[]){ int a[]={33,3,4,5};//declaring and initializing an array min(a);//passing array to method }} 
Testen Sie es jetzt

Ausgabe:

 3 

Anonymes Array in Java

Java unterstützt die Funktion eines anonymen Arrays, sodass Sie das Array nicht deklarieren müssen, während Sie ein Array an die Methode übergeben.

 //Java Program to demonstrate the way of passing an anonymous array //to method. public class TestAnonymousArray{ //creating a method which receives an array as a parameter static void printArray(int arr[]){ for(int i=0;i <arr.length;i++) system.out.println(arr[i]); } public static void main(string args[]){ printarray(new int[]{10,22,44,66}); passing anonymous array to method }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 10 22 44 66 </pre> <h2>Returning Array from the Method</h2> <p>We can also return an array from the method in Java.</p> <pre> //Java Program to return an array from the method class TestReturnArray{ //creating method which returns an array static int[] get(){ return new int[]{10,30,50,90,60}; } public static void main(String args[]){ //calling method which returns an array int arr[]=get(); //printing the values of an array for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 10 30 50 90 60 </pre> <h2>ArrayIndexOutOfBoundsException</h2> <p>The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if length of the array in negative, equal to the array size or greater than the array size while traversing the array.</p> <pre> //Java Program to demonstrate the case of //ArrayIndexOutOfBoundsException in a Java Array. public class TestArrayException{ public static void main(String args[]){ int arr[]={50,60,70,80}; for(int i=0;i<=arr.length;i++){ system.out.println(arr[i]); } }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> Exception in thread &apos;main&apos; java.lang.ArrayIndexOutOfBoundsException: 4 at TestArrayException.main(TestArrayException.java:5) 50 60 70 80 </pre> <hr> <h2>Multidimensional Array in Java</h2> <p>In such case, data is stored in row and column based index (also known as matrix form).</p> <p> <strong>Syntax to Declare Multidimensional Array in Java</strong> </p> <pre> dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; </pre> <p> <strong>Example to instantiate Multidimensional Array in Java</strong> </p> <pre> int[][] arr=new int[3][3];//3 row and 3 column </pre> <p> <strong>Example to initialize Multidimensional Array in Java</strong> </p> <pre> arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; </pre> <h3>Example of Multidimensional Java Array</h3> <p>Let&apos;s see the simple example to declare, instantiate, initialize and print the 2Dimensional array.</p> <pre> //Java Program to illustrate the use of multidimensional array class Testarray3{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" system.out.print(arr[i][j]+\' \'); } system.out.println(); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 1 2 3 2 4 5 4 4 5 </pre> <h2>Jagged Array in Java</h2> <p>If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is an array of arrays with different number of columns.</p> <pre> //Java Program to illustrate the jagged array class TestJaggedArray{ public static void main(String[] args){ //declaring a 2D array with odd columns int arr[][] = new int[3][]; arr[0] = new int[3]; arr[1] = new int[4]; arr[2] = new int[2]; //initializing a jagged array int count = 0; for (int i=0; i <arr.length; i++) for(int j="0;" <arr[i].length; j++) arr[i][j]="count++;" printing the data of a jagged array for (int i="0;" <arr.length; i++){ j++){ system.out.print(arr[i][j]+\' \'); } system.out.println(); new line < pre> <span> Test it Now </span> <p>Output:</p> <pre> 0 1 2 3 4 5 6 7 8 </pre> <hr> <h2>What is the class name of Java array?</h2> <p>In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.</p> <pre> //Java Program to get the class name of array in Java class Testarray4{ public static void main(String args[]){ //declaration and initialization of array int arr[]={4,4,5}; //getting the class name of Java array Class c=arr.getClass(); String name=c.getName(); //printing the class name of Java array System.out.println(name); }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> I </pre> <hr> <h2>Copying a Java Array</h2> <p>We can copy an array to another by the arraycopy() method of System class.</p> <p> <strong>Syntax of arraycopy method</strong> </p> <pre> public static void arraycopy( Object src, int srcPos,Object dest, int destPos, int length ) </pre> <h3>Example of Copying an Array in Java</h3> <pre> //Java Program to copy a source array into a destination array in Java class TestArrayCopyDemo { public static void main(String[] args) { //declaring a source array char[] copyFrom = { &apos;d&apos;, &apos;e&apos;, &apos;c&apos;, &apos;a&apos;, &apos;f&apos;, &apos;f&apos;, &apos;e&apos;, &apos;i&apos;, &apos;n&apos;, &apos;a&apos;, &apos;t&apos;, &apos;e&apos;, &apos;d&apos; }; //declaring a destination array char[] copyTo = new char[7]; //copying array using System.arraycopy() method System.arraycopy(copyFrom, 2, copyTo, 0, 7); //printing the destination array System.out.println(String.valueOf(copyTo)); } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> caffein </pre> <hr> <h2>Cloning an Array in Java</h2> <p>Since, Java array implements the Cloneable interface, we can create the clone of the Java array. If we create the clone of a single-dimensional array, it creates the deep copy of the Java array. It means, it will copy the actual value. But, if we create the clone of a multidimensional array, it creates the shallow copy of the Java array which means it copies the references.</p> <pre> //Java Program to clone the array class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; System.out.println(&apos;Printing original array:&apos;); for(int i:arr) System.out.println(i); System.out.println(&apos;Printing clone of the array:&apos;); int carr[]=arr.clone(); for(int i:carr) System.out.println(i); System.out.println(&apos;Are both equal?&apos;); System.out.println(arr==carr); }} </pre> <p>Output:</p> <pre> Printing original array: 33 3 4 5 Printing clone of the array: 33 3 4 5 Are both equal? false </pre> <h2>Addition of 2 Matrices in Java</h2> <p>Let&apos;s see a simple example that adds two matrices.</p> <pre> //Java Program to demonstrate the addition of two matrices in Java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="a[i][j]+b[i][j];" system.out.print(c[i][j]+\' \'); } system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 2 6 8 6 8 10 </pre> <h2>Multiplication of 2 Matrices in Java</h2> <p>In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.</p> <img src="//techcodeview.com/img/java-object-class/39/java-arrays.webp" alt="Matrix Multiplication in Java"> <p>Let&apos;s see a simple example to multiply two matrices of 3 rows and 3 columns.</p> <pre> //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+\' \'); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){></pre></2;i++){></pre></arr.length;></pre></3;i++){></pre></=arr.length;i++){></pre></arr.length;i++)></pre></arr.length;i++)>

Zurückgeben eines Arrays von der Methode

Wir können auch ein Array von der Methode in Java zurückgeben.

 //Java Program to return an array from the method class TestReturnArray{ //creating method which returns an array static int[] get(){ return new int[]{10,30,50,90,60}; } public static void main(String args[]){ //calling method which returns an array int arr[]=get(); //printing the values of an array for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 10 30 50 90 60 </pre> <h2>ArrayIndexOutOfBoundsException</h2> <p>The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if length of the array in negative, equal to the array size or greater than the array size while traversing the array.</p> <pre> //Java Program to demonstrate the case of //ArrayIndexOutOfBoundsException in a Java Array. public class TestArrayException{ public static void main(String args[]){ int arr[]={50,60,70,80}; for(int i=0;i<=arr.length;i++){ system.out.println(arr[i]); } }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> Exception in thread &apos;main&apos; java.lang.ArrayIndexOutOfBoundsException: 4 at TestArrayException.main(TestArrayException.java:5) 50 60 70 80 </pre> <hr> <h2>Multidimensional Array in Java</h2> <p>In such case, data is stored in row and column based index (also known as matrix form).</p> <p> <strong>Syntax to Declare Multidimensional Array in Java</strong> </p> <pre> dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; </pre> <p> <strong>Example to instantiate Multidimensional Array in Java</strong> </p> <pre> int[][] arr=new int[3][3];//3 row and 3 column </pre> <p> <strong>Example to initialize Multidimensional Array in Java</strong> </p> <pre> arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; </pre> <h3>Example of Multidimensional Java Array</h3> <p>Let&apos;s see the simple example to declare, instantiate, initialize and print the 2Dimensional array.</p> <pre> //Java Program to illustrate the use of multidimensional array class Testarray3{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" system.out.print(arr[i][j]+\' \'); } system.out.println(); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 1 2 3 2 4 5 4 4 5 </pre> <h2>Jagged Array in Java</h2> <p>If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is an array of arrays with different number of columns.</p> <pre> //Java Program to illustrate the jagged array class TestJaggedArray{ public static void main(String[] args){ //declaring a 2D array with odd columns int arr[][] = new int[3][]; arr[0] = new int[3]; arr[1] = new int[4]; arr[2] = new int[2]; //initializing a jagged array int count = 0; for (int i=0; i <arr.length; i++) for(int j="0;" <arr[i].length; j++) arr[i][j]="count++;" printing the data of a jagged array for (int i="0;" <arr.length; i++){ j++){ system.out.print(arr[i][j]+\' \'); } system.out.println(); new line < pre> <span> Test it Now </span> <p>Output:</p> <pre> 0 1 2 3 4 5 6 7 8 </pre> <hr> <h2>What is the class name of Java array?</h2> <p>In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.</p> <pre> //Java Program to get the class name of array in Java class Testarray4{ public static void main(String args[]){ //declaration and initialization of array int arr[]={4,4,5}; //getting the class name of Java array Class c=arr.getClass(); String name=c.getName(); //printing the class name of Java array System.out.println(name); }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> I </pre> <hr> <h2>Copying a Java Array</h2> <p>We can copy an array to another by the arraycopy() method of System class.</p> <p> <strong>Syntax of arraycopy method</strong> </p> <pre> public static void arraycopy( Object src, int srcPos,Object dest, int destPos, int length ) </pre> <h3>Example of Copying an Array in Java</h3> <pre> //Java Program to copy a source array into a destination array in Java class TestArrayCopyDemo { public static void main(String[] args) { //declaring a source array char[] copyFrom = { &apos;d&apos;, &apos;e&apos;, &apos;c&apos;, &apos;a&apos;, &apos;f&apos;, &apos;f&apos;, &apos;e&apos;, &apos;i&apos;, &apos;n&apos;, &apos;a&apos;, &apos;t&apos;, &apos;e&apos;, &apos;d&apos; }; //declaring a destination array char[] copyTo = new char[7]; //copying array using System.arraycopy() method System.arraycopy(copyFrom, 2, copyTo, 0, 7); //printing the destination array System.out.println(String.valueOf(copyTo)); } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> caffein </pre> <hr> <h2>Cloning an Array in Java</h2> <p>Since, Java array implements the Cloneable interface, we can create the clone of the Java array. If we create the clone of a single-dimensional array, it creates the deep copy of the Java array. It means, it will copy the actual value. But, if we create the clone of a multidimensional array, it creates the shallow copy of the Java array which means it copies the references.</p> <pre> //Java Program to clone the array class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; System.out.println(&apos;Printing original array:&apos;); for(int i:arr) System.out.println(i); System.out.println(&apos;Printing clone of the array:&apos;); int carr[]=arr.clone(); for(int i:carr) System.out.println(i); System.out.println(&apos;Are both equal?&apos;); System.out.println(arr==carr); }} </pre> <p>Output:</p> <pre> Printing original array: 33 3 4 5 Printing clone of the array: 33 3 4 5 Are both equal? false </pre> <h2>Addition of 2 Matrices in Java</h2> <p>Let&apos;s see a simple example that adds two matrices.</p> <pre> //Java Program to demonstrate the addition of two matrices in Java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="a[i][j]+b[i][j];" system.out.print(c[i][j]+\' \'); } system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 2 6 8 6 8 10 </pre> <h2>Multiplication of 2 Matrices in Java</h2> <p>In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.</p> <img src="//techcodeview.com/img/java-object-class/39/java-arrays.webp" alt="Matrix Multiplication in Java"> <p>Let&apos;s see a simple example to multiply two matrices of 3 rows and 3 columns.</p> <pre> //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+\' \'); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){></pre></2;i++){></pre></arr.length;></pre></3;i++){></pre></=arr.length;i++){></pre></arr.length;i++)>

ArrayIndexOutOfBoundsException

Die Java Virtual Machine (JVM) löst beim Durchlaufen des Arrays eine ArrayIndexOutOfBoundsException aus, wenn die Länge des Arrays negativ, gleich der Array-Größe oder größer als die Array-Größe ist.

 //Java Program to demonstrate the case of //ArrayIndexOutOfBoundsException in a Java Array. public class TestArrayException{ public static void main(String args[]){ int arr[]={50,60,70,80}; for(int i=0;i<=arr.length;i++){ system.out.println(arr[i]); } }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> Exception in thread &apos;main&apos; java.lang.ArrayIndexOutOfBoundsException: 4 at TestArrayException.main(TestArrayException.java:5) 50 60 70 80 </pre> <hr> <h2>Multidimensional Array in Java</h2> <p>In such case, data is stored in row and column based index (also known as matrix form).</p> <p> <strong>Syntax to Declare Multidimensional Array in Java</strong> </p> <pre> dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; </pre> <p> <strong>Example to instantiate Multidimensional Array in Java</strong> </p> <pre> int[][] arr=new int[3][3];//3 row and 3 column </pre> <p> <strong>Example to initialize Multidimensional Array in Java</strong> </p> <pre> arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; </pre> <h3>Example of Multidimensional Java Array</h3> <p>Let&apos;s see the simple example to declare, instantiate, initialize and print the 2Dimensional array.</p> <pre> //Java Program to illustrate the use of multidimensional array class Testarray3{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" system.out.print(arr[i][j]+\' \'); } system.out.println(); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 1 2 3 2 4 5 4 4 5 </pre> <h2>Jagged Array in Java</h2> <p>If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is an array of arrays with different number of columns.</p> <pre> //Java Program to illustrate the jagged array class TestJaggedArray{ public static void main(String[] args){ //declaring a 2D array with odd columns int arr[][] = new int[3][]; arr[0] = new int[3]; arr[1] = new int[4]; arr[2] = new int[2]; //initializing a jagged array int count = 0; for (int i=0; i <arr.length; i++) for(int j="0;" <arr[i].length; j++) arr[i][j]="count++;" printing the data of a jagged array for (int i="0;" <arr.length; i++){ j++){ system.out.print(arr[i][j]+\' \'); } system.out.println(); new line < pre> <span> Test it Now </span> <p>Output:</p> <pre> 0 1 2 3 4 5 6 7 8 </pre> <hr> <h2>What is the class name of Java array?</h2> <p>In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.</p> <pre> //Java Program to get the class name of array in Java class Testarray4{ public static void main(String args[]){ //declaration and initialization of array int arr[]={4,4,5}; //getting the class name of Java array Class c=arr.getClass(); String name=c.getName(); //printing the class name of Java array System.out.println(name); }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> I </pre> <hr> <h2>Copying a Java Array</h2> <p>We can copy an array to another by the arraycopy() method of System class.</p> <p> <strong>Syntax of arraycopy method</strong> </p> <pre> public static void arraycopy( Object src, int srcPos,Object dest, int destPos, int length ) </pre> <h3>Example of Copying an Array in Java</h3> <pre> //Java Program to copy a source array into a destination array in Java class TestArrayCopyDemo { public static void main(String[] args) { //declaring a source array char[] copyFrom = { &apos;d&apos;, &apos;e&apos;, &apos;c&apos;, &apos;a&apos;, &apos;f&apos;, &apos;f&apos;, &apos;e&apos;, &apos;i&apos;, &apos;n&apos;, &apos;a&apos;, &apos;t&apos;, &apos;e&apos;, &apos;d&apos; }; //declaring a destination array char[] copyTo = new char[7]; //copying array using System.arraycopy() method System.arraycopy(copyFrom, 2, copyTo, 0, 7); //printing the destination array System.out.println(String.valueOf(copyTo)); } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> caffein </pre> <hr> <h2>Cloning an Array in Java</h2> <p>Since, Java array implements the Cloneable interface, we can create the clone of the Java array. If we create the clone of a single-dimensional array, it creates the deep copy of the Java array. It means, it will copy the actual value. But, if we create the clone of a multidimensional array, it creates the shallow copy of the Java array which means it copies the references.</p> <pre> //Java Program to clone the array class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; System.out.println(&apos;Printing original array:&apos;); for(int i:arr) System.out.println(i); System.out.println(&apos;Printing clone of the array:&apos;); int carr[]=arr.clone(); for(int i:carr) System.out.println(i); System.out.println(&apos;Are both equal?&apos;); System.out.println(arr==carr); }} </pre> <p>Output:</p> <pre> Printing original array: 33 3 4 5 Printing clone of the array: 33 3 4 5 Are both equal? false </pre> <h2>Addition of 2 Matrices in Java</h2> <p>Let&apos;s see a simple example that adds two matrices.</p> <pre> //Java Program to demonstrate the addition of two matrices in Java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="a[i][j]+b[i][j];" system.out.print(c[i][j]+\' \'); } system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 2 6 8 6 8 10 </pre> <h2>Multiplication of 2 Matrices in Java</h2> <p>In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.</p> <img src="//techcodeview.com/img/java-object-class/39/java-arrays.webp" alt="Matrix Multiplication in Java"> <p>Let&apos;s see a simple example to multiply two matrices of 3 rows and 3 columns.</p> <pre> //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+\' \'); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){></pre></2;i++){></pre></arr.length;></pre></3;i++){></pre></=arr.length;i++){>

Mehrdimensionales Array in Java

In diesem Fall werden die Daten in einem zeilen- und spaltenbasierten Index (auch Matrixform genannt) gespeichert.

Syntax zum Deklarieren eines mehrdimensionalen Arrays in Java

 dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; 

Beispiel zum Instanziieren eines mehrdimensionalen Arrays in Java

Konvertierung von int in string in Java
 int[][] arr=new int[3][3];//3 row and 3 column 

Beispiel zum Initialisieren eines mehrdimensionalen Arrays in Java

 arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; 

Beispiel eines mehrdimensionalen Java-Arrays

Sehen wir uns das einfache Beispiel zum Deklarieren, Instanziieren, Initialisieren und Drucken des 2Dimensional-Arrays an.

 //Java Program to illustrate the use of multidimensional array class Testarray3{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" system.out.print(arr[i][j]+\\' \\'); } system.out.println(); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 1 2 3 2 4 5 4 4 5 </pre> <h2>Jagged Array in Java</h2> <p>If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is an array of arrays with different number of columns.</p> <pre> //Java Program to illustrate the jagged array class TestJaggedArray{ public static void main(String[] args){ //declaring a 2D array with odd columns int arr[][] = new int[3][]; arr[0] = new int[3]; arr[1] = new int[4]; arr[2] = new int[2]; //initializing a jagged array int count = 0; for (int i=0; i <arr.length; i++) for(int j="0;" <arr[i].length; j++) arr[i][j]="count++;" printing the data of a jagged array for (int i="0;" <arr.length; i++){ j++){ system.out.print(arr[i][j]+\\' \\'); } system.out.println(); new line < pre> <span> Test it Now </span> <p>Output:</p> <pre> 0 1 2 3 4 5 6 7 8 </pre> <hr> <h2>What is the class name of Java array?</h2> <p>In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.</p> <pre> //Java Program to get the class name of array in Java class Testarray4{ public static void main(String args[]){ //declaration and initialization of array int arr[]={4,4,5}; //getting the class name of Java array Class c=arr.getClass(); String name=c.getName(); //printing the class name of Java array System.out.println(name); }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> I </pre> <hr> <h2>Copying a Java Array</h2> <p>We can copy an array to another by the arraycopy() method of System class.</p> <p> <strong>Syntax of arraycopy method</strong> </p> <pre> public static void arraycopy( Object src, int srcPos,Object dest, int destPos, int length ) </pre> <h3>Example of Copying an Array in Java</h3> <pre> //Java Program to copy a source array into a destination array in Java class TestArrayCopyDemo { public static void main(String[] args) { //declaring a source array char[] copyFrom = { &apos;d&apos;, &apos;e&apos;, &apos;c&apos;, &apos;a&apos;, &apos;f&apos;, &apos;f&apos;, &apos;e&apos;, &apos;i&apos;, &apos;n&apos;, &apos;a&apos;, &apos;t&apos;, &apos;e&apos;, &apos;d&apos; }; //declaring a destination array char[] copyTo = new char[7]; //copying array using System.arraycopy() method System.arraycopy(copyFrom, 2, copyTo, 0, 7); //printing the destination array System.out.println(String.valueOf(copyTo)); } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> caffein </pre> <hr> <h2>Cloning an Array in Java</h2> <p>Since, Java array implements the Cloneable interface, we can create the clone of the Java array. If we create the clone of a single-dimensional array, it creates the deep copy of the Java array. It means, it will copy the actual value. But, if we create the clone of a multidimensional array, it creates the shallow copy of the Java array which means it copies the references.</p> <pre> //Java Program to clone the array class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; System.out.println(&apos;Printing original array:&apos;); for(int i:arr) System.out.println(i); System.out.println(&apos;Printing clone of the array:&apos;); int carr[]=arr.clone(); for(int i:carr) System.out.println(i); System.out.println(&apos;Are both equal?&apos;); System.out.println(arr==carr); }} </pre> <p>Output:</p> <pre> Printing original array: 33 3 4 5 Printing clone of the array: 33 3 4 5 Are both equal? false </pre> <h2>Addition of 2 Matrices in Java</h2> <p>Let&apos;s see a simple example that adds two matrices.</p> <pre> //Java Program to demonstrate the addition of two matrices in Java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="a[i][j]+b[i][j];" system.out.print(c[i][j]+\\' \\'); } system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 2 6 8 6 8 10 </pre> <h2>Multiplication of 2 Matrices in Java</h2> <p>In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.</p> <img src="//techcodeview.com/img/java-object-class/39/java-arrays.webp" alt="Matrix Multiplication in Java"> <p>Let&apos;s see a simple example to multiply two matrices of 3 rows and 3 columns.</p> <pre> //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+\\' \\'); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){></pre></2;i++){></pre></arr.length;></pre></3;i++){>

Gezacktes Array in Java

Wenn wir eine ungerade Anzahl von Spalten in einem 2D-Array erstellen, spricht man von einem gezackten Array. Mit anderen Worten handelt es sich um ein Array aus Arrays mit unterschiedlicher Spaltenanzahl.

 //Java Program to illustrate the jagged array class TestJaggedArray{ public static void main(String[] args){ //declaring a 2D array with odd columns int arr[][] = new int[3][]; arr[0] = new int[3]; arr[1] = new int[4]; arr[2] = new int[2]; //initializing a jagged array int count = 0; for (int i=0; i <arr.length; i++) for(int j="0;" <arr[i].length; j++) arr[i][j]="count++;" printing the data of a jagged array for (int i="0;" <arr.length; i++){ j++){ system.out.print(arr[i][j]+\\' \\'); } system.out.println(); new line < pre> <span> Test it Now </span> <p>Output:</p> <pre> 0 1 2 3 4 5 6 7 8 </pre> <hr> <h2>What is the class name of Java array?</h2> <p>In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.</p> <pre> //Java Program to get the class name of array in Java class Testarray4{ public static void main(String args[]){ //declaration and initialization of array int arr[]={4,4,5}; //getting the class name of Java array Class c=arr.getClass(); String name=c.getName(); //printing the class name of Java array System.out.println(name); }} </pre> <span> Test it Now </span> <p>Output:</p> <pre> I </pre> <hr> <h2>Copying a Java Array</h2> <p>We can copy an array to another by the arraycopy() method of System class.</p> <p> <strong>Syntax of arraycopy method</strong> </p> <pre> public static void arraycopy( Object src, int srcPos,Object dest, int destPos, int length ) </pre> <h3>Example of Copying an Array in Java</h3> <pre> //Java Program to copy a source array into a destination array in Java class TestArrayCopyDemo { public static void main(String[] args) { //declaring a source array char[] copyFrom = { &apos;d&apos;, &apos;e&apos;, &apos;c&apos;, &apos;a&apos;, &apos;f&apos;, &apos;f&apos;, &apos;e&apos;, &apos;i&apos;, &apos;n&apos;, &apos;a&apos;, &apos;t&apos;, &apos;e&apos;, &apos;d&apos; }; //declaring a destination array char[] copyTo = new char[7]; //copying array using System.arraycopy() method System.arraycopy(copyFrom, 2, copyTo, 0, 7); //printing the destination array System.out.println(String.valueOf(copyTo)); } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> caffein </pre> <hr> <h2>Cloning an Array in Java</h2> <p>Since, Java array implements the Cloneable interface, we can create the clone of the Java array. If we create the clone of a single-dimensional array, it creates the deep copy of the Java array. It means, it will copy the actual value. But, if we create the clone of a multidimensional array, it creates the shallow copy of the Java array which means it copies the references.</p> <pre> //Java Program to clone the array class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; System.out.println(&apos;Printing original array:&apos;); for(int i:arr) System.out.println(i); System.out.println(&apos;Printing clone of the array:&apos;); int carr[]=arr.clone(); for(int i:carr) System.out.println(i); System.out.println(&apos;Are both equal?&apos;); System.out.println(arr==carr); }} </pre> <p>Output:</p> <pre> Printing original array: 33 3 4 5 Printing clone of the array: 33 3 4 5 Are both equal? false </pre> <h2>Addition of 2 Matrices in Java</h2> <p>Let&apos;s see a simple example that adds two matrices.</p> <pre> //Java Program to demonstrate the addition of two matrices in Java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="a[i][j]+b[i][j];" system.out.print(c[i][j]+\\' \\'); } system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 2 6 8 6 8 10 </pre> <h2>Multiplication of 2 Matrices in Java</h2> <p>In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.</p> <img src="//techcodeview.com/img/java-object-class/39/java-arrays.webp" alt="Matrix Multiplication in Java"> <p>Let&apos;s see a simple example to multiply two matrices of 3 rows and 3 columns.</p> <pre> //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+\\' \\'); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){></pre></2;i++){></pre></arr.length;>

Wie lautet der Klassenname des Java-Arrays?

In Java ist ein Array ein Objekt. Für Array-Objekte wird eine Proxy-Klasse erstellt, deren Name mit der Methode getClass().getName() für das Objekt abgerufen werden kann.

 //Java Program to get the class name of array in Java class Testarray4{ public static void main(String args[]){ //declaration and initialization of array int arr[]={4,4,5}; //getting the class name of Java array Class c=arr.getClass(); String name=c.getName(); //printing the class name of Java array System.out.println(name); }} 
Testen Sie es jetzt

Ausgabe:

 I 

Kopieren eines Java-Arrays

Mit der Methode arraycopy() der Systemklasse können wir ein Array in ein anderes kopieren.

Syntax der Arraycopy-Methode

 public static void arraycopy( Object src, int srcPos,Object dest, int destPos, int length ) 

Beispiel für das Kopieren eines Arrays in Java

 //Java Program to copy a source array into a destination array in Java class TestArrayCopyDemo { public static void main(String[] args) { //declaring a source array char[] copyFrom = { &apos;d&apos;, &apos;e&apos;, &apos;c&apos;, &apos;a&apos;, &apos;f&apos;, &apos;f&apos;, &apos;e&apos;, &apos;i&apos;, &apos;n&apos;, &apos;a&apos;, &apos;t&apos;, &apos;e&apos;, &apos;d&apos; }; //declaring a destination array char[] copyTo = new char[7]; //copying array using System.arraycopy() method System.arraycopy(copyFrom, 2, copyTo, 0, 7); //printing the destination array System.out.println(String.valueOf(copyTo)); } } 
Testen Sie es jetzt

Ausgabe:

 caffein 

Klonen eines Arrays in Java

Da das Java-Array die klonbare Schnittstelle implementiert, können wir den Klon des Java-Arrays erstellen. Wenn wir den Klon eines eindimensionalen Arrays erstellen, wird die tiefe Kopie des Java-Arrays erstellt. Das bedeutet, dass der tatsächliche Wert kopiert wird. Wenn wir jedoch den Klon eines mehrdimensionalen Arrays erstellen, wird eine flache Kopie des Java-Arrays erstellt, was bedeutet, dass die Referenzen kopiert werden.

 //Java Program to clone the array class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; System.out.println(&apos;Printing original array:&apos;); for(int i:arr) System.out.println(i); System.out.println(&apos;Printing clone of the array:&apos;); int carr[]=arr.clone(); for(int i:carr) System.out.println(i); System.out.println(&apos;Are both equal?&apos;); System.out.println(arr==carr); }} 

Ausgabe:

 Printing original array: 33 3 4 5 Printing clone of the array: 33 3 4 5 Are both equal? false 

Hinzufügung von 2 Matrizen in Java

Sehen wir uns ein einfaches Beispiel an, das zwei Matrizen hinzufügt.

 //Java Program to demonstrate the addition of two matrices in Java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="a[i][j]+b[i][j];" system.out.print(c[i][j]+\\' \\'); } system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 2 6 8 6 8 10 </pre> <h2>Multiplication of 2 Matrices in Java</h2> <p>In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.</p> <img src="//techcodeview.com/img/java-object-class/39/java-arrays.webp" alt="Matrix Multiplication in Java"> <p>Let&apos;s see a simple example to multiply two matrices of 3 rows and 3 columns.</p> <pre> //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+\\' \\'); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){></pre></2;i++){>

Multiplikation von 2 Matrizen in Java

Bei der Matrixmultiplikation wird ein einzeiliges Element der ersten Matrix mit allen Spalten der zweiten Matrix multipliziert, was aus dem untenstehenden Bild ersichtlich ist.

Matrixmultiplikation in Java

Sehen wir uns ein einfaches Beispiel für die Multiplikation zweier Matrizen mit 3 Zeilen und 3 Spalten an.

 //Java Program to multiply two matrices public class MatrixMultiplicationExample{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j="0;j&lt;3;j++){" c[i][j]="0;" k="0;k&lt;3;k++)" { c[i][j]+="a[i][k]*b[k][j];" } end of loop system.out.print(c[i][j]+\\' \\'); printing matrix element system.out.println(); new line }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> 6 6 6 12 12 12 18 18 18 </pre> <h3>Related Topics</h3> <h2> 1) Java Program to copy all elements of one array into another array </h2> <h2> 2) Java Program to find the frequency of each element in the array </h2> <h2> 3) Java Program to left rotate the elements of an array </h2> <h2> 4) Java Program to print the duplicate elements of an array </h2> <h2> 5) Java Program to print the elements of an array </h2> <h2> 6) Java Program to print the elements of an array in reverse order </h2> <h2> 7) Java Program to print the elements of an array present on even position </h2> <h2> 8) Java Program to print the elements of an array present on odd position </h2> <h2> 9) Java Program to print the largest element in an array </h2> <h2> 10) Java Program to print the smallest element in an array </h2> <h2> 11) Java Program to print the number of elements present in an array </h2> <h2> 12) Java Program to print the sum of all the items of the array </h2> <h2> 13) Java Program to right rotate the elements of an array </h2> <h2> 14) Java Program to sort the elements of an array in ascending order </h2> <h2> 15) Java Program to sort the elements of an array in descending order </h2> <h2>16) Find 3rd Largest Number in an Array </h2> <h2>17) Find 2nd Largest Number in an Array </h2> <h2>18) Find Largest Number in an Array </h2> <h2>19) Find 2nd Smallest Number in an Array </h2> <h2>20) Find Smallest Number in an Array </h2> <h2>21) Remove Duplicate Element in an Array </h2> <h2>22) Add Two Matrices </h2> <h2>23) <a href="/java-program-multiply-two-matrices">Multiply Two Matrices</a> </h2> <h2>24) Print Odd and Even Number from an Array </h2> <h2>25) Transpose matrix </h2> <h2> 26) Java Program to subtract the two matrices </h2> <h2> 27) Java Program to determine whether a given matrix is an identity matrix </h2> <h2> 28) Java Program to determine whether a given matrix is a sparse matrix </h2> <h2> 29) Java Program to determine whether two matrices are equal </h2> <h2> 30) Java Program to display the lower triangular matrix </h2> <h2> 31) Java Program to display the upper triangular matrix </h2> <h2> 32) Java Program to find the frequency of odd &amp; even numbers in the given matrix </h2> <h2> 33) Java Program to find the product of two matrices </h2> <h2> 34) Java Program to find the sum of each row and each column of a matrix </h2> <h2> 35) Java Program to find the transpose of a given matrix </h2></3;i++){>

Verwandte Themen

1) Java-Programm zum Kopieren aller Elemente eines Arrays in ein anderes Array

2) Java-Programm zum Ermitteln der Häufigkeit jedes Elements im Array

3) Java-Programm, um die Elemente eines Arrays nach links zu drehen

4) Java-Programm zum Drucken der doppelten Elemente eines Arrays

5) Java-Programm zum Drucken der Elemente eines Arrays

6) Java-Programm zum Drucken der Elemente eines Arrays in umgekehrter Reihenfolge

7) Java-Programm zum Drucken der Elemente eines Arrays, die an gerader Position vorhanden sind

8) Java-Programm zum Drucken der Elemente eines Arrays, die an ungeraden Positionen vorhanden sind

9) Java-Programm zum Drucken des größten Elements in einem Array

10) Java-Programm zum Drucken des kleinsten Elements in einem Array

11) Java-Programm zum Drucken der Anzahl der in einem Array vorhandenen Elemente

12) Java-Programm zum Drucken der Summe aller Elemente des Arrays

13) Java-Programm zum Rechtsdrehen der Elemente eines Arrays

14) Java-Programm zum Sortieren der Elemente eines Arrays in aufsteigender Reihenfolge

15) Java-Programm zum Sortieren der Elemente eines Arrays in absteigender Reihenfolge

16) Finden Sie die drittgrößte Zahl in einem Array

17) Finden Sie die zweitgrößte Zahl in einem Array

18) Finden Sie die größte Zahl in einem Array

19) Finden Sie die zweitkleinste Zahl in einem Array

20) Finden Sie die kleinste Zahl in einem Array

21) Entfernen Sie doppelte Elemente in einem Array

22) Fügen Sie zwei Matrizen hinzu

23) Multiplizieren Sie zwei Matrizen

24) Drucken Sie ungerade und gerade Zahlen aus einem Array

25) Matrix transponieren

26) Java-Programm zum Subtrahieren der beiden Matrizen

27) Java-Programm zur Bestimmung, ob eine gegebene Matrix eine Identitätsmatrix ist

28) Java-Programm zur Bestimmung, ob eine gegebene Matrix eine dünn besetzte Matrix ist

29) Java-Programm zur Bestimmung, ob zwei Matrizen gleich sind

30) Java-Programm zur Anzeige der unteren Dreiecksmatrix

31) Java-Programm zur Anzeige der oberen Dreiecksmatrix

32) Java-Programm zum Ermitteln der Häufigkeit ungerader und gerader Zahlen in der angegebenen Matrix

33) Java-Programm zum Ermitteln des Produkts zweier Matrizen

34) Java-Programm zum Ermitteln der Summe jeder Zeile und jeder Spalte einer Matrix

35) Java-Programm zum Ermitteln der Transponierten einer bestimmten Matrix