Static Arrays
- Static arrays are a fundamental data structure in programming, designed to store a fixed number of elements of the same data type.
- Unlike dynamic arrays, which can resize during runtime, static arrays have a predetermined size set during their initialisation.
Static arrays are also known as fixed-size arrays because of their property.
Analogy- Think of a static array as a row of lockers, each with a fixed position.
- You can store items in each locker, but you can't add more lockers or remove existing ones.
Declaring and Initialising Static Arrays (in Java)
There are different methods for starting to work with static arrays in Java:
Declaration: Specify the data type and name of the array.
int[] numbers;Initialisation: Define the size and allocate memory.
numbers = new int[10]; // of length 10Combined declaration and initialisation: Often used together to save space.
int[] numbers = new int[10];Initialisation with values:
int[] numbers = {1, 2, 3, 4, 5, 6};When initialising an array with specific values, the size is automatically determined by the number of elements provided.