logo

מערכי Java

בדרך כלל, מערך הוא אוסף של אלמנטים מסוג דומה שיש להם מיקום זיכרון רציף.

מערך Java הוא אובייקט המכיל אלמנטים מסוג נתונים דומה. בנוסף, הרכיבים של מערך מאוחסנים במיקום זיכרון רציף. זהו מבנה נתונים שבו אנו מאחסנים אלמנטים דומים. אנו יכולים לאחסן רק קבוצה קבועה של אלמנטים במערך Java.

מערך ב-Java מבוסס על אינדקס, האלמנט הראשון של המערך מאוחסן באינדקס ה-0, האלמנט השני מאוחסן באינדקס הראשון וכן הלאה.

שלא כמו C/C++, אנו יכולים לקבל את אורך המערך באמצעות איבר האורך. ב-C/C++, עלינו להשתמש באופרטור sizeof.

ב-Java, array הוא אובייקט של מחלקה שנוצרה באופן דינמי. מערך Java יורש את מחלקת ה-Object, ומיישם את הממשקים הניתנים ל-Serializable וגם ה-Cloneable. אנו יכולים לאחסן ערכים או אובייקטים פרימיטיביים במערך ב-Java. כמו C/C++, אנו יכולים גם ליצור מערכים חד-ממדיים או רב-ממדיים ב-Java.

יתרה מכך, Java מספקת את התכונה של מערכים אנונימיים שאינה זמינה ב-C/C++.

מערך Java

יתרונות

    אופטימיזציה של קוד:זה הופך את הקוד למוטב, אנחנו יכולים לאחזר או למיין את הנתונים ביעילות.גישה אקראית:אנחנו יכולים לקבל כל מידע שנמצא במיקום אינדקס.

חסרונות

    מגבלת גודל:אנחנו יכולים לאחסן רק את הגודל הקבוע של אלמנטים במערך. זה לא מגדיל את גודלו בזמן ריצה. כדי לפתור בעיה זו, נעשה שימוש במסגרת אוסף ב-Java אשר גדל אוטומטית.

סוגי מערך ב-java

ישנם שני סוגים של מערך.

  • מערך יחיד ממדי
  • מערך רב מימדי

מערך יחיד ממדי ב-Java

תחביר להכרזה על מערך ב-Java

גודל של פיתון
 dataType[] arr; (or) dataType []arr; (or) dataType arr[]; 

מופע של מערך ב-Java

 arrayRefVar=new datatype[size]; 

דוגמה למערך Java

בואו נראה את הדוגמה הפשוטה של ​​מערך java, שבו אנחנו הולכים להכריז, להפעיל, לאתחל ולעבור מערך.

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

הכרזה, מופע ואיתחול של מערך Java

אנו יכולים להכריז, ליצור מופע ולאתחל את מערך ה-Java ביחד על ידי:

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

בואו נראה את הדוגמה הפשוטה להדפסת מערך זה.

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

עבור כל לולאה עבור מערך Java

אנחנו יכולים גם להדפיס את מערך Java באמצעות לכל לולאה . ה-Java for-each loop מדפיס את רכיבי המערך בזה אחר זה. הוא מחזיק אלמנט מערך במשתנה, ואז מבצע את גוף הלולאה.

התחביר של לולאה לכל אחד ניתן להלן:

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

תן לנו לראות את הדוגמה של הדפסת האלמנטים של מערך Java באמצעות לולאה עבור כל.

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

תְפוּקָה:

 33 3 4 5 

העברת מערך לשיטה ב-Java

אנחנו יכולים להעביר את מערך ה-Java למתודה כדי שנוכל לעשות שימוש חוזר באותו היגיון בכל מערך.

בואו נראה את הדוגמה הפשוטה לקבלת המספר המינימלי של מערך באמצעות שיטה.

 //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 }} 
בדוק את זה עכשיו

תְפוּקָה:

 3 

מערך אנונימי בג'אווה

Java תומכת בתכונה של מערך אנונימי, כך שאין צורך להכריז על המערך בזמן העברת מערך לשיטה.

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

החזרת מערך מהשיטה

אנחנו יכולים גם להחזיר מערך מהשיטה ב-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++)>

אינדקס מערך מחוץ לתחום Exception

ה-Java Virtual Machine (JVM) זורק ArrayIndexOutOfBoundsException אם אורך המערך שלילי, שווה לגודל המערך או גדול מגודל המערך בזמן חציית המערך.

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

מערך רב מימדי ב-Java

במקרה כזה, הנתונים מאוחסנים באינדקס מבוסס שורות ועמודות (הידוע גם כצורת מטריצה).

תחביר להכרזה על מערך רב ממדי ב-Java

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

דוגמה ליצירת מערך רב מימדי ב-Java

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

דוגמה לאתחול מערך רב מימדי ב-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; 

דוגמה למערך Java Multi-dimensional

בואו נראה את הדוגמה הפשוטה להכרזה, מופע, אתחול והדפסת המערך הדו-מימדי.

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

מערך משונן ב-Java

אם אנו יוצרים מספר אי-זוגי של עמודות במערך דו-ממדי, זה ידוע כמערך משונן. במילים אחרות, זהו מערך של מערכים עם מספר שונה של עמודות.

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

מהו שם המחלקה של מערך Java?

ב-Java, מערך הוא אובייקט. עבור אובייקט מערך, נוצרת מחלקה פרוקסי שניתן להשיג את שמה על ידי שיטת getClass().getName() באובייקט.

 //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); }} 
בדוק את זה עכשיו

תְפוּקָה:

 I 

העתקת מערך Java

אנו יכולים להעתיק מערך לאחר בשיטת arraycopy() של מחלקה System.

תחביר של שיטת arraycopy

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

דוגמה להעתקת מערך ב-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)); } } 
בדוק את זה עכשיו

תְפוּקָה:

 caffein 

שיבוט מערך ב-Java

מכיוון שמערך Java מיישם את ממשק Cloneable, אנו יכולים ליצור את השיבוט של מערך Java. אם ניצור את השיבוט של מערך חד מימדי, זה יוצר את העותק העמוק של מערך Java. זה אומר שזה יעתיק את הערך האמיתי. אבל, אם אנחנו יוצרים את השיבוט של מערך רב מימדי, זה יוצר את העותק הרדוד של מערך Java מה שאומר שהוא מעתיק את ההפניות.

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

תְפוּקָה:

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

תוספת של 2 מטריצות בג'אווה

בוא נראה דוגמה פשוטה שמוסיפה שתי מטריצות.

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

כפל של 2 מטריצות ב-Java

במקרה של כפל מטריצה, אלמנט של שורה אחת של המטריצה ​​הראשונה מוכפל בכל העמודות של המטריצה ​​השנייה, שניתן להבין מהתמונה המופיעה למטה.

כפל מטריקס ב-Java

בוא נראה דוגמה פשוטה להכפלת שתי מטריצות של 3 שורות ו-3 עמודות.

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

נושאים קשורים

1) תוכנית Java להעתקת כל הרכיבים של מערך אחד למערך אחר

2) תוכנית Java למציאת התדירות של כל אלמנט במערך

3) תוכנית Java לסובב שמאלה את האלמנטים של מערך

4) תוכנית Java להדפסת רכיבים כפולים של מערך

5) תוכנית Java להדפסת הרכיבים של מערך

6) תוכנית Java להדפיס את הרכיבים של מערך בסדר הפוך

7) תוכנית Java להדפיס את הרכיבים של מערך במיקום שווה

8) תוכנית Java להדפסת הרכיבים של מערך במיקום אי זוגי

9) תוכנית Java להדפסת האלמנט הגדול ביותר במערך

10) תוכנית Java להדפיס את האלמנט הקטן ביותר במערך

11) תוכנית Java להדפסת מספר האלמנטים הקיימים במערך

12) תוכנית Java להדפסת סכום כל הפריטים במערך

13) תוכנית Java לסובב ימינה את האלמנטים של מערך

14) תוכנית Java למיון האלמנטים של מערך בסדר עולה

15) תוכנית Java למיון האלמנטים של מערך בסדר יורד

16) מצא את המספר השלישי בגודלו במערך

17) מצא את המספר השני בגודלו במערך

18) מצא את המספר הגדול ביותר במערך

19) מצא את המספר השני הקטן ביותר במערך

20) מצא את המספר הקטן ביותר במערך

21) הסר רכיב כפול במערך

22) הוסף שתי מטריצות

23) הכפל שתי מטריצות

24) הדפס מספר זוגי וזוגי ממערך

25) טרנספוזיציה מטריצה

26) תוכנית Java להפחתת שתי המטריצות

27) תוכנית Java כדי לקבוע אם מטריצה ​​נתונה היא מטריצת זהות

28) תוכנית Java כדי לקבוע אם מטריצה ​​נתונה היא מטריצה ​​דלילה

29) תוכנית Java כדי לקבוע אם שתי מטריצות שוות

30) תוכנית Java להצגת המטריצה ​​המשולשת התחתונה

31) תוכנית Java להצגת המטריצה ​​המשולשת העליונה

32) תוכנית Java למציאת התדירות של מספרים אי-זוגיים וזוגיים במטריצה ​​הנתונה

33) תוכנית Java למציאת התוצר של שתי מטריצות

34) תוכנית Java למציאת הסכום של כל שורה וכל עמודה של מטריצה

35) תוכנית Java למציאת הטרנספוזיציה של מטריצה ​​נתונה