Array in Java
Array in Java
An array is a collection of data having a similar datatype and with the same variable name but different in indexes- The array works on index bases.
- The array index starts with 0 to length -1.
- The array store the data having similar datatype only.
- The array can have one or more than one dimensions.
One Dimension Array
A one-dimensional array example is given below.
One dimensional array is kind of list which is able to hold the data on an index basis as you can see the array data is similar type "char" and the index used by them is 0-4 means array start storing the data from the 0th index, where the array has 10 lengths that's why it starts from 0th to 9th or we can say length-1 index.
Program:
/**
*
* @author Suraj Pratap Singh
*
*/
class Test{
public static void main(String arg[]) {
//creating array of size 10.
char arr[] = new char[10];
//passing the value to array index.
arr[0] = 'S';
arr[1] = 'U';
arr[2] = 'R';
arr[3] = 'A';
arr[4] = 'J';
System.out.println("Length of Array is: "+arr.length);
for(int i=0; i<arr.length; i++) {
System.out.println("Element ["+arr[i]+"] at index of ["+i+"]");
}
}
}
Output :
Length of Array is: 10
Element [S] at index of [0]
Element [U] at index of [1]
Element [R] at index of [2]
Element [A] at index of [3]
Element [J] at index of [4]
Element [ ] at index of [5]
Element [ ] at index of [6]
Element [ ] at index of [7]
Element [ ] at index of [8]
Element [ ] at index of [9]
We can also create array something like that
String arr[] = {"ABC", "BCD", "EFG"};
This is a string array and we can pass values on the creation time like above and then it automatically makes the array of size(index) 3.
We can also create array something like that
String arr[] = {"ABC", "BCD", "EFG"};
This is a string array and we can pass values on the creation time like above and then it automatically makes the array of size(index) 3.
Read below posts.Most Frequently Asked Java Interview Programs. PART-1Most Frequently Asked Java Interview Programs PART-2
Comments
Post a Comment