Understanding Arrays in Java

Arrays in Java are used to store multiple values of the same data type under a single variable name. They offer a convenient way to manage collections of data elements. Arrays have a fixed size determined at the time of declaration, and elements can be accessed using an index starting from zero. Java arrays support various operations such as initialization, accessing elements, modifying elements, iterating through elements, and finding the length of the array. They are essential for many programming tasks, providing efficient storage and retrieval of data.

  • General format of creating a Single Dimensional Array:
  • type array_var=new type[size];

    It is explained as under:
    type :It determines the type of data array will hold.
    array_var :It is a common variable to store different data values.
    new :It is a special operator to allocate space in dynamic memory.
    size :It specifies the number of elements to stored in the array.
    For example: int m[]=new int[10]; or int []m=new int[10]


  • Some programs using Single Dimensional Array:
  • Question.1
    Write a program to accept 10 different numbers in S.D.A and search whether a given numbers is present or not and display the relevant message.
    Solution:

    import java.util.*; class PBAINST{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int arr[]=new int[10]; System.out.println("Enter the array:"); for(int i=0;i<10;i++){ arr[i]=sc.nextInt(); } System.out.println("Enter the number :"); int n=sc.nextInt(); int i,c=0,j; for(j=0;j<10;j++){ if(arr[j]==n){ c++; } } if(c==1){ System.out.print("number is present."); } else{ System.out.print("number is not present."); } } }

    OUTPUT:
    Enter the array:
    1
    2
    4
    6
    8
    7
    5
    3
    9
    10
    Enter the number :
    6
    number is present.

    Question.2:
    Write a program to find the sum and average of the numbers of the given Single Dimensional array. int m[]= {25,38,49,50,70,59}
    Solution:

    import java.util.*; class PBAINST{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int m[]={25,38,49,50,70,59}; int i; double sum=0; for(i=0;i<6;i++){ sum=sum+m[i]; } double avg; avg=(sum)/(6); System.out.println("Sum="+sum); System.out.println("Average="+avg); } }

    OUTPUT:
    Sum=291.0
    Average=48.5

  • Arrays in Java offer several advantages:
  • 1.Efficient Memory Usage :Arrays allocate memory in a contiguous block, which leads to efficient memory usage compared to some other data structures that might require additional memory overhead for pointers or metadata.
    2.Flexibility : Arrays can store elements of any data type, including primitive types and objects. This flexibility allows for the creation of arrays to suit various needs and data types.
    3.Compact Syntax :Java provides a compact syntax for array declaration and initialization,making it easy to work with arrays.

  • Practices Exercise:
  • 1. Write a program to store 10 temperatures °F in S.D.A and display all the temperatures after converting them into °C.
    2. Write a program to store 10 numbers in S.D.A and display only those which are prime.

  • Conclusion:
  • In Java, arrays offer efficient memory usage, indexed access, and predictable performance. They provide a compact syntax for declaration and initialization, supporting various data types. However, they have limitations like fixed size and inability to resize dynamically. Despite this, arrays remain fundamental for storing and manipulating collections of elements, forming the basis for many data structures and algorithms in Java programming. Understanding their strengths and limitations is crucial for effective application development.