DEV Community

Nick Shattuck
Nick Shattuck

Posted on

Java ArrayLists: A Dead Easy Tutorial For Absolute Beginnners

Greetings Dev community! I thought I would finally try my hand at writing a series of tutorials in Java. But wait why Java? Isn't it like from the 90's? The reason I choose Java is because it's what I am learning in school, and I am willing to conjecture that many other students and beginners have at least had a few classes or tutorials using Java. Personally, I find Java to be really fun to develop with. Yes, it's old, but it's aged like a fine wine. I'm also a product of the 90's; a retro-millennial if you will. Java is still very current, and you can build a multitudinous repertoire of projects with it ranging from simple CRUD applications, Android apps and then some.
Additionally, writing is also a great way to solidify your understanding of a topic and I thought it would help me keep my Java fresh. Mind you, I am still a student, so if I make any grievous errors, kindly point it out to me so I can make any necessary edits. My aim is to provide other beginners in the Java/programming world a series of easy tutorials on one of the most common data structures used in Java: Array Lists. In this tutorial, I will briefly talk about what ArrayLists are, and demonstrate 3 basic CRUD operations with provided examples.

What is an ArrayList?

I'm assuming that you've had some introduction into classes, polymorphism, and interfaces, but since I am a beginner myself let's take a brief moment to look under the hood. Officially an ArrayList class is an array-based implementation of the List interface. This rather cryptic definition basically means that an ArrayList is a class that defines the empty methods in the List interface. Thankfully, the Java gods were kind enough to define these methods for us mere mortals, so ArrayLists aren't as complicated as they initially seem. Let's start with the basics. To declare an ArrayList, simply do the following:

public static void main (String [] args) {

 ArrayList<String> list = new ArrayList<>();

}

We have a few things happening here. Firstly, we've declared an ArrayList object called list. But Nick! I hear you cry. What in the world is ? ArrayLists actually store generics.

    ArrayList<T> list = new ArrayList<> (); 

T is whatever object you wish to store in it. In my example, I decided to store Strings, but you can put any type of object inside of list that you like. For example, you could delcare

public static void main (String [] args) {

 ArrayList<Integer> list = new ArrayList<>();

}

or...

public static void main (String [] args) {

ArrayList<Double> list = new ArrayList<> ();

}

The list goes on (pardon the pun). For now, I will focus on using Strings, but I encourage you to play around with other object types.

Adding to an ArrayList

The ArrayList class comes with some predefined methods for CRUD(CREATE, READ, UPDATE, DELETE) implementation features. Adding elements to an ArrayList is pretty straight forward. Allow me to introduce to you the add(E e) method, where E is the element we want to add.

public static void main (String [] args) {

 ArrayList<Integer> list = new ArrayList<>();
list.add("Java"); 

System.out.println(list); 

}

When I compile this, I get the following output:

[Java]

What I've done here is added a String object with the value of "Java" into the ArrayList. Printing it, notice that it looks like an array!
We can add as many elements in the ArrayList as we want (well, as much as your computer's memory can handle!), so we can think of the size of the
ArrayList as size n. Let's add a few more values into the list.

  list.add("Python");
  list.add("Kotlin");
  list.add("JavaScript");
  list.add("Ruby");

Now when I print this, I get the following output:

public static void main (String [] args) {

 ArrayList<Integer> list = new ArrayList<>();
list.add("Java"); 
list.add("Python");
list.add("Kotlin");
list.add("JavaScript");
list.add("Ruby");

System.out.println(list);
}

Now when I print this, I get the following output:

  [Java, Python, Kotlin, JavaScript, Ruby] 

Updating the list

Sometimes we might want to update something inside of our ArrayList. Updating ArrayLists are straight forward. Let's say now that we've added a few elements to list that we want to make a few updates. Updating a list is handled using the set(int index, E element) method. Let's say I want to modify JavaScript to display "React".

public static void main (String [] args) {

 ArrayList<Integer> list = new ArrayList<>();

list.add("Java"); 
list.add("Python");
list.add("Kotlin");
list.add("JavaScript");
list.add("Ruby");

list.set(3, "React");

System.out.println(list);

}

Now when I print this, I should see the desired edit.

[Java, Python, Kotlin, React, Ruby]

Be careful! Elements in an ArrayList are off by a value of one. This means that Java is at index 0, not 1! If you don't believe me, I'll prove it:

 list.set(1, "Oops!"); 

//Output 
 [Java, Oops!, Kotlin, React, Ruby]


Python is at index 1, but we might have intended to change Java to "oops!". This is a common mistake amongst newbies (myself included), so just keep that in mind.

Deleting from ArrayLists

Finally, I will introduce you to the remove() method. With remove(), you can remove the index of the elements, or Objects itself. But they both accomplish the same effect!

public static void main (String [] args) {

 ArrayList<Integer> list = new ArrayList<>();

list.add("Java"); 
list.add("Python");
list.add("Kotlin");
list.add("JavaScript");
list.add("Ruby");

list.set(3, "React");

//Remove index zero
list.remove(0);

System.out.println(list);

//Output
 [Python, Kotlin, React, Ruby]

}

Here I am removing the index 0, which contains the String "Java". Java has been removed. Similarly, we could also do the following:

public static void main (String [] args) {

 ArrayList<Integer> list = new ArrayList<>();

list.add("Java"); 
list.add("Python");
list.add("Kotlin");
list.add("JavaScript");
list.add("Ruby");

list.set(3, "React");

//Remove index zero
list.remove("Java");

System.out.println(list);

//Output
 [Python, Kotlin, React, Ruby]

}

Notice that this produces the same result. The only caveat is that this way of removing from the list returns a boolean, whereas removing the index returns E or the element at the specified index. If the object is present in the list, then it will be removed. If I try to remove an object not present in the list

 System.out.println(list.remove("HTML")); //returns false

This is because HTML is obviously not in the list. For more information, checkout the official documentation for more methods and functionalities of ArrayLists. I hope that this has been informative and has provided an easy transition into the more advanced features of the Java language. In future tutorials, I'd like to play around with creating some custom objects to demonstrate some of the more flexible features of ArrayLists. Please leave any
questions or comments below, and I will be happy to answer them!

Top comments (4)

Collapse
 
andevr profile image
drew

Hey, really good read. I'm learning Java also. Going through Tim Buchalka's Udemy course. Have you done any android development yet?

Collapse
 
tacomanick profile image
Nick Shattuck

Most of the android development I've done are through Google's courses. You can check them out on the official Android Developer documentation. There are courses for both Java and Kotlin

Collapse
 
andevr profile image
drew

Nice, I will check them out. I wasn't aware there were courses available there. How long have you been studying Java?

Thread Thread
 
tacomanick profile image
Nick Shattuck

I've been seriously studying Java for avout 6 months now, but I've taken classes off and on for about 2 years.