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
70
votes
1 answer

1D numpy concatenate: TypeError: only integer scalar arrays can be converted to a scalar index

I want to store numpy array into to another numpy array I am using np.concatenate This is my code x=np.concatenate(x,s_x) These are the type and the shape of x and s_x Type of s_x: , Shape of s_x: (173,) Type of x:
Santhosh
  • 1,554
  • 1
  • 13
  • 19
70
votes
17 answers

Java Array, Finding Duplicates

I have an array, and am looking for duplicates. duplicates = false; for(j = 0; j < zipcodeList.length; j++){ for(k = 0; k < zipcodeList.length; k++){ if (zipcodeList[k] == zipcodeList[j]){ duplicates = true; } …
Snowman
  • 31,411
  • 46
  • 180
  • 303
70
votes
5 answers

How to compare equality of lists of arrays with modern Java?

I have two lists of arrays. How do I easily compare equality of these with Java 8 and its features, without using external libraries? I am looking for a "better" (higher-level, shorter, more efficient) solution than brute-force code like this…
hyde
  • 60,639
  • 21
  • 115
  • 176
70
votes
10 answers

How to create a numpy array of lists?

I want to create a numpy array in which each element must be a list, so later I can append new elements to each. I have looked on google and here on stack overflow already, yet it seems nowhere to be found. Main issue is that numpy assumes your list…
Ricardo Silveira
  • 1,193
  • 1
  • 8
  • 16
70
votes
5 answers

Why is Arrays.fill() not used in HashMap.clear() anymore?

I noticed something strange in the implementation of HashMap.clear(). This is how it looked in OpenJDK 7u40: public void clear() { modCount++; Arrays.fill(table, null); size = 0; } And this is how it looks as of OpenJDK 8u40: public…
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
70
votes
3 answers

Check if value exists in the array (AngularJS)

Currently, I'm using the forEach()-method of angular to check the new value with the array of objects. But that's the wrong approach because, for example, in the list are 20 objects. When I'm creating an object with an existing article, then the…
yuro
  • 2,189
  • 6
  • 40
  • 76
70
votes
2 answers

Java: How initialize an array in Java in one line?

int[] array1 = {1, 2, 3, 4, 5, 6, ,7, 8}; - working array1 = {1, 1, 1, 1, 2, 5, ,7, 8}; - NOT working The first line is working, but second line is not working. How can I make the initialization from the second line in one single line of code?
Peter
  • 703
  • 1
  • 5
  • 4
70
votes
6 answers

C# and arrays of anonymous objects

What does such an expression mean? obj.DataSource = new[] { new {Text = "Silverlight", Count = 10, Link="/Tags/Silverlight" }, new {Text = "IIS 7", Count = 11, Link="http://iis.net" }, new {Text = "IE 8", Count = 12, Link="/Tags/IE8" }, …
Yevhen
  • 1,897
  • 2
  • 14
  • 25
70
votes
5 answers

Change String-Array in Strings.xml to ArrayList

I'm developing an Android app. I need to convert a string array into an ArrayList. I've read up on this, and all have cases where you add the values to the array in the java file. I have declared the string-array in the strings.xml file. My…
hichris123
  • 10,145
  • 15
  • 56
  • 70
70
votes
5 answers

Converting between C++ std::vector and C array without copying

I would like to be able to convert between std::vector and its underlying C array int* without explicitly copying the data. Does std::vector provide access to the underlying C array? I am looking for something like this vector v (4,100) int*…
D R
  • 21,936
  • 38
  • 112
  • 149
70
votes
3 answers

How to make IEnumerable.Contains case-insensitive?

Suppose I have a .net Array of strings. string[] strings = new string[] { "AbC", "123", "Xyz", "321" }; If I wanted to see if the array of strings contains "ABC", I could write strings.Contains("ABC"); However, suppose that I want a function that…
Vivian River
  • 31,198
  • 62
  • 198
  • 313
70
votes
12 answers

Creating two dimensional arrays in Rust

How do I create an empty mutable two dimensional array in Rust? This is what I have tried so far: let mut state[[u8 * 4] * 4]; This produces the error error: expected one of `:`, `;`, `=`, or `@`, found `[` --> src/main.rs:2:18 | 2 | let mut…
php--
  • 2,647
  • 4
  • 16
  • 18
70
votes
13 answers

jQuery equivalent to Prototype array.last()

Prototype: var array = [1,2,3,4]; var lastEl = array.last(); Anything similar to this in jQuery?
Paul
70
votes
14 answers

Multi-dimensional arrays in Bash

I am planning a script to manage some pieces of my Linux systems and am at the point of deciding if I want to use bash or python. I would prefer to do this as a Bash script simply because the commands are easier, but the real deciding factor is…
scphantm
  • 4,293
  • 8
  • 43
  • 77
70
votes
2 answers

PHP - use array as class constant

Possible Duplicate: Is it possible to declare an array as constant Is it possible to use an array as a class constant in PHP? I.e const MYARRAY = array('123', '234'); If not why?
Martin
  • 815
  • 1
  • 7
  • 6
1 2 3
99
100