Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why did you use ArrayList as opposed to defining an array of Strings in your cod

ID: 3818632 • Letter: W

Question

Why did you use ArrayList as opposed to defining an array of Strings in your code? Any advantages of using ArrayList?

Can you implement the same logic with an array of Strings?

ArrayList was used opposed to defining an array of Strings. Can you implement the same logic with an array of Strings with the following code? import java.util.ArrayList; public class Array { public static void main(String args[]) { // create and initialize array of integer int array[] = {6,8,6,8,4}; // Display length int length = array.length; System.out.println("Length of array : " + length); // create Arraylist of years ArrayList nameList = new ArrayList<>(); // add years to arraylist list nameList.add("Johhny Barnes"); nameList.add("Deion Loyd"); nameList.add("Sally Pugh"); nameList.add("Sarah Mckindley"); // print list elements System.out.println("Names on list are:"); for (int i = 0; i < nameList.size(); i++) { System.out.println(nameList.get(i)); } // remove elements from nameList nameList.remove(1); // now again print the list System.out.println(" After removing name at position 1:"); for (int i = 0; i < nameList.size(); i++) { System.out.println(nameList.get(i)); } } }

Explanation / Answer

the main difference between the arraylist and the array is the size of the array is fixed while the size of the arraylist is not fixed and is dynamic ie we can add any number of elements in the arraylist we don't have to mention the initial size of the arraylist

Arralylist is resizeable ie. when the size of the arraylist increaes to threasold capacity the arralist is again created with new size but wit the same contents

while in array the size is fixed we can add the a contant amount of element beyonw which it will give array out of bound exception.

while in the code arraylist of string is used because it is easy to remove element from the arraylist as compared to removing element from the array, since in array for removing the element we need to move all the elements forward so as to remove the current element

import java.util.ArrayList;


public class Array1 {
   public static void main(String args[]) {
       // create and initialize array of integer
       int array[] = {6,8,6,8,4};
       // Display length
       int length = array.length;
       System.out.println("Length of array : " + length);
       // create Arraylist of years
      
       String[] nameList = new String[4];
      
       nameList[0] = "Johhny Barnes";
       nameList[1] = "Deion Loyd";
       nameList[2] = "Sally Pugh";
       nameList[3] = "Sarah Mckindley";
       // print list elements System.out.println("Names on list are:");
       for (int i = 0; i < nameList.length; i++)
       {
           System.out.print(nameList[i]+" ");
       }
       // remove elements from nameList (removing the second element from the array ie. "Deion Loyd")
       int count =0;
       for(int i=0; i<nameList.length; i++)
   {
   if(nameList[i] == "Deion Loyd")
   {
   for(int j=i; j<(nameList.length-1); j++)
   {
       nameList[j] = nameList[j+1];
   }
   count++;
   break;
   }
   }
   if(count==0)
   {
   System.out.print("Element Not Found..!!");
   }
   else
   {
   System.out.print("Element Deleted Successfully..!!");
   System.out.print(" Now the New Array is : ");
   for(int i=0; i<(nameList.length-1); i++)
   {
   System.out.print(nameList[i]+ " ");
   }
   }
      
   }
}

as you can see from the code that to remove the second element from the array i need to write 10 lines of code

since there is no mehod defined for array to remove the element by just mentioning the array index ot the value but the same is poosible with arraylist by just mentioning the index value

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote