Implementation of array in Java

  • Declares an integer array arr and initializes it with values using the array initializer syntax.
  • Accesses the element at index 4 using arr[4] and prints its value.
  • Modifies the element at index 4 by assigning a new value (20) to arr[4] and prints the modified value.

Java




public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};  // Initializing an array with values
 
        // Accessing elements
        System.out.println("Element at index 4: " + arr[4]);
 
        // Modifying elements
        arr[4] = 20;
        System.out.println("Modified element at index 4: " + arr[4]);
    }
}


Javascript




class Main {
    static main() {
        const arr = [1, 2, 3, 4, 5];  // Initializing an array with values
 
        // Accessing elements
        console.log("Element at index 4: " + arr[4]);
 
        // Modifying elements
        arr[4] = 20;
        console.log("Modified element at index 4: " + arr[4]);
    }
}
 
// Call the main method
Main.main();


Output

Element at index 4: 5
Modified element at index 4: 20

Implement Arrays in different Programming Languages

Arrays are one of the basic data structures that should be learnt by every programmer. Arrays stores a collection of elements, each identified by an index or a key. They provide a way to organize and access a fixed-size sequential collection of elements of the same type. In this article, we will learn about the basic use of array in most common languages like C, C++, Java, Python, etc.

Table of Content

  • What is an Array?
  • Implementation of array in C++:
  • Implementation of array in C:
  • Implementation of array in Java:
  • Implementation of array in Python:
  • Implementation of array in C#:
  • Implementation of array in JavaScript:
  • Pros and Cons of array:

Similar Reads

What is an Array?

Arrays are data structures that store collections of elements in contiguous memory locations. Each element is identified by an index or key, allowing for efficient random access. Arrays offer advantages like constant-time access and memory efficiency. However, they have disadvantages such as a fixed size and inefficient insertion/deletion....

Implementation of array in C++:

Declares an integer array arr with a size of 5 and initializes it with values. Accesses the element at index 3 using arr[3] and prints its value. Modifies the element at index 3 by assigning a new value (15) to arr[3] and prints the modified value....

Implementation of array in C:

...

Implementation of array in Java:

Declares an integer array arr with a size of 5 and initializes it with values. Accesses the element at index 2 using arr[2] and prints its value. Modifies the element at index 2 by assigning a new value (10) to arr[2] and prints the modified value....

Implementation of array in Python:

...

Implementation of array in C#:

Declares an integer array arr and initializes it with values using the array initializer syntax. Accesses the element at index 4 using arr[4] and prints its value. Modifies the element at index 4 by assigning a new value (20) to arr[4] and prints the modified value....

Implementation of array in JavaScript:

...

Pros and Cons of array:

...