Arrays in Java


                                                             Arrays:-
Array: - Array is the collection of variables belonging to the same data type. Array is used to store a collection of data. If any user want to store marks of 50 students. This can be possible only by creating 50 variables individually but, this process is very difficult. This type of problem can be handled by using array.

Advantage of Java Array
1) Code Optimization: - It makes the code optimized; we can retrieve or sort the data easily.
2) Random access: - We can get any data located at any index position.

Disadvantage of Java Array
1) Size Limit: - We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.

Types of Array in java
There are two types of array.
1) Single Dimensional Array
2) Multidimensional Array

//Write a program for Array
import java.util.Scanner;
class array
{
public static void main(String args[])
{
          Scanner in = new Scanner(System.in);
          int i;
          int arr[] = new int[5];
for(i=0;i<5;i++)
{
          System.out.println("Enter the element of array ");
          arr[i] = in.nextInt();
}
for(i=0;i<5;i++)
{
          System.out.println("Element of array = "+arr[i]);
}
}
}

No comments:

Post a Comment