Concept of Array in Java
An array is a collection of similar data types. Array is a container object that hold values of homogenous type. It is also known as static data structure because size of an array must be specified at the time of its declaration.
An array can be either primitive or reference type. It gets memory in heap area. Index of array starts from zero to size-1.
Array Declaration
Syntax :
datatype[] identifier;
or
datatype identifier[];
Both are valid syntax for array declaration. But the former is more readable.
Example :
int[] arr;
char[] arr;
short[] arr;
long[] arr;
int[][] arr; //two dimensional array.
Initialization of Array
new
operator is used to initialize an array.
Example :
int[] arr = new int[10]; //10 is the size of array.
or
int[] arr = {10,20,30,40,50};
Accessing array element
As mention earlier array index starts from 0. To access nth element of an array. Syntax
arrayname[n-1];
Example : To access 4th element of a given array
int[] arr={10,20,30,40};
System.out.println("Element at 4th place"+arr[3]);
The above code will print the 4th element of array arr on console.
foreach or enhanced for loop
J2SE 5 introduces special type of for loop called foreach loop to access elements of array. Using foreach loop you can access complete array sequentially without using index of array. Let us see an example of foreach loop.
class Test
{
public static void main(String[] args)
{
int[] arr={10,20,30,40};
for(int x:arr)
{
System.out.println(x);
}
}
}
Output :
20
30
40
Array Program To Find Sum of Diagonals
import java.io.*;public class sumofdiagonals
{ public static void main(String args[]) throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(read); int arr[][] = {{6,9,0,8},{3,2,1,5},{2,9,0,1},{4,1,9,6}};
int sum=0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(i==j)
sum+=arr[i][j];
}
} System.out.println(sum);
There are some more java programs .You can click on the various links below
swapping-two-numbers-without-using-third-variable
printing-the-initials-of-a-name-or-first-letters-of-each-word-of-a-sentence
A menu driven calculator program
Finding whether an number is an automorphic
Program to find sum of series 1/1!+ 2/2! + 3/3!.........N/N!
Java Program To Print Number Triangle Reversing A Number
3 Comments
Nice information, thanks for sharing
ReplyDeletehttp://ibadanland.org/forums.php?action=allposts&t=4491#4600
Great information
ReplyDeletedata science training in Marathahalli
best data science courses in Marathahalli
data science institute in Marathahalli
data science certification Marathahalli
data analytics training in Marathahalli
data science training institute in Marathahalli
Nice Blog
ReplyDeletepython django training in Bangalore
python training centers in Bangalore
python scripting classes in Bangalore
python certification course in Bangalore
python training courses in Bangalore
python institutes in Bangalore
python training in Bangalore
python course in Bangalore
best python training institute in Bangalore