Skip to main content

Posts

Showing posts from January, 2020

Remove Element From an Array in Java

In this post we’ll see how to remove or delete element from an array in Java. You have several options to do that task of removing an element from an array. Write your own logic. See example . Use System.arraycopy() method for removing element from an array. See example . Use Apache Commons library. In that library there is a ArrayUtils class that has remove method for that purpose. See example . Use ArrayList to remove an element. You will need to convert array to ArrayList and then back to array. See example . Writing your own logic If you have to write your own Java program to remove element from an array then you will have to shift all the elements, to the left, that come after the element that has to be removed . Another thing that you will have to consider is; once created array is of fixed length so after shifting all the elements there will still be one space left in the array and that space will be filled by repeating the last element. Let’s see with a

Matrix Subtraction Java Program

This post shows a Java program to subtract two matrices. When you subtract two matrices you subtract the element at the same index in both matrices so you’ll subtract the element at index (0, 0) in the first matrix with the element at index (0, 0) in the second matrix to get the element at (0, 0) in the resultant matrix. Also note that both of the matrix have to be of the same order for subtraction. For example – If you are subtracting two 3 X 3 matrices. Java program for matrix subtraction import java.util.Scanner; public class MatrixSubtraction { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter number of rows and columns in the matrix : "); int row = in.nextInt(); int column = in.nextInt(); // First matrix int[][] matrix1 = prepareMatrix(row, column); // Second matrix int[][] matrix2 = prepareMatrix(row, column); // Subtraction result stored in this matrix int sub

Matrix Addition Java Program

This post shows a Java program to add two matrices. When you add two matrices you add the element at the same index in both matrices so you’ll add the element at index (0, 0) in the first matrix with the element at index (0, 0) in the second matrix to get the element at (0, 0) in the resultant matrix. Also note that both of the matrix have to be of the same order for addition. For example – If you are adding two 3 X 3 matrices. Java program for matrix addition import java.util.Scanner; public class MatrixAddition { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter number of rows and columns in the matrix : "); int row = in.nextInt(); int column = in.nextInt(); // First matrix int[][] matrix1 = prepareMatrix(row, column); // Second matrix int[][] matrix2 = prepareMatrix(row, column); // addition result stored in this matrix int addedMatrix[][] = new int[row][column]; //

Matrix Multiplication Java Program

This post shows a Java program to multiply two matrices. To multiply one matrix with another you need to do a dot product of rows and columns. Let’s see it with an example where you are trying to multiply a 3X3 matrix with a 3X2 matrix. How matrix multiplication happens here using dot product can be explained as follows- First row of first matrix is multiplied with the first column of second matrix. s11 = r11Xp11 + r12Xp21 + r13Xp31 Second row of first matrix is multiplied with the second column of second matrix. s12 = r11Xp12 + r12Xp22 + r13Xp32 Then second row of first matrix is multiplied with the first column of second matrix. s21 = r21Xp11 + r22Xp21 + r23Xp31 and so on... Java program for matrix multiplication In the matrix multiplication Java program, initially user is prompted to enter the matrices. You can also check that the number of columns in the first matrix are equal to the number of rows in the second matrix. Then using these two matrices you can do