DEV Community

Fabian Fabro
Fabian Fabro

Posted on

List vs Array Arrays vs. Lists vs. ArrayLists

From experiencing a coding bootcamp, I mostly worked with Ruby & Javascript, later finding out that working with arrays in these two languages are not really considered more 'traditional' arrays. I started picking up C# and noticed that arrays are built differently than Ruby & Javascript. The 'arrays' that we worked with in those two languages are viewed more as a 'List', or even more-so as an 'ArrayList.' Let's explore the differences of these three and see which one would be appropriate to use depending on circumstances.


'In Ruby'

empty_array = []
num_array = [2, 6, 25, 100]
random_array = ['words', 'house', 10, true, 7, nil]

num_array.push(7, 5, 'something') 
#num_array << 7 << 5 << 'something' #works the same way
#num_array output: [2, 6, 25, 100, 7, 5, 'something']
Enter fullscreen mode Exit fullscreen mode

'In Javascript'

const emptyArray = []
const numArray = [2, 6, 25, 100]
const randomArray = ['words', 'house', 10, true, 7, null]

numArray.push(7, 5, 'something')
//numArray output: [2, 6, 25, 100, 7, 5, 'something']

Enter fullscreen mode Exit fullscreen mode

'In C#'

int[] emptyNumArray = new int[0];
int[] numArray = new int[] {2, 6, 25, 100};
//int[] numArray = {2, 6, 25, 100}; //Alternate to above
string[] stringArray = {'words', 'something', 'stuff'};

string[] randomArray = {'words', 'house', 10, true, 7, null};
//This returns an error

//No method for pushing, or appending to an array
Enter fullscreen mode Exit fullscreen mode

Now we can see Ruby and Javascript are pretty similar and flexible with how they work their arrays. You can create empty arrays, push new objects into the array, more specifically push pretty much anything to an array. C# seems to be very picky about how the array is constructed. Let's look through each line and see what's going on in C#.

int[] emptyNumArray = new int[0];
Enter fullscreen mode Exit fullscreen mode

This is actually a pretty useless array because sure we are creating an empty array, but we are giving it 0 length of slots. If we changed this to at least a length:

int[] emptyNumArray = new int[5];

for(int i=0; i < emptyNumArray.Length; i++){
   emptyNumArray[i] = i;
//Then we have to append integers to each slot of the array.
Enter fullscreen mode Exit fullscreen mode

This shows that arrays are locked in this sense, not giving this flexibility of being appending to a container. Also notice that there is this 'int[].' This also locks the array to only allow integer values in this array, that's why we saw earlier with the 'string[] randomArray' in C# that it gave an error.

Although, one thing I did find for Ruby and Javascript is that you can instantiate an array with a fixed size.


array = #array with a length of 5
Enter fullscreen mode Exit fullscreen mode


const newArr = Array(5).fill(null).map((x, i) => i);
Enter fullscreen mode Exit fullscreen mode

So, now that we've seen some differences in languages of using arrays, let's understand what an Array really is.

"An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type" - From Tutorialspoint

Wait, then how do we describe Ruby and Javascript's 'arrays?' We can describe them more as a Dynamic array because it is growable, resizable, and mutable.

So the opposite of Dynamic would a Static array. In this link, Henry Feild defines static arrays as 'arrays that require that we know the size at compile time.' We can think of this as that we have to define the array its size and that it is immutable, mainly with locked values in place.

This is all due to the heap and stack memory allocation. That is a whole lot to be discussed for this blog so I will provide links for each language of how they deal with memory allocation:

How Ruby Uses Memory
JavaScript stack size
C# Heap(ing) Vs Stack(ing) In .NET - Part One

But lets move on, so how do we solve this problem with appending in C#? Introducing lists!


"A list is an object which holds variables in a specific order." - Source

In retrospect, this is the dynamic array like in Ruby and Javascript that we can use for C#.

//You'll have to import a method library to use the List syntax
using System.Collections.Generic;

List<int> listOfNumbers = new List<int>();


//listOfNumbers output: {2, 6, 25, 100};

//You can also instantiate from the beginning
List<int> listOfNumbers2 = new List<int>{2, 6, 25, 100};
Enter fullscreen mode Exit fullscreen mode

In C#, lists are commonly used more than arrays, but there are some situations when arrays are necessary, like if your data is not supposed to grow drastically.

Here is a link to seeing the differences between arrays and lists in C#.

C# Array vs List

So onto the last part, ArrayLists!


"An ArrayList is a non-generic type of collection in C#. It can contain elements of any data types. It is similar to an array, except that it grows automatically as you add items in it. Unlike an array, you don't need to specify the size of ArrayList." - Source

//Import syntax library to use ArrayList
using System.Collections;

ArrayList newList = new ArrayList();


//newList output: {"A", 4, false};

Enter fullscreen mode Exit fullscreen mode

So it looks like Arraylist is more flexible than List because of the data type constraint. It is unusual that Lists are considered the 'generic' collection while Arraylists are the 'non-generic' collection. Arraylists mainly create a collection of "Object" Type, converting the other type variables into an object, as opposed to Lists that stick with a certain data type when instantiating a list.

And that is all I got about Arrays, Lists, and Arraylists. There is many resources that discuss this topic about differences these data structures because of how different languages utilize their data structures, as we compared the differences with Ruby, Javascript, and C#. I hope this was a useful blog of a mini introduction and seeing these data structures in different perspectives.

Top comments (1)

mariorayas profile image

What about multidimentional arrays? Is it posibble to replace them with List?