Array
Often we require to perform matrix addition in our program here is the example of mattix addition using 2d array
CODING:
import java.util.Scanner;
class MatrixAddition {
Scanner scan;
int matrix1[][], matrix2[][], sum[][];
int row, column;
void create() {
scan = new Scanner(System.in);
//Creation of first Matrix..
System.out.println("\nEnter number of rows ");
row = Integer.parseInt(scan.nextLine());
System.out.println("\nEnter number of columns ");
column = Integer.parseInt(scan.nextLine());
matrix1 = new int[row][column];
matrix2 = new int[row][column];
sum = new int[row][column];
System.out.println("Enter the data for first matrix :");
for(int i=0; i < row; i++) {
for(int j=0; j < column; j++) {
matrix1[i][j] = scan.nextInt();
}
}
//Creation of second matrix..
System.out.println("Enter the data for second matrix :");
for(int i=0; i < row; i++) {
for(int j=0; j < column; j++) {
matrix2[i][j] = scan.nextInt();
}
}
}
void display() {
System.out.println("The First Matrix is :");
for(int i=0; i < row; i++) {
for(int j=0; j < column; j++) {
System.out.print("\t" + matrix1[i][j]);
}
System.out.println();
}
System.out.println("\nThe Second Matrix is :");
for(int i=0; i < row; i++) {
for(int j=0; j < column; j++) {
System.out.print("\t" + matrix2[i][j]);
}
System.out.println();
}
}
void add() {
for(int i=0; i < row; i++) {
for(int j=0; j < column; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
System.out.println("\n\nThe Sum is :");
for(int i=0; i < row; i++) {
for(int j=0; j < column; j++) {
System.out.print("\t" + sum[i][j]);
}
System.out.println();
}
}
}
class MatrixAdditionExample
{
public static void main(String args[])
{
MatrixAddition x = new MatrixAddition();
x.create();
x.display();
x.add();
}
}
OUTPUT:
Array, Collections Framework
Before reading this tutorial you must have knowledge regarding basics of array list see tutorial on array list .To perform any operation on array list first it should be converted into an array of desired data in this example we are converting arraylist to int array to perform simple addition to this we have use two different method first is toArray which convert arraylist to array then we we use intValue to convert it into int array.
CODING:
import java.util.*;
class arraylisttoarray
{
public static void main(String args[])
{
ArrayList a1 = new ArrayList();
a1.add(1);
a1.add(2);
a1.add(2);
System.out.println("Initial size of a1:"+a1.size());
Object a[]=a1.toArray();
int sum=0;
for(int i=0;i<a.length;i++)
sum +=((Integer) a[i]).intValue();
System.out.println(sum);
}
}
OUTPUT:
Array
This tutorial illustrate an another method of array which is used to copy data from one array to another its general form is shown below
public static void arraycopy(Object src, int srcPos,Object dest, int destpos, int length)
here src object specify from where array is to be copied whereas dest object specify where to be copied.And int src specify starting postion of array and int destpos specify starting index of destination array int length shows length of character to be copied.
CODING:
class arraycopy
{
public static void main(String args[])
{
char[] From = { 'd', 'j', 'a', 'v', 'a', 'e',
'i', 'n', 'a' };
char[] To = new char[7];
System.arraycopy(From, 1, To, 0, 4);
System.out.println(new String(To));
}
}
OUTPUT:
Array
This tutorial illustrate how to fill values in array using fill() method in simple way.This method assign value to the array with a specified valued in array.It has two versions first version is shown here in this version specified value fill according to the given range by the user in this tutorial we fill 0 in 2 and 3 position it fill values from start-1 position its constructor shown below
static void fill(int array[],int start,int end,int value)
CODING:
import java.util.*;
class arrayfill
{
public static void main(String args[])
{
int arr[] = {100,20,40,60,70,10};
System.out.println("Original Content:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
Arrays.fill(arr,1,3,0);
System.out.println("After Filling:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
OUTPUT:
Array
In last our last tutorial we try to illustrate How to sort numbers in ascending order but as we can see it is difficult,instead of using loop for sorting we can also use sort() method it arrange array in ascending order it has two version.The first version,sort entire array.
static void sort(int array[])
And second method allow you to sort array within range.its shown below
staic void sort(int array[],int start,int end)
In this tutorial we use loop to print array,The implementation of sort method shown below
CODING:
import java.util.*;
class arraysort
{
public static void main(String args[])
{
int arr[] = {100,20,40,60,70,10};
System.out.println("Before sorting:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
Arrays.sort(arr);
System.out.println("After sorting:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
OUTPUT:
Array
There are many situation in which we need to sort numbers or word this tutorial provides a simple way to do this for string see this how to sort string in java .There are two type of number sorting ascending sorting and descending sorting in this tutorial we show how to sort number in descending order for ascending order sorting see ascending sorting in java.
CODING:
class intsort
{
public static void main(String args[])
{
int arr[] = {10,20,40,60,70,100};
System.out.println("Before sorting:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("After sorting:");
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[j]>arr[i])
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
System.out.println(arr[i]);
}
}
}
OUTPUT:
Array
There are many situation in which we need to sort numbers or word this tutorial provides a simple way to do this for string see this how to sort string in java .There are two type of number sorting ascending sorting and descending sorting in this tutorial we show how to sort number in ascending order for descending order sorting see descending sorting in java.
CODING:
class intsort
{
public static void main(String args[])
{
int arr[] = {100,30,10,20,60,70};
System.out.println("Before sorting:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("After sorting:");
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[j]<arr[i])
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
System.out.println(arr[i]);
}
}
}
OUTPUT: