Skip to content

Array1

Introduction to Java Arrays

In Java, an array is a collection of elements of the same data type, stored in a contiguous block of memory. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. You can access elements using an index, where the index starts from 0 for the first element.

Array Basics

Declaration and Initialization:

int[] numbers = new int[5]; // declares an array of 5 integers
numbers[0] = 10; // assigns 10 to the first element

Accessing Elements:
You can access and modify elements in the array using their index.

int firstNumber = numbers[0]; // gets the first element
numbers[1] = 20; // sets the second element to 20

Array Length:
Arrays have a fixed size, which is determined when they are created. You can find the length of an array using the .length attribute.

int length = numbers.length; // gets the length of the array

Short Video Lecture

(Video here)

VisualCodeChat Tutoring (Exercise: Find Max)

Interact and Learn with VisualCodeChat!

After-class Exercises

(Exercises here)