logo

Array Java

Normalmente, un array è una raccolta di tipi simili di elementi che hanno una posizione di memoria contigua.

matrice Java è un oggetto che contiene elementi di un tipo di dati simile. Inoltre, gli elementi di un array vengono archiviati in una posizione di memoria contigua. È una struttura dati in cui memorizziamo elementi simili. Possiamo memorizzare solo un insieme fisso di elementi in un array Java.

L'array in Java è basato sull'indice, il primo elemento dell'array è memorizzato nell'indice 0, il 2° elemento è memorizzato nel 1° indice e così via.

A differenza di C/C++, possiamo ottenere la lunghezza dell'array utilizzando il membro length. In C/C++ dobbiamo utilizzare l'operatore sizeof.

In Java, l'array è un oggetto di una classe generata dinamicamente. L'array Java eredita la classe Object e implementa le interfacce Serializable e Cloneable. Possiamo memorizzare valori o oggetti primitivi in ​​un array in Java. Come C/C++, anche in Java possiamo creare array monodimensionali o multidimensionali.

Inoltre, Java fornisce la funzionalità di array anonimi che non è disponibile in C/C++.

matrice Java

Vantaggi

    Ottimizzazione del codice:Rende il codice ottimizzato, possiamo recuperare o ordinare i dati in modo efficiente.Accesso casuale:Possiamo ottenere qualsiasi dato situato in una posizione di indice.

Svantaggi

    Limite di dimensione:Possiamo memorizzare solo la dimensione fissa degli elementi nell'array. Non aumenta le sue dimensioni in fase di esecuzione. Per risolvere questo problema, in Java viene utilizzato il framework di raccolta che cresce automaticamente.

Tipi di array in Java

Esistono due tipi di array.

  • Array monodimensionale
  • Matrice multidimensionale

Array unidimensionale in Java

Sintassi per dichiarare un array in Java

Python genera uuid
 dataType[] arr; (or) dataType []arr; (or) dataType arr[]; 

Istanziazione di un array in Java

 arrayRefVar=new datatype[size]; 

Esempio di array Java

Vediamo il semplice esempio di array Java, in cui dichiareremo, istanziaremo, inizializzeremo e attraverseremo un array.

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

Dichiarazione, istanziazione e inizializzazione di Java Array

Possiamo dichiarare, istanziare e inizializzare insieme l'array Java tramite:

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

Vediamo il semplice esempio per stampare questo array.

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

Ciclo For-each per Java Array

Possiamo anche stampare l'array Java utilizzando per ogni ciclo . Il ciclo Java for-each stampa gli elementi dell'array uno per uno. Contiene un elemento dell'array in una variabile, quindi esegue il corpo del ciclo.

La sintassi del ciclo for-each è riportata di seguito:

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

Vediamo l'esempio di stampare gli elementi dell'array Java utilizzando il ciclo 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); }} 

Produzione:

 33 3 4 5 

Passaggio di array a un metodo in Java

Possiamo passare l'array Java al metodo in modo da poter riutilizzare la stessa logica su qualsiasi array.

Vediamo il semplice esempio per ottenere il numero minimo di un array utilizzando un metodo.

 //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 }} 
Provalo adesso

Produzione:

 3 

Array anonimo in Java

Java supporta la funzionalità di un array anonimo, quindi non è necessario dichiarare l'array mentre si passa un array al metodo.

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

Array restituito dal metodo

Possiamo anche restituire un array dal metodo in 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++)>

Indice della Matrice Fuori Dai Limiti d'Eccezione

La Java Virtual Machine (JVM) lancia un'eccezione ArrayIndexOutOfBoundsException se la lunghezza dell'array è negativa, uguale alla dimensione dell'array o maggiore della dimensione dell'array mentre attraversa l'array.

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

Array multidimensionale in Java

In tal caso, i dati vengono archiviati in un indice basato su righe e colonne (noto anche come formato a matrice).

Sintassi per dichiarare array multidimensionali in Java

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

Esempio per istanziare un array multidimensionale in Java

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

Esempio per inizializzare un array multidimensionale 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; 

Esempio di array Java multidimensionale

Vediamo il semplice esempio per dichiarare, istanziare, inizializzare e stampare l'array 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++){>

Array frastagliato in Java

Se stiamo creando un numero dispari di colonne in un array 2D, è noto come array frastagliato. In altre parole, è un array di array con un numero diverso di colonne.

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

Qual è il nome della classe dell'array Java?

In Java, un array è un oggetto. Per l'oggetto array, viene creata una classe proxy il cui nome può essere ottenuto tramite il metodo getClass().getName() sull'oggetto.

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

Produzione:

 I 

Copia di un array Java

Possiamo copiare un array in un altro tramite il metodo arraycopy() della classe System.

Sintassi del metodo arraycopy

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

Esempio di copia di un array 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)); } } 
Provalo adesso

Produzione:

 caffein 

Clonazione di un array in Java

Poiché l'array Java implementa l'interfaccia Cloneable, possiamo creare il clone dell'array Java. Se creiamo il clone di un array unidimensionale, crea la copia profonda dell'array Java. Significa che copierà il valore effettivo. Ma, se creiamo il clone di un array multidimensionale, crea la copia superficiale dell'array Java, il che significa che copia i riferimenti.

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

Produzione:

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

Aggiunta di 2 matrici in Java

Vediamo un semplice esempio che somma due matrici.

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

Moltiplicazione di 2 matrici in Java

Nel caso della moltiplicazione di matrici, un elemento di una riga della prima matrice viene moltiplicato per tutte le colonne della seconda matrice, come si può comprendere dall'immagine riportata di seguito.

Moltiplicazione di matrici in Java

Vediamo un semplice esempio per moltiplicare due matrici di 3 righe e 3 colonne.

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

Argomenti correlati

1) Programma Java per copiare tutti gli elementi di un array in un altro array

2) Programma Java per trovare la frequenza di ciascun elemento nell'array

3) Programma Java per ruotare a sinistra gli elementi di un array

4) Programma Java per stampare gli elementi duplicati di un array

5) Programma Java per stampare gli elementi di un array

6) Programma Java per stampare gli elementi di un array in ordine inverso

7) Programma Java per stampare gli elementi di un array presenti in posizione pari

8) Programma Java per stampare gli elementi di un array presenti in posizione dispari

9) Programma Java per stampare l'elemento più grande in un array

10) Programma Java per stampare l'elemento più piccolo in un array

11) Programma Java per stampare il numero di elementi presenti in un array

12) Programma Java per stampare la somma di tutti gli elementi dell'array

13) Programma Java per ruotare a destra gli elementi di un array

14) Programma Java per ordinare gli elementi di un array in ordine crescente

15) Programma Java per ordinare gli elementi di un array in ordine decrescente

16) Trova il terzo numero più grande in una matrice

17) Trova il 2° numero più grande in una matrice

18) Trova il numero più grande in una matrice

19) Trova il 2° numero più piccolo in una matrice

20) Trova il numero più piccolo in una matrice

21) Rimuovi l'elemento duplicato in un array

22) Aggiungi due matrici

23) Moltiplicare due matrici

24) Stampa i numeri pari e dispari da un array

25) Matrice trasposta

26) Programma Java per sottrarre le due matrici

27) Programma Java per determinare se una data matrice è una matrice identità

28) Programma Java per determinare se una data matrice è una matrice sparsa

29) Programma Java per determinare se due matrici sono uguali

30) Programma Java per visualizzare la matrice triangolare inferiore

31) Programma Java per visualizzare la matrice triangolare superiore

32) Programma Java per trovare la frequenza dei numeri pari e dispari nella matrice data

33) Programma Java per trovare il prodotto di due matrici

34) Programma Java per trovare la somma di ogni riga e di ogni colonna di una matrice

35) Programma Java per trovare la trasposta di una determinata matrice