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
71
votes
8 answers

Why can arrays not be trimmed?

On the MSDN Documentation site it says the following about the Array.Resize method: If newSize is greater than the Length of the old array, a new array is allocated and all the elements are copied from the old array to the new one. If newSize is…
Kjara
  • 2,504
  • 15
  • 42
71
votes
11 answers

Read a text file line by line in Swift?

I just started learning Swift. I have got my code to read from the text file, and the App displays the content of the entire text file. How can I display line by line and call upon that line multiple times? TextFile.txt contains the following: 1.…
ScarletEnvy
  • 871
  • 1
  • 7
  • 14
71
votes
7 answers

Compare arrays in swift

Trying to understand how swift compares arrays. var myArray1 : [String] = ["1","2","3","4","5"] var myArray2 : [String] = ["1","2","3","4","5"] // 1) Comparing 2 simple arrays if(myArray1 == myArray2) { println("Equality") } else { …
vivien.destpern
  • 1,020
  • 1
  • 7
  • 14
71
votes
9 answers

Fastest way to move first element to the end of an Array

I'm wondering what is the fastest way in JavaScript to move an element from the beginning of an Array to the end. For example if we have [8,1,2,3,4,5,6,7] And we want: [1,2,3,4,5,6,7,8] I want to move the first element to the end. I was thinking…
Bosiwow
  • 2,025
  • 3
  • 28
  • 46
71
votes
6 answers

filter an array in C#

i have an array of objects (Car[] for example) and there is an IsAvailable Property on the object i want to use the full array (where IsAvailable is true for some items and false for some others) as the input and return a new array which includes…
leora
  • 188,729
  • 360
  • 878
  • 1,366
71
votes
4 answers

Get file's size from bytes array (without saving to disc)

I have a byte's array and I want to calculate what would be the file size if I'll write these bytes to file. Is it possible without writing the file to disc?
nKognito
  • 6,297
  • 17
  • 77
  • 138
71
votes
8 answers

Why does my sorting loop seem to append an element where it shouldn't?

I am trying to sort an array of Strings using compareTo(). This is my code: static String Array[] = {" Hello ", " This ", "is ", "Sorting ", "Example"}; String temp; public static void main(String[] args) { for (int j=0; j
Sikander
  • 2,799
  • 12
  • 48
  • 100
71
votes
5 answers

What causes: "Notice: Uninitialized string offset" to appear?

I have a form that users fill out, and on the form there are multiple identical fields, like "project name", "project date", "catagory", etc. Based on how many forms a user is submitting, my goal is to: loop over the number of forms create…
Tomasz Iniewicz
  • 4,379
  • 6
  • 42
  • 47
71
votes
11 answers

Calculating average of an array list?

I'm trying to use the below code to calculate the average of a set of values that a user enters and display it in a jTextArea but it does not work properly. Say, a user enters 7, 4, and 5, the program displays 1 as the average when it should display…
user1419306
  • 779
  • 2
  • 6
  • 10
71
votes
9 answers

Check if all values in array are the same

I need to check if all values in an array equal the same thing. For example: $allValues = array( 'true', 'true', 'true', ); If every value in the array equals 'true' then I want to echo 'all true'. If any value in the array equals…
Nick
  • 1,969
  • 3
  • 21
  • 24
70
votes
6 answers

Removing elements from array Ruby

Let's say I am trying to remove elements from array a = [1,1,1,2,2,3]. If I perform the following: b = a - [1,3] Then I will get: b = [2,2] However, I want the result to be b = [1,1,2,2] i.e. I only remove one instance of each element in the…
Michael
  • 7,087
  • 21
  • 52
  • 81
70
votes
5 answers

Getting the "diff" between two arrays in C#?

Let's say I have these two arrays: var array1 = new[] {"A", "B", "C"}; var array2 = new[] {"A", "C", "D"}; I would like to get the differences between the two. I know I could write this in just a few lines of code, but I want to make sure I'm not…
Michael
  • 1,919
  • 2
  • 16
  • 26
70
votes
7 answers

Are 'new' and 'delete' getting deprecated in C++?

I stumbled upon a quiz that involved array declaration with different sizes. The first thing that came to my mind is that I would need to use dynamic allocation with the new command, like this: while(T--) { int N; cin >> N; int *array = new…
70
votes
14 answers

In Java, remove empty elements from a list of Strings

In Java, I have an ArrayList of Strings like: [,Hi, ,How,are,you] I want to remove the null and empty elements, how to change it so it is like this: [Hi,How,are,you]
Niranjan Kumar
  • 819
  • 1
  • 10
  • 15
70
votes
8 answers

Array.includes() to find object in array

I'm attempting to find an object in an array using Array.prototype.includes. Is this possible? I realize there is a difference between shallow and deep comparison. Is that the reason the below code returns false? I could not find a relevant answer…
timothym
  • 2,853
  • 5
  • 19
  • 26