Posts

Showing posts from October, 2011

Matrix in Java

Image
Creating a matrix 
In this example  you will learn about array and matrix. An array is the collection of same data type values. If we create a variable of integer type then, the array of int can only store the int values. It can't store other than int data type. Matrix: A matrix is a collection of data in rows and columns format. In this program we are going to implement a matrix. To make a program over the two dimensional array,irst of all we have to declare class named as 

Java program to search an element in an array

Search an element in an arrayFind an element from an array using Linear Searching. Linear searching is a good way to find an element from the array. The array can be of any order, it checks whether a certain element (number , string , etc. ) is in a specified array or not. Basically it is used for small arrays. In the given code, we have allowed the user to enter the numbers to be stored into the arrays. If the element is found we can return the index where the element is located in the array. If the element is not found we can return -1.

Mean - average of array in java

For this, first of all we have to define a class name "ArrayAverage" that has int type array to contain some values .Now we take a double type data that calculates the average of array ( avg = sum / array.length). And finally it will display the result on the command prompt with message by using the System.out.println().

Maximum element in an array in java

To find the maximum value in an array :
Assign the first (or any) array element to the variable that will hold the maximum valueLoop through the remaining array elements, starting at the second element (subscript 1). When a larger value is found, that becomes the new maximum. To make this work with object types (eg String), you will have to change the type declarations of course, but you will also have to change the comparison to use .compareTo(), which must be defined for that object type. The minimum can be found by the same algorithm, simply inverting the test. It's also easy to add a test for the minimum to this loop so that both the maximum and minimum are determined within the same loop. There is one common variation on this -- sometimes it isn't the maximum value that is desired, but the index of the maximum value (eg, so it can be changed).

Java version of the Maximum element problem is given below :
/** * To change this template, choose Tools | Templates * and open …

Declaring an array in java

An array variable is like other variables -- you must declare it, which means you must declare the type of elements that are in an array. All elements must be the same type. Write the element type name, then "[]", then the name of the array variable. The declaration allocates only enough space for a reference to an array (typically 4 bytes), but doesn't create the actual array object.

Subtraction of two matrix in java

we are going to calculate the subtraction of two matrix and containing its rows and columns. See below for better understanding to this:  In this program we are going to calculate the subtraction of two matrix. To make this program, we need to declare two dimensional array of type integer. Now we need to make a matrix out of it. To make the matrix we will use the for loop.

Addition of two matrix in java

we are going to calculate the sum of two matrix and containing its rows and columns. See below for better understanding to this:  In this program we are going to calculate the sum of two matrix. To make this program, we need to declare two dimensional array of type integer.