Questions tagged [arrays]

An array is an ordered linear data structure consisting of a collection of elements (values, variables, or references), each identified by one or more indexes. When asking about specific variants of arrays, use these related tags instead: [vector], [arraylist], [matrix]. When using this tag, in a question that is specific to a programming language, tag the question with the programming language being used.

An array is an ordered linear data structure consisting of a collection of elements (values or variables), each identified by at least one index, stored in contiguous memory locations.

An array is typically stored so that the position of each element can be computed from its index tuple by a mathematical formula.

In some languages (C, Java, etc.) the length of the array must be set beforehand. In other languages (Ruby, Python, LISP, Haxe, etc.) the length of the array grows dynamically as elements are added.

When tagging a question with this tag, also tag the question with the programming language being used.

Array in specific languages

  • C# arrays are actually objects and not just addressable regions of contiguous memory as in C and C++. Array is the abstract base type of all array types. You can use the properties and other class members of the Array base type.
  • C arrays act to store related data under a single variable name with an index, also known as a subscript. They are stored in row-major order, which means the last subscript varies fastest. It is easiest to think of an array as simply a list or ordered grouping for variables of the same type. As such, arrays often help a programmer organize collections of data efficiently and intuitively.
  • C++ inherits raw arrays from C and adds its own array-class std::array for compile-time array sizes, std::vector for runtime dynamic sized arrays. It also has smart pointer implementations like std::unique_ptr, std::shared_ptr.
  • Objective C inherits raw arrays from C and adds its own array-class NSArray and NSMutableArray for dynamic arrays.
  • Ruby's normal array class is called array.
  • In Python, the normal array datatype is called a list, while the array type is used for homogeneous arrays.
  • In NumPy there is a powerful N-dimensional array with many capabilities.
  • PHP arrays are implemented as ordered maps which may contain a mix of numeric or string keys.
  • JavaScript arrays are just objects with a different prototype (with additional functions more useful for array-like structures), with numerical index values stored as strings (all JavaScript keys are strings). Unlike other objects, you cannot use dot notation to access keys - only square bracket notation.
  • In Haxe, an Array has one type parameter which corresponds to the type of collection of elements. Arrays can be created using their constructor new Array() or [1, 2, 3], but also using Array comprehension: [for (i in 0...10) if (i % 2 == 0) i]. For storage of fixed size the abstract type haxe.ds.Vector can be used, which may be faster than Array on some targets and is never slower.
  • In Scala, the normal array class is called Array. To get an element from an array you use parentheses (most languages use square brackets).
  • In Java, an array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
  • In Perl, array variables are denoted with the @ prefix and arrays are declared with parentheses. Replacing the prefix with $# returns the last index.
  • In Rust, arrays are groups of data of the same type that are contiguous in memory, so they can be used when communicating with C. The length of an array is fixed.
  • In Swift, an array that includes the specified values, automatically inferring the array’s Element type. Swift makes it easy to create arrays in your code using an array literal: simply surround a comma-separated list of values with square brackets.
  • In Pascal, arrays declarations specify the index range rather than the number of elements; so parray: array [1..25] of real; declares a one-based array of 25 real numbers (valid elements are parray[1] thru parray[25]), whereas parray: array [0..24] of real; declares a zero-based ('C-style') array of the same size (the first element is parray[0]). Pascal array index ranges can include negative numbers!

Characteristics

Elements of an array are usually specified with a 0-first index, for example, myarray[0] would represent the first element of myarray. myarray[n] (where n is the length of the array minus 1) would represent the last element in the array. However, some languages such as old Fortran and Lua use 1 as the initial index.

Some languages (C++, Java, C#) have "basic arrays" and collections. Basic arrays are supported by the compiler directly, have a fixed size, and only provide element access by index. Collections, like Java's ArrayList, are system library classes implemented on the top of these basic arrays and have a wide range of various methods. In such cases, the tag should be used to name the simple arrays.

Arrays can be statically allocated or dynamically allocated. The way in which you access the array and its type varies based on how it is declared and allocated.

Arrays can contain multiple indices. For example, an array with one index (e.g. array[0]) is known as a one-dimensional array. If it has two indices (e.g. array[0][0]) it is considered two dimensional and maybe visualized as a grid. Multidimensional arrays, or, in other words, arrays with multiple indices are called matrices, or a matrix is singular.

References

Related tags

Where talking about specific variants of arrays, use these related tags instead:

414766 questions
75
votes
4 answers

Array as session variable

Is it possible to make an array a session variable in PHP? The situation is that I have a table (page 1) with some cells having a link to a particular page. The next page will have a list of names (page 2, which I want to keep in a session array)…
anurag-jain
  • 1,380
  • 2
  • 11
  • 31
75
votes
5 answers

Array vs ArrayList in performance

Which one is better in performance between Array of type Object and ArrayList of type Object? Assume we have a Array of Animal objects : Animal animal[] and a arraylist : ArrayList list Now I am doing animal[10] and …
Spark-Beginner
  • 1,334
  • 5
  • 17
  • 24
75
votes
9 answers

Create two-dimensional arrays and access sub-arrays in Ruby

I wonder if there's a possibility to create a two dimensional array and to quickly access any horizontal or vertical sub array in it? I believe we can access a horizontal sub array in the following case: x = Array.new(10) { Array.new(20)…
oldhomemovie
  • 14,621
  • 13
  • 64
  • 99
75
votes
13 answers

Finding matches between multiple JavaScript Arrays

I have multiple arrays with string values and I want to compare them and only keep the matching results that are identical between ALL of them. Given this example code: var arr1 = ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco',…
Chris Barr
  • 29,851
  • 23
  • 95
  • 135
75
votes
4 answers

Get JSONArray without array name?

I'm new to JSON and trying this tutorial: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/#comments I'm new to JSON, C languages, Java and also Android, but am learning. The tutorial uses what I'm calling a…
Ricky
  • 840
  • 1
  • 8
  • 11
74
votes
5 answers

Iterate over the first n elements of an array

How can I iterate up to four objects of an array and not all? In the following code, it iterates over all objects. I need only the first four objects. objects = Products.all(); arr=Array.new objects.each do |obj| arr << obj end p arr Can it be…
Ben
  • 25,389
  • 34
  • 109
  • 165
74
votes
8 answers

How can I check if an array contains a specific value in php?

I have a PHP variable of type Array and I would like find out if it contains a specific value and let the user know that it is there. This is my array: Array ( [0] => kitchen [1] => bedroom [2] => living_room [3] => dining_room) and I would like…
hairynuggets
  • 3,191
  • 22
  • 55
  • 90
74
votes
9 answers

Pass array to MySQL stored routine

I need to pass an array of strings as parameter to a MySQL stored routine. The array could be long and its number of elements is not fixed. I then want to put the string values into an in-memory table with one column, so I can work with the data. I…
Gruber
  • 4,478
  • 6
  • 47
  • 74
74
votes
5 answers

How does "sizeof" determine the size of an array?

How does C find the size of an array at runtime? Where is the information about the array size or bounds stored ?
Kazoom
  • 5,659
  • 16
  • 56
  • 69
74
votes
6 answers

Have I reached the limits of the size of objects JavaScript in my browser can handle?

I'm embedding a large array in In this particular example, the array has 210,000 elements. That's well below the…
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
74
votes
3 answers

Difference between Array.length = 0 and Array =[]?

Can some one explain the conceptual difference between both of them. Read somewhere that the second one creates a new array by destroying all references to the existing array and the .length=0 just empties the array. But it didn't work in my…
Srikanth Rayabhagi
  • 1,393
  • 3
  • 12
  • 23
74
votes
3 answers

What is the max key size for an array in PHP?

I am generating associative arrays and the key value is a string concat of 1..n columns. Is there a max length for keys that will come back to bite me? If so, I'll probably stop and do it differently.
Ross
  • 743
  • 1
  • 5
  • 4
74
votes
1 answer

Given an array of arguments, how do I send those arguments to a particular function in Ruby?

Forgive the beginner question, but say I have an array: a = [1,2,3] And a function somewhere; let's say it's an instance function: class Ilike def turtles(*args) puts args.inspect end end How do I invoke Ilike.turtles with a as if I were…
Steven
  • 17,796
  • 13
  • 66
  • 118
74
votes
3 answers

Why does JSON.parse(['1234']) return 1234?

I am having problems understanding the behavior of JSON.parse. JSON.parse should work for only strings. But it seems to work for an array which contains only one string (even single quoted), if the string contains only numbers. JSON.parse(['1234'])…
Akshendra Pratap
  • 2,002
  • 1
  • 12
  • 25
74
votes
8 answers

How can I push an object into an array?

I know it's simple, but I don't get it. I have this code: // My object const nieto = { label: "Title", value: "Ramones" } let nietos = []; nietos.push(nieto.label); nietos.push(nieto.value); If I do this I'll get a simple array: ["Title",…
pmiranda
  • 7,602
  • 14
  • 72
  • 155