Arrays
Difinition :
An array represent a group of the element of the same data type .It can store a group of element. So we can store a group of int values or a group of float value or a group of string in the array. But we can not store some int float values in the array
* On which memory ,arrays are create in java?
Array are create on dynamic memory by JVM, There is no question of static memory in java, everything (variable ,array .object etc.)is create on dynamic memory only.
Types Of Array
Array are genarally categorized into two parts as described here:
*Single dimensional arrays (or 1D ,arrays)
* Multi dimensional array (or 2D ,3D, arrays)
Single Dimensional Array (1D array)
A one dimesional (1D )or single dimensional array represent a row or a column of elements. For
example , the marks obtained by a student in 5 different subject can be represent by a 1D array
because these marks can be written as row or as a column.
Example : ---------------------------
class Array1D
{
public static void main(String[]args)
{
// declare and initialize the array Array1D That is Output here
int arr[]={10,20,30,40,50};
// dispaly all the five elements
for(int i=0;i<5;i++)
{
System.out.println(arr[i]);
}
}
}
Multi Dimensional array(2D,3D.,... array)
Multi Dimensional array repersent 2D, 3D ..array which are combination of several earlier type
of array. For example, a two dimensional array is a combination of two or more (1D) one dimensional array. Similarly ,a three dimensional array is a combinational of two or more (2D) twodimensional array . Let us understand the two dimensional array now.
Example ................................................
import java.io.*;
class Array2D
{
public static void main(String[]args) throws IOException
{
// to accept data from keybord
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// ask how many subject
System.out.print("How many Subject?");
int n=Integer.parseInt(br.readLine());
//create 1D array with size n
int[]marks =new int[n];
// store element into the array
for(int i=0; i<n; i++)
{
System.out.println("Enter the Marks =");
marks[i]= Integer.parseInt(br.readLine());
}
// find total marks
int tot=0;
for(int i=0; i<n; i++)
tot+=marks[i];
// display total marks
System.out.println("Total Marks="+tot);
//find persentage That is Output Array2D
float persent=(float)tot/n;
System.out.println("persentage"+persent);
}
}
0 comments:
Post a Comment