An array is a data structure that holds a sequence of elements, usually of the same type, in contiguous memory. Each element is reached by its numeric index, and most languages start counting at 0.
Why it matters
Because elements are laid out side by side, the computer can calculate any element's address directly, making reads and writes by index O(1). Sequential access is also very cache friendly, which makes arrays fast in practice. The drawbacks are that inserting or deleting in the middle requires shifting later elements (O(n)), and fixed-size arrays cannot grow. Many languages provide dynamic arrays, such as Python lists, Java ArrayList and C++ vector, that resize automatically by allocating a larger block and copying.
Example
const codes = [200, 301, 404, 500];
codes[2]; // 404 (index 2, the third element)
codes.length; // 4
codes.push(503); // append at the end
Common mistakes
- Off-by-one errors, such as looping to
lengthinclusive instead oflength - 1. - Reading outside the bounds, which is an error in safe languages and undefined behavior in C.
- Using
includesorindexOfrepeatedly on large arrays where a set or hash table would be faster.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.