DEV Community

Pavithra C
Pavithra C

Posted on

Day-40:Array in java

Array
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Image description

The following program, ArrayDemo, creates an array of integers, puts some values in the array, and prints each value to standard output.

class ArrayDemo {
    public static void main(String[] args) {
        // declares an array of integers
        int[] anArray;

        // allocates memory for 10 integers
        anArray = new int[10];

        // initialize first element
        anArray[0] = 100;
        // initialize second element
        anArray[1] = 200;
        // and so forth
        anArray[2] = 300;
        anArray[3] = 400;
        anArray[4] = 500;
        anArray[5] = 600;
        anArray[6] = 700;
        anArray[7] = 800;
        anArray[8] = 900;
        anArray[9] = 1000;

        System.out.println("Element at index 0: "
                           + anArray[0]);
        System.out.println("Element at index 1: "
                           + anArray[1]);
        System.out.println("Element at index 2: "
                           + anArray[2]);
        System.out.println("Element at index 3: "
                           + anArray[3]);
        System.out.println("Element at index 4: "
                           + anArray[4]);
        System.out.println("Element at index 5: "
                           + anArray[5]);
        System.out.println("Element at index 6: "
                           + anArray[6]);
        System.out.println("Element at index 7: "
                           + anArray[7]);
        System.out.println("Element at index 8: "
                           + anArray[8]);
        System.out.println("Element at index 9: "
                           + anArray[9]);
    }
} 
Enter fullscreen mode Exit fullscreen mode

output:

The output from this program is:

Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000

Enter fullscreen mode Exit fullscreen mode

Declaring a Variable to Refer to an Array

The preceding program declares an array (named anArray) with the following line of code:

// declares an array of integers
int[] anArray;

Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty).[TBD]

Similarly, you can declare arrays of other types:

byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;

public class Main {
    public static void main(String[] args)
    {

        // initializing array
        int[] arr = { 1, 2, 3, 4, 5 };

        // size of array
        int n = arr.length;

        // traversing array
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create an Array To create an array, you need to allocate memory for it using the new keyword:
// Creating an array of 5 integers
int[] numbers = new int[5]; 
Enter fullscreen mode Exit fullscreen mode
  1. Access an Element of an Array[TBD] We can access array elements using their index, which starts from 0:
// Setting the first element of the array
numbers[0] = 10; 

// Accessing the first element
int firstElement = numbers[0]; 
Enter fullscreen mode Exit fullscreen mode
  1. Change an Array Element To change an element, assign a new value to a specific index:
    // Changing the first element to 20
    numbers[0] = 20; 

Enter fullscreen mode Exit fullscreen mode
  1. Array Length We can get the length of an array using the length property:
    // Getting the length of the array
    int length = numbers.length; 


Enter fullscreen mode Exit fullscreen mode

Creating, Initializing, and Accessing an Arrays in Java

For understanding the array we need to understand how it actually works. To understand this follow the flow mentioned below:

  1. Declare
  2. Initialize
  3. Access

1. Declaring an Array


Enter fullscreen mode Exit fullscreen mode

Reference link:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Top comments (0)