Today we are discussing about array
Now what is array:
An array is a data structure, which can store a fixed-size collection of elements of the same data type. ... An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
we can declare the array in different way. In this blog we learning the major way to describe/declare the array.
note: array start with the index of zero(0)
don't forget that ( array=n-1)
now this blog is little bit long.
package com.ritesh;
public class arr {
public static void main(String[] args) {
arr_type1();
arr_type2();
}
public static void arr_type1()
{
String [] arr={"Ram","Shyam","Hari","sita","gopi","katla","kalla katla","hurra hutti"}; //defining the array
//System.out.println(arr[0]); //printing the array
//printing the array using for loop
System.out.println("using for loop");
System.out.println();
for (String s : arr) {
System.out.println(s);
}
System.out.println();
//printing using while loop
int p=0;
System.out.println("using while loop");
System.out.println();
while (p<arr.length)
{
System.out.println(arr[p]);
p++;
}
int x=0;
System.out.println();
System.out.println("using do while loop");
do {
System.out.println(arr[x]);
x++;
}
while (x < arr.length);
}
public static void arr_type2()
{
int []num=new int[5];
num[0]=100;
num[1]=200;
num[2]=300;
num[3]=400;
num[4]=500;
int sd=0; //array start with the index of zero
System.out.println();
System.out.println("by the another type of array deceleration ");
while(sd<num.length)
{
System.out.println(num[sd]);
sd++;
}
}
}
Comments
Post a Comment