Intern
What is an array and what kind of information can it store?
sobes.tech AI
Answer from AI
An array is a data structure that represents an ordered sequence of elements of the same type, stored contiguously in computer memory.
The information that an array can store depends on the data type of its elements:
- Primitive types: Integers (int, short, long), floating-point numbers (float, double), characters (char), boolean values (boolean).
- Objects: Instances of classes (created by the user or standard libraries), such as strings (String in Java), wrapper objects for primitive types (Integer, Double).
An array can be one-dimensional, two-dimensional, or multi-dimensional.
// One-dimensional array of integers
int[] numbers = {1, 2, 3, 4, 5};
// One-dimensional array of strings
String[] names = new String[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
// Two-dimensional array of integers
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
Key characteristics of an array:
- Fixed size: The size of the array is determined at creation and cannot be changed.
- Indexing: Access to elements is done via a numerical index (starting from 0).
- Homogeneity: All elements of the array must be of the same data type (or a type compatible through inheritance in object-oriented languages).