logo

Matrice Java

În mod normal, o matrice este o colecție de elemente de tip similar care are o locație de memorie contigua.

matrice Java este un obiect care conține elemente de un tip de date similar. În plus, elementele unui tablou sunt stocate într-o locație de memorie adiacentă. Este o structură de date în care stocăm elemente similare. Putem stoca doar un set fix de elemente într-o matrice Java.

Matricea în Java este bazată pe index, primul element al matricei este stocat la al 0-lea index, al 2-lea element este stocat pe primul index și așa mai departe.

Spre deosebire de C/C++, putem obține lungimea matricei folosind membrul de lungime. În C/C++, trebuie să folosim operatorul sizeof.

În Java, matricea este un obiect al unei clase generate dinamic. Matricea Java moștenește clasa Object și implementează interfețele Serializable și Cloneable. Putem stoca valori sau obiecte primitive într-o matrice în Java. La fel ca C/C++, putem crea și matrice monodimensionale sau multidimensionale în Java.

Mai mult, Java oferă caracteristica matricelor anonime care nu este disponibilă în C/C++.

matrice Java

Avantaje

    Optimizarea codului:Face codul optimizat, putem prelua sau sorta datele eficient.Acces aleator:Putem obține orice date situate la o poziție index.

Dezavantaje

    Limita de dimensiune:Putem stoca doar dimensiunea fixă ​​a elementelor din matrice. Nu își crește dimensiunea în timpul rulării. Pentru a rezolva această problemă, în Java se folosește cadrul de colectare care crește automat.

Tipuri de matrice în java

Există două tipuri de matrice.

  • Matrice unică dimensională
  • Matrice multidimensională

Single Dimensional Array în Java

Sintaxă pentru a declara o matrice în Java

java somn
 dataType[] arr; (or) dataType []arr; (or) dataType arr[]; 

Instanciarea unui Array în Java

 arrayRefVar=new datatype[size]; 

Exemplu de matrice Java

Să vedem exemplul simplu de matrice java, în care vom declara, instanția, inițializa și parcurge un tablou.

 //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++)>

Declararea, instanțierea și inițializarea matricei Java

Putem declara, instanția și inițializa împreună matricea java prin:

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

Să vedem exemplul simplu pentru a tipări această matrice.

 //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 Loop for Java Array

De asemenea, putem imprima matricea Java folosind pentru-fiecare buclă . Java for-each bucla tipărește elementele matricei unul câte unul. Deține un element de matrice într-o variabilă, apoi execută corpul buclei.

Sintaxa buclei for-each este dată mai jos:

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

Să vedem exemplul de tipărire a elementelor matricei Java folosind bucla for-each.

 //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); }} 

Ieșire:

 33 3 4 5 

Trecerea unui array la o metodă în Java

Putem trece matricea java la metoda, astfel încât să putem reutiliza aceeași logică pe orice matrice.

Să vedem exemplul simplu pentru a obține numărul minim al unui tablou folosind o metodă.

 //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 }} 
Testează-l acum

Ieșire:

 3 

Matrice anonimă în Java

Java acceptă caracteristica unei matrice anonime, deci nu trebuie să declarați matricea în timp ce treceți o matrice la metodă.

 //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++)>

Returing Array de la Metodă

De asemenea, putem returna o matrice din metoda în Java.

 //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++)>

Indicele matricei este în afara limitelor

Mașina virtuală Java (JVM) lansează o excepție ArrayIndexOutOfBoundsException dacă lungimea matricei este negativă, egală cu dimensiunea matricei sau mai mare decât dimensiunea matricei în timpul parcurgerii matricei.

tăierea alfa beta
 //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++){>

Matrice multidimensională în Java

În acest caz, datele sunt stocate în index bazat pe rând și coloană (cunoscut și ca formă de matrice).

Sintaxă pentru declararea matricei multidimensionale în Java

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

Exemplu de instanțiere a unui tablou multidimensional în Java

 int[][] arr=new int[3][3];//3 row and 3 column 

Exemplu de inițializare a matricei multidimensionale în 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; 

Exemplu de matrice Java multidimensională

Să vedem exemplul simplu de declarare, instanțiere, inițializare și imprimare a tabloului 2Dimensional.

 //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++){>

Jagged Array în Java

Dacă creăm un număr impar de coloane într-o matrice 2D, acesta este cunoscut sub numele de matrice zimțată. Cu alte cuvinte, este o matrice de matrice cu un număr diferit de coloane.

 //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;>

Care este numele clasei matricei Java?

În Java, o matrice este un obiect. Pentru obiectul matrice, este creată o clasă proxy al cărei nume poate fi obținut prin metoda getClass().getName() de pe obiect.

 //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); }} 
Testează-l acum

Ieșire:

 I 

Copierea unei matrice Java

Putem copia o matrice în alta prin metoda arraycopy() a clasei System.

Sintaxa metodei arraycopy

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

Exemplu de copiere a unui tablou în 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)); } } 
Testează-l acum

Ieșire:

 caffein 

Clonarea unui Array în Java

Deoarece matricea Java implementează interfața Cloneable, putem crea clona matricei Java. Dacă creăm clona unei matrice unidimensionale, aceasta creează copia profundă a matricei Java. Înseamnă că va copia valoarea reală. Dar, dacă creăm clona unei matrice multidimensionale, aceasta creează o copie superficială a matricei Java, ceea ce înseamnă că copiază referințele.

 //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); }} 

Ieșire:

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

Adăugarea a 2 Matrici în Java

Să vedem un exemplu simplu care adaugă două matrice.

 //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++){>

Înmulțirea a 2 matrici în Java

În cazul înmulțirii matricei, un element cu un rând din prima matrice este înmulțit cu toate coloanele celei de-a doua matrice, ceea ce poate fi înțeles din imaginea de mai jos.

Înmulțirea matricelor în Java

Să vedem un exemplu simplu de înmulțire a două matrice de 3 rânduri și 3 coloane.

 //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++){>

Subiecte asemănătoare

1) Program Java pentru a copia toate elementele unei matrice într-o altă matrice

2) Program Java pentru a găsi frecvența fiecărui element din matrice

3) Program Java pentru a roti la stânga elementele unui tablou

4) Program Java pentru a imprima elementele duplicate ale unui tablou

5) Program Java pentru a imprima elementele unui tablou

6) Program Java pentru a imprima elementele unui tablou în ordine inversă

7) Program Java pentru a imprima elementele unui tablou prezent pe poziția pară

8) Program Java pentru a imprima elementele unui tablou prezent pe poziție impară

9) Program Java pentru a imprima cel mai mare element dintr-o matrice

10) Program Java pentru a imprima cel mai mic element dintr-o matrice

11) Program Java pentru a imprima numărul de elemente prezente într-o matrice

12) Program Java pentru a tipări suma tuturor elementelor matricei

13) Program Java pentru a roti la dreapta elementele unui tablou

14) Program Java pentru a sorta elementele unui tablou în ordine crescătoare

15) Program Java pentru a sorta elementele unui tablou în ordine descrescătoare

16) Găsiți al treilea cel mai mare număr dintr-o matrice

17) Găsiți al doilea cel mai mare număr dintr-o matrice

18) Găsiți cel mai mare număr dintr-o matrice

19) Găsiți al doilea cel mai mic număr dintr-o matrice

20) Găsiți cel mai mic număr dintr-o matrice

21) Eliminați elementul duplicat dintr-o matrice

22) Adăugați două matrici

23) Înmulțiți două matrici

24) Tipăriți numărul par și impar dintr-o matrice

25) Transpunerea matricei

26) Program Java pentru scăderea celor două matrici

27) Program Java pentru a determina dacă o matrice dată este o matrice de identitate

28) Program Java pentru a determina dacă o matrice dată este o matrice rară

29) Program Java pentru a determina dacă două matrici sunt egale

30) Program Java pentru afișarea matricei triunghiulare inferioare

31) Program Java pentru afișarea matricei triunghiulare superioare

32) Program Java pentru a găsi frecvența numerelor pare și impare în matricea dată

33) Program Java pentru a găsi produsul a două matrici

34) Program Java pentru a găsi suma fiecărui rând și a fiecărei coloane dintr-o matrice

35) Program Java pentru a găsi transpunerea unei matrice date