Single Dimension Array in Java

Single Dimension Array in Java

Single Dimension Array In Java Notes

An array is a continuous location in memory where elements of the same type (datatype) are stored. An array consists of several locations, as defined, and are referenced by a common name. The different locations in an array are accessed by indeces as shown in the figure below.

To use an array in a program, you must declare an array first, then assign values to it and then access it within a loop.

Array Declaration

  • To declare an integer array:

     int a[] = new int[10];
  • To declare a String array:

     String a[] = new String[10];
  • To assign values to an array:

     System.out.println("Enter 10 integer values");
        for(i = 0; i < 10; i++){
          a[i] = Integer.parseInt(br.readLine());
       }catch(Exception e){}
  • Accessing array elements

    for(i= 0; i < 10; i++){

Initializing arrays

  • Integer array

     int a[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  • String array

     String str[] = {"Programming", "is", "fun"};
  • Float array:

     float a[] = {25.4f, 24.1f, 26.3f, 29.0f};
  • Character array:

     char ca[] = {'a', 'e', 'i', 'o', 'u'};