JAVA Array Introduction

  • An array is nothing but a variable having capability to contain more than one similar types of data items in linear manner.
  • Array is also called as linear data structure.
  • The length of an array is established when the array is created. After creation, its length is fixed.
  • Each item in an array is called an element, and each element is accessed by its numerical index.
  • Here array index begins with 0. For example, the 9th element will be accessed at index 8.
  • Java supports two types of arrays, such as one-dimensional array and multi-dimensional array.
  • The array taking one subscript([]) is known as one-dimensional array.
  • The array taking more than one subscript is known as multi-dimensional array.

 

// Java program to illustrate creating an array
// of integers,  puts some values in the array,
// and prints each value to standard output.

class ArraySample {
 public static void main(String[] args) {
  // declares an Array of integers.
  int[] arr;

  // allocating memory for 5 integers.
  arr = new int[5];

  // initialize the first elements of the array
  arr[0] = 10;

  // initialize the second elements of the array
  arr[1] = 20;

  //so on...
  arr[2] = 30;
  arr[3] = 40;
  arr[4] = 50;

  // accessing the elements of the specified array
  for (int i = 0; i < arr.length; i++)
   System.out.println("Element at index " + i +
    " : " + arr[i]);
 }
}

Output

Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50

Limitations

  • Array size is fixed.
  • Containing homogeneous data items.
  • Readymade methods are not supported.
Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment