Conversions Between Lists and Arrays

Start course
Overview
Difficulty
Beginner
Duration
2h 16m
Students
26
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 this video, we will discuss the conversion between  Array and  ArrayList. The first one is the conversion from a List to an  Array. To do this, we use the  toArray() method. There are two types of  toArray() method. One is without parameter and the other is with parameter. It takes an  Array as a parameter. For example, myList represents our ArrayList and the  myArray represents our Array. So, to convert the  ArrayList to  Array, we just use the  myList.toArray() method or you can create a new Array, and you can pass this  Array as a parameter to the  toArray() method. Okay, the other conversion is from an  Array to a List. To do this,  we use the asList() method of the  Array class or the addAll() method of the Collections class. The asList() method takes an  Array as a parameter, but the addAll() method takes two parameters. The first one is the name of the list and the other is the name of the  Array. Okay, if you're ready, let's get some practice.

First, I will create a new class. So, I right click on the 'collectionsexample' package and select the new class options. The class name can be ConversionsLists  Array, and check the check box for the main method and click the 'Finish' button. First, I will convert the  ArrayList to an  Array. Okay, let's create an  ArrayList. List < string > list1 = new ArrayList<>(). Now, let's add some elements to this list. List1.add("Ferrari"), List1.add("Mercedes"), List1.add("BMW"), List1.add("Ford"). Okay, now let's convert this list to an  Array. First, I'll use the  toArray() method without parameters. I write List1.toArray. As you can see, this method returns the object  Array. So, we have to assign the new  Array to an  Array. At the beginning of the code I write,  object[]Array1=. 

Okay, the conversion is ready. Let's print the elements of the List and  Array. S.out.printIn(list1), and S.out.printIn(Arrays.toString(array1)). Let's run and see. As you can see, the elements of the List and  Array are printed on the console. So, the conversion is successful. Now, let's use the  toArray() method with parameter. First, I'll convert this line to the comment line. Okay, now I will create an  Array, String[]  Array1= new String[]. So, we have to write the size of this  Array. You can write the size of the list or you can only put a zero. But the point you need to pay attention to is this; the size you write here must not be larger than the size of the list, otherwise non-correlated elements are converted to null. For example, currently, the size of this list is four. If we type five here, the fifth element will be printed as null. But if you write zero, one, two, three or four here, the output will be the same, but the correct usage is to write the size of the list. 

So, list1.size. Okay, now let's continue. In the next line, I write,  Array1=list1.toArray(Array1). The conversion is ready. Let's run the code and test it. As you can see, the output is the same. Also, you can write it in one line. Inside the parentheses, you can use the new keyword. So, I'll convert this line to the comment line and I'll copy the part after the equal sign and paste it inside the parentheses. Okay, this will be a string  Array. Okay, so this is also valid. You can use this syntax. Now let's run and see. As you can see, the output is the same. Also, other than the ready made method, you can convert it with the for or for-each loop, let's use the for loop. First, I'll copy this line and paste it here. So we need to create an  Array and I will convert this line to the comment line again. Let's create the for loop, for(int i=0). The condition is, i< list1.size and i ++. In the for loop, I write,  Array 1[i] and after the equal sign, I write, list1.get. 

So, thanks to the get() method, we can get the elements of a list which is the specific index. So, in parentheses I write i. 'i' represents the index of the list and  Array. In each loop, the elements of the list will assign to the  Array respectively. So, let's run and see. As you can see, the output is ready. Also, after the conversion, you can add many more elements to the list, but we cannot add an element to the  Array because the size of the  Array is fixed, not changed. For example, let's add the fifth element to the list; list.add("Opel"). Let's run it one more time. As you can see, the new element is added to the list. Yes, the conversion from the List to an  Array is like this. Now, let's look at the conversion from the  Array to a List. Let's create an animals Array; String[]  animalsArray= new String[], and inside the curly braces, I'll create the elements. 

The first element can be "Dog", the second can be "Cat", and third can be "Cow", and the last element can be "Lion". Okay, the  Array is ready. Also, you can create the  Array just by typing the curly braces. Now, let's convert this  Array to an  ArrayList. First, I will use the asList() method of the  Arrays class. So I write, list < String > animalsList= Arrays.asList. As the parameter, l'll write the  Array name, ie., animalsArray. Now, let's print the elements of the  Array and ArrayList; S.out.printIn Arrays.toString(animalsArray), and S.out.printIn(animalsList). Let's run and see. As you can see, the conversion is successful. Also, if you use the  ArrayList instead of the List, this time you should assign the  Arrays.toArray() method as the constructor parameter of the  ArraysList. Let's change it. I'll copy and paste this line. 

I'll convert this line to the comment line. This will be ArrayList, and after the equal sign, I write new ArrayList, and in parentheses I write Arrays.asList(animalArray). So, this usage is also valid. Now, I will convert this line to the comment line. Now, let's use the addAll () method of the Collections class. First, I will create an  ArrayList. ArrayList < string > animalsList= new  ArrayList<>(). And in the next line I write, Collections.addAll. The addAll() method takes two parameters. The first one is the name of the list, so the animalsList, and the second parameter is the name of the  Array, so the  animalsArray. Okay, let's run and see. As you can see, the output is the same. Also, we can convert it using the for or for-each loop. If you want this time, let's use the for-each loop. First, I will convert this line to the comment line. 

And let's create the for-each loop, for(String animal  : animalsArray). And in the loop, I write animalsList.add (animal). Thus, in each iteration, the elements of the  Array will be added to the  ArrayList. Let's run and see. As you can see, the output is the same. Also, you can add an element to the  ArrayList after the conversion. Let's add another animal to the list, animalsList.add("Monkey"). Let's run and see. As you can see, the monkey is added to the list, but here is a small detail you need to pay attention to. If you do the conversion using the asList() method, you can no longer add an element to the list because these will now be linked  Arrays and you can't add elements since the  Arrays are fixed in size. Let's just do an example. First of all, let's convert the for loop here to a comment line. Let's also activate the asList() method and I will also add the comment on this line. Yes, let's run the application now. 

And as you can see, we are getting the unsupported operation exception error because we have done the conversion using the asList method and adding a new element is no longer allowed. So, what happens if we change any element? For example, let's change the first element to Rabbit.  AnimalsArray[0]=''Rabbit''. And let's run the code and observe the output. As you can see, the first element of the  Array and  ArrayList has changed. The reverse is also true. So, if you change an element of the  ArrayList, both will be changed. For example, let's change it to animalsList.set. The Parameters will be zero and "Rabbit". Yes, let's run and test it one more time. As you can see, both have changed. As a result, if you do the conversion using the asList() method, you can no longer add elements to the  Array or delete any element of the  Array. If you change an element, this time the change is applied to both. Do not forget this. Yes, let's take a short break here. See you in the next lesson.

 

About the Author
Students
409
Courses
35
Learning Paths
2

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