

If you already have six apartment boxes, you're going to have to throw away someone's mail (e.g. Let's say with that 6-apartment box represented above, if you want to add a new apartment between, say, #1 and #2, then to "make room" you have to move the mail from #2 to the third box, #3 to the fourth box, etc.
#Java array vs arraylist reddit series#
The best way I've found to think about it is an array is like a series of mailboxes. It's easy to manipulate, just tell guy 4 who the new guy 5 is or change is information, but you can't just walk up to someone and say "Are you guy 9?" You have to go trough guys 1 trough 8 first. Guy number 2 can tell you who guy number three is, and so forth. You know who is first, so you talk to him. You can easily walk up to them and talk to the people in the row, but you have a harder time changing their order.Ī list is like a lot of people randomly scattered about a room. Imagine an array as a group of people all in a row. You usually can only access list sequencially (begin at the first item and then go down the line) but manipulating them is easier because they aren't all stuck together in memory. Each item in the list points to the item after it (in the most basic implementation). An array is a collection of items where each item has an index, from 0 to (n-1), where n is the number of items in the array.Ī list is a linear sequence of items. It starts at memory address A and ends at memory address A+X. This makes it easy to add or remove items from the middle of the list, but difficult to access items (in order to get to item #4, you start from #0, from there to #1, to #2, etc.)Īn array is simply a block of data. The list can be a linked list - each item in the list holds a reference to the next item on the list (as in /u/EgNotaEkkiReddit's example).

This for example is known as ArrayList in Java or List in C#. The actual data can be stored in an array, so it's easy to access any item in the middle of the list (you can ask for item #4 in the list for exapmle), but hard to add and remove items unless it's from the end of the list.

Unlike an array, a list can have different implementations: You can add items to a list, remove items, sometimes even move items around. Usually fixed size (can't resize it without creating a new array and copying the items to it), with N elements stored sequentially in memory, numbered from 0 to N-1.Ī list is any sequential collection of items. As /u/EgNotaEkkiReddit wrote, an array is simply a block of data.
