image
Working with ArrayList - Part 1
Start course
Difficulty
Beginner
Duration
2h 16m
Students
134
Ratings
5/5
Description

In this course, we'll learn the Collection framework and the Map Interface.

Learning Objectives

  • Collection Framework vs Array
  • Hierarchy of Collection Framework
  • List interface - ArrayList Class
  • Iterator Interface
  • Set Interface
  • Queue and Dequeue Interfaces
  • Map Interface

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In the previous video, we talked about the list and ArrayList and the declaration of the ArrayList. In this video, we will use the methods of the ArrayList class. We will make some examples with the ArrayList. First, let's look at these methods. The add() method is used to add elements to the list. There is an add() method with two different parameters. The first takes only the element name as a parameter, while the other takes the index number and element name together. If we do not specify an index number, it adds it to the end of the list. If we specify an index number, it adds an element to the index number we specified. And, if there is an element in the index, it shifts it to the right. The remove() method is used to delete elements from the list. This method has two different uses. First, you give the index number and that deletes the element in the index, or you give the name of the element you want to delete and it deletes that element. The set() method is used to change an element in the list. It takes two parameters:

The first is the index number of the element to be changed. The second is the new value of the element. The size() method returns the size of the list. The isEmpty() method checks if the list is empty. Returns True if it is empty, False otherwise. The clear() method deletes all the elements in the list at once. The contains() method checks if an element is in that array. Returns True if it is, and False otherwise. The equals() method compares the elements of the two arrays in order. Returns True if the elements are the same, False otherwise. Okay. Now, let's get some practice with these methods. If you're ready, let's start. First, I'll create a new class. I right click on the 'collectionsexample' package and select 'New Class' option. The class name can be ArrayListMethods, and I will check the checkbox and click the 'Finish' button. Okay. Let's create an ArrayList. ArrayList<>. The type of this list will be object and the name can be myList1 = new ArrayList<>(). Okay. Since the type of this list is object, we can store int, string, or double etc.

So, to add an element to the ArrayList, we use the add() method. So, I write, myList1.add(); Inside the parentheses I write the name of the element which I want to add to this list. So, the first element can be int 5. Now, let's add another element. myList1.add(); This can be string "Java". Now, let's add another element. myList1.add(); This element can be double. So, I write 2.8. Okay. Now, let's print the elements of this ArrayList. System.out.println(myList1); If you remember, while we work with the array, if we want to print the elements of the array, we used the Array.toString() method. If we wrote the name of the array, we only see the ID of the array not the elements, remember. But the ArrayList class has its own toString() method. So, if we write the name of the ArrayList, it prints the elements on the console. Now, let's run the code and see. As you can see, all elements are printed on the console. And if you notice, since we do not specify any index number while adding the elements, the elements are added to the list in order.

Now, let's create another ArrayList. List<string> list2 = new ArrayList<>(); So, the type of this list is string. Let's add some elements to this list. list2.add("Ferrari"); For the second element, list2.add(5); As you can see, this time we get a compilation error because the type of this array is string and it can only hold string objects. So, let the second element be "Mercedes". And, let's add another element. list2.add(); But this time, I will use the index number while adding the element. Currently, this array has two elements. So, I want to add it as the third element. The index number of the third element is 2. So, the first parameter will be 2 and the second parameter is the element which I want to add. So, I write "BMW". Let's add another element with an index number. list2.add(); This time, the index number will be 1 and the element can be "Ford". There is the Mercedes element in index 1. So, if we add another element to index 1, in this case, it adds the element we want to add to index number 1 and shifts the index number of the old elements one digit to the right.

So, the list would be like this: Ferrari, Ford, Mercedes, BMW. Now, let's print this list on the console. Let's run and see. As you can see, the first element is Ferrari, the second Ford, the third Mercedes, and the last element is BMW. So, the add() method with index number is similar to the insert() method of the string. Remember. Okay. So, we learned the add() method of the ArrayList. Now, let's look at the delete an element from the ArrayList. For the delete an element, we use the remove() method of the ArrayList class. Let's remove an element of the list2 Arraylist. list2.remove(); As you can see, there are two remove() methods. The first one takes the object or element name, and the other method takes the index number of the element which we want to delete. First, I will use the index number. So, I'll delete the element in index 2. So, I write 2 in parentheses. In this case, it will delete the Mercedes because the index number of the Mercedes is 2. Let's print it on the console. System.out.pritnln(list2); So, let's run the code and see.

As you can see, the Mercedes is removed from the list and the element after the Mercedes is shifted to the left. So, the new element in index 2 is BMW. Okay. Now, let's remove another element using the element name. list2.remove(); This time, I will remove the Ferrari. So, in parentheses, I write "Ferrari". This time, the first element will be Ford and the second element will be BMW. Let's run and see. As you can see, the Ferrari is removed from the list and the new list with two elements is on the console. Okay. So, the remove() method of the ArrayList is like this. Also, we can change any element of the ArrayList with the set() method of the ArrayList class. So, let's change the BMW with the Opel. I write, list2.set(); The set() method takes two parameters. The first one is the index number of the element we want to change, and the second parameter is the new value of that element. The index number of the BMW is 1 and the new value is Opel. Also, you can use the .toString() method here. The result will not change.

The toString() method calls automatically in the ArrayList. Okay. Let's run and see. As you can see, the new element is Opel, not BMW. Okay. Now, I will show you another thing. You cannot initialize an Arraylist like the array. Let's do an example. ArrayList<String> newList =. After the equal sign, I'll open the curly braces and try to add some elements. But notice, we get a compilation error because we cannot initialize the ArrayList like the array. This is important. Okay. So, let's take a short break here. We will continue in the next video. See you in the next video.

 

About the Author
Students
3969
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics