Arrays

Java Arrays

  • collection of similar types of data

  • single, pre allocated chunk of contiguous elements (all of the same type), fixed in size and location

  • static in nature; once memory is allocated, it cannot be resized or freed dynamically

 DECLARATION!

dataType[] arrayName;

  • dataType - can be primitive data types like int, char, double, byte, string, etc., or java objects

  • arrayName - an identifier

example: double[] data;

TYPES OF ARRAYS IN JAVA

Single-Dimensional Array

  • most common type of arrays, where elements are stored in a linear order

Multi-Dimensional Array

  • arrays with more than one dimension

ADVANTAGES OF DISADVANTAGE

Advantages

  1. Efficient Access: fast and has constant time complexity

  2. Memory Management: have fixed size, which makes memory management straightforward and predictable

  3. Data Organization: structured manner, making it easier to manage related elements

Disadvantages

  1. Fixed Size: leads to memory waste if the size is overestimated or insufficient if underestimated

  2. Type Homogeneity: can only store elements of the same data type, which may require additional handling for mixed data types

  3. Insertion and Deletion: can be costly as it may require shifting elements