Skip to content

Arrays

This content is for DSA. Switch to the latest version for up-to-date documentation.

Image

Fixed-size collection of elements of the same type, stored in contiguous memory locations.

  • Fixed size: Size is set when created and cannot change.
  • Contiguous memory: Elements are stored next to each other.
  • Random access: Any element can be accessed directly by index.
  • Same type: All elements must be of the same data type.
  • Allows duplicates: Repeated values are permitted.
  • Access: Direct access using index.
  • Insertion: Need to shift elements.
  • Update: Directly modify an element by index.
  • Deletion: Need to shift elements after deletion.
  • Fast access: Elements can be quickly accessed using their index.
  • Memory efficient: Elements are stored together in memory, reducing overhead.
  • Fixed size: Cannot be resized after creation.
  • Manual insertion and deletion: Adding or removing elements requires shifting other elements.
  • Memory waste: Unused space is wasted if the array is not fully filled.
// Declaration and initialization
int[] numbers = {1, 2, 3, 4, 5};
// Declare and set the size of the array
String[] students = new String[3];
// Insert elements
students[0] = "Dustin";
students[1] = "Rewish";
students[2] = "Ada Lovelace";
// Accessing an element
System.out.println(students[0]); // Prints "Dustin"
// Updating an element
students[0] = "Dustin VII"; // Updates "Dustin" to "Dustin VII"
System.out.println(students[0]);
// Deleting an element
students[2] = null; // Removes "Ada Lovelace"
System.out.println(students[2]); // Prints "null"
Built with passion by Ngineer Lab