logo

Java-arrayer

Normalt er en matrise en samling av lignende type elementer som har sammenhengende minneplassering.

Java-array er et objekt som inneholder elementer av en lignende datatype. I tillegg er elementene i en matrise lagret i en sammenhengende minneplassering. Det er en datastruktur hvor vi lagrer lignende elementer. Vi kan bare lagre et fast sett med elementer i en Java-array.

Array i Java er indeksbasert, det første elementet i arrayet er lagret på 0. indeks, 2. element er lagret på 1. indeks og så videre.

I motsetning til C/C++, kan vi få lengden på matrisen ved å bruke lengdemedlemmet. I C/C++ må vi bruke operatorstørrelsen.

I Java er array et objekt av en dynamisk generert klasse. Java-arrayen arver Object-klassen, og implementerer de serialiserbare og klonebare grensesnittene. Vi kan lagre primitive verdier eller objekter i en matrise i Java. I likhet med C/C++ kan vi også lage enkeltdimensjonale eller multidimensjonale arrays i Java.

Dessuten gir Java funksjonen til anonyme arrays som ikke er tilgjengelig i C/C++.

Java-array

Fordeler

    Kodeoptimalisering:Det gjør koden optimalisert, vi kan hente eller sortere dataene effektivt.Tilfeldig tilgang:Vi kan få alle data som ligger ved en indeksposisjon.

Ulemper

    Størrelsesgrense:Vi kan bare lagre den faste størrelsen på elementene i matrisen. Den vokser ikke i størrelse under kjøring. For å løse dette problemet brukes samlingsrammeverket i Java som vokser automatisk.

Typer Array i java

Det finnes to typer array.

  • Endimensjonal array
  • Flerdimensjonal matrise

Single Dimensional Array i Java

Syntaks for å deklarere en matrise i Java

gimp lagre som jpeg
 dataType[] arr; (or) dataType []arr; (or) dataType arr[]; 

Instantiering av en matrise i Java

 arrayRefVar=new datatype[size]; 

Eksempel på Java Array

La oss se det enkle eksemplet på java-array, der vi skal deklarere, instansiere, initialisere og krysse en matrise.

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

Erklæring, instansiering og initialisering av Java Array

Vi kan deklarere, instansiere og initialisere java-arrayen sammen ved å:

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

La oss se det enkle eksemplet for å skrive ut denne matrisen.

 //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 hver sløyfe for Java Array

Vi kan også skrive ut Java-matrisen ved hjelp av for hver løkke . Java for-each loop skriver ut array-elementene én etter én. Den inneholder et matriseelement i en variabel, og utfører deretter løkkens kropp.

Syntaksen til for-hver-løkken er gitt nedenfor:

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

La oss se eksemplet med å skrive ut elementene i Java-arrayen ved å bruke for-each-løkken.

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

Produksjon:

 33 3 4 5 

Sende Array til en metode i Java

Vi kan sende java-matrisen til metoden slik at vi kan gjenbruke den samme logikken på en hvilken som helst matrise.

La oss se det enkle eksemplet for å få minimum antall av en matrise ved hjelp av en metode.

 //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 }} 
Test det nå

Produksjon:

 3 

Anonym Array i Java

Java støtter funksjonen til en anonym matrise, slik at du ikke trenger å deklarere matrisen mens du sender en matrise til metoden.

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

Returnerer Array fra metoden

Vi kan også returnere en matrise fra metoden i 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++)>

ArrayIndexOutOfBoundsException

Java Virtual Machine (JVM) kaster en ArrayIndexOutOfBoundsException hvis lengden på matrisen er negativ, lik matrisestørrelsen eller større enn matrisestørrelsen mens den krysser matrisen.

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

Flerdimensjonal matrise i Java

I slike tilfeller lagres data i rad- og kolonnebasert indeks (også kjent som matriseform).

Syntaks for å deklarere flerdimensjonal matrise i Java

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

Eksempel for å instansiere Multidimensional Array i Java

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

Eksempel for å initialisere Multidimensional Array i 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; 

Eksempel på multidimensjonal Java Array

La oss se det enkle eksemplet for å deklarere, instansiere, initialisere og skrive ut den 2Dimensjonale matrisen.

string.replaceall i java
 //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 i Java

Hvis vi lager et oddetall kolonner i en 2D-matrise, er det kjent som en tagget matrise. Med andre ord er det en rekke matriser med forskjellig antall kolonner.

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

Hva er klassenavnet til Java-arrayen?

I Java er en matrise et objekt. For matriseobjekter opprettes en proxy-klasse hvis navn kan fås ved hjelp av getClass().getName()-metoden på objektet.

 //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); }} 
Test det nå

Produksjon:

 I 

Kopiere en Java-array

Vi kan kopiere en matrise til en annen ved hjelp av arraycopy()-metoden til System-klassen.

Syntaks for arraycopy-metoden

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

Eksempel på kopiering av en matrise i 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)); } } 
Test det nå

Produksjon:

 caffein 

Kloning av en matrise i Java

Siden Java-array implementerer Cloneable-grensesnittet, kan vi lage klonen til Java-arrayen. Hvis vi lager klonen til en endimensjonal matrise, skaper den den dype kopien av Java-matrisen. Det betyr at den vil kopiere den faktiske verdien. Men hvis vi lager klonen til en flerdimensjonal matrise, skaper den den grunne kopien av Java-matrisen som betyr at den kopierer referansene.

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

Produksjon:

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

Tillegg av 2 matriser i Java

La oss se et enkelt eksempel som legger til to matriser.

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

Multiplikasjon av 2 matriser i Java

Ved matrisemultiplikasjon multipliseres et enradselement i den første matrisen med alle kolonnene i den andre matrisen som kan forstås av bildet gitt nedenfor.

Matrisemultiplikasjon i Java

La oss se et enkelt eksempel for å multiplisere to matriser med 3 rader og 3 kolonner.

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

Relaterte temaer

1) Java-program for å kopiere alle elementene i en array til en annen array

2) Java-program for å finne frekvensen til hvert element i matrisen

3) Java-program for å venstrerotere elementene i en matrise

4) Java-program for å skrive ut de dupliserte elementene i en matrise

5) Java-program for å skrive ut elementene i en matrise

6) Java-program for å skrive ut elementene i en matrise i omvendt rekkefølge

7) Java-program for å skrive ut elementene i en matrise på jevn posisjon

8) Java-program for å skrive ut elementene i en matrise som er tilstede på oddetall

9) Java-program for å skrive ut det største elementet i en matrise

10) Java-program for å skrive ut det minste elementet i en matrise

11) Java-program for å skrive ut antall elementer som finnes i en matrise

12) Java-program for å skrive ut summen av alle elementene i matrisen

13) Java-program for å høyrerotere elementene i en matrise

14) Java-program for å sortere elementene i en matrise i stigende rekkefølge

15) Java-program for å sortere elementene i en matrise i synkende rekkefølge

16) Finn det tredje største tallet i en matrise

17) Finn det andre største tallet i en matrise

18) Finn det største tallet i en matrise

19) Finn det andre minste tallet i en matrise

20) Finn det minste tallet i en matrise

21) Fjern duplikatelement i en matrise

22) Legg til to matriser

23) Multipliser to matriser

24) Skriv ut oddetall og partall fra en matrise

25) Transponer matrise

26) Java-program for å trekke fra de to matrisene

27) Java-program for å bestemme om en gitt matrise er en identitetsmatrise

28) Java-program for å bestemme om en gitt matrise er en sparsom matrise

29) Java-program for å finne ut om to matriser er like

30) Java-program for å vise den nedre trekantede matrisen

31) Java-program for å vise den øvre trekantede matrisen

32) Java-program for å finne frekvensen av oddetall og partall i den gitte matrisen

33) Java-program for å finne produktet av to matriser

34) Java-program for å finne summen av hver rad og hver kolonne i en matrise

35) Java Program for å finne transponeringen av en gitt matrise