DEV Community

Cover image for Java Quickies. Arrays, Lists and ArrayLists
Tristan Elliott
Tristan Elliott

Posted on • Updated on

Java Quickies. Arrays, Lists and ArrayLists

Introduction

  • This series is going to be dedicated to the basic understanding of Java. When ever I find myself asking, "How does this work ?". I will create a blog post and put it here. This series will not be in order so feel free to read what ever post you find most relevant.

Lists

  • In theory(computer science) a list is just an ordered collection of elements(also called a sequence). In Java a list is an interface. I stated in theory and in Java because it is important to know what type of list you are talking about. When googling make sure you are certain about whether you are reading list theory(computer science) or list implementation(Java).

  • The user of this interface has precise control over where in the list each event is inserted. It allows us to access elements by their integer index and search for elements in the list. A list like an array is 0 based, meaning they both start at 0

  • When an object implements this interface it gives that object access to specific methods that allow for great control over the individual elements that are inside the list. Since list is an interface we will not see any instantiations of it like you do with arrays(new Array()). Instead we will see objects that implement the list interface, ArrayList and LinkedList are good examples of objects that have implemented the list interface.

Arrays

  • An array is just a container object that holds a fixed number of elements where each element holds a specified index. The length of an array is established when the array is created. After creation its length is fixed, we can not add or removed elements from the array.
int[] anArray
Enter fullscreen mode Exit fullscreen mode
  • An array declaration, like the one above, has two components to it. The first being the array's type, for us its int. The second is the name of the array and for us its anArray. The declaration does not create the array it simply tells the compiler that this variable will hold an array of the specified type. The array will be created with the new operator.
int[] anArray = new int[10]
Enter fullscreen mode Exit fullscreen mode

-if you are unfamiliar with the new operator, there are 3 main things that happen when the new operator is invoked.

  • 1) Declaration : associates a variable with an object type. It tells the compile what type we are going to assign to the variable.

  • 2) instantiation : the new operator instantiates the class by allocating memory for a new object and returning a reference to that variable.

  • 3) Initialization : the constructor on the object is called, which creates the new object.

  • So our call to int[] anArray = new int[10] will allocate an array with enough memory for 10 integer elements.

  • Each element in the Array is accessed by numerical index,anArray[0].We can also add integer elements to anArray through this syntax anArray[0] = 33 this will assign the integer 33 to anArray at the index of 0

  • Alternatively we can use a shortcut to create an Array with 10 predefined values.

int[] anArray = {1,2,3,4,5,6,7,8,9,10}
Enter fullscreen mode Exit fullscreen mode
  • This automatically creates an array for us with predefined numbers.

ArrayList

  • ArrayList is a resizable-array implementation of the list interface. It implements all of the list methods and has additional methods to manipulate the size of the array that is used internally to store the list.

  • As elements are added to an ArrayList, its capacity grows automatically. The capacity is always a minimum of the size of the list you provide.

new ArrayList<>(10)
Enter fullscreen mode Exit fullscreen mode
  • This means that the underlying array has a minimum capacity of at least 10. The underlying mechanics of how the list grows and shrinks are considered a specification detail, so it is not listed in the documentation.

Array vs List

  • I am sure that I will not list out all of the differences but I will list out the major ones.

  • 1) : In Java a list is an interface and an Array is a class.

  • 2) : A List is able to expand and shrink where an Array is not. This is a big advantage when it comes to memory storage. However, only a LinkedList can shrink and expand like a true list. An ArrayList can not, because internally it uses an array for storage.

Conclusion

  • Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
  • Also make sure to checkout my YouTube channel for more programming tutorials.

Top comments (0)