In this exercise, you will create a couple of helper methods for ArrayLists in a class called ArrayListMethods.

Create three static methods:

print- This method takes an ArrayList as a parameter, and simply prints each value of the ArrayList on a separate line in the console.
condense- This method takes an ArrayList as a parameter, and condenses the ArrayList into half the amount of values. While traversing, this method will take the existing value at the index and add the index following to the existing value. For example, if we had an ArrayList that consisted of Strings ["0", "1", "2", "3"], the ArrayListMethods.condense(["0", "1", "2", "3"]) would alter the ArrayList to be ["01", "23"].
duplicate- This method takes an ArrayList and duplicates the value of the index at the position index + 1. As a result, ArrayListMethods.duplicate(["01", "23"] would be ["01", "01", "23", "23"].
If done correctly, the methods should work in the ArrayListMethodsTester file.

[ArrayListMethodsTester.java]

import java.util.ArrayList;

public class ArrayListMethodsTester
{
public static void main(String[] args)
{
ArrayList stringArray = new ArrayList();
stringArray.add("This");
stringArray.add("is");
stringArray.add("an");
stringArray.add("ArrayList");
stringArray.add("of");
stringArray.add("Strings");

ArrayListMethods.print(stringArray);
System.out.println("\nArrayList is condensing:");
ArrayListMethods.condense(stringArray);
ArrayListMethods.print(stringArray);
System.out.println("\nArrayList is duplicating:");
ArrayListMethods.duplicate(stringArray);
ArrayListMethods.print(stringArray);

}
}

[ArrayListMethods.java]

import java.util.ArrayList;
public class ArrayListMethods
{



}

Respuesta :

Answer:

package lab1;

import java.util.ArrayList;

import java.util.Iterator;

public class ArrayListMethodsTester

{

public static void main(String[] args)

{

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

stringArray.add("This");

stringArray.add("is");

stringArray.add("an");

stringArray.add("ArrayList");

stringArray.add("of");

stringArray.add("Strings");

System.out.println("Printing the arraylist:");

ArrayListMethods.print(stringArray);

System.out.println("\nArrayList is condensing:");

ArrayListMethods.condense(stringArray);

ArrayListMethods.print(stringArray);

System.out.println("\nArrayList is duplicating:");

ArrayListMethods.duplicate(stringArray);

ArrayListMethods.print(stringArray);

}

}

class ArrayListMethods

{

static void print(ArrayList stringArray)

{

  Iterator it = stringArray.iterator();

  while (it.hasNext())

     System.out.println(it.next() + " ");

     

}

static void condense(ArrayList stringArray)

{

  int length=stringArray.size();

     

  ArrayList cloneArray=(ArrayList) stringArray.clone();

     

  stringArray.clear();

  StringBuilder sb=new StringBuilder("");

     

  for (int i = 0; i < (length/2); i++)

  {

      sb.append(cloneArray.get(i));

      sb.append(" ");    

  }

  stringArray.add(sb);

     

  StringBuilder sb1=new StringBuilder("");

  for (int i = (length/2); i < length; i++)

  {

      sb1.append(cloneArray.get(i));

      sb1.append(" ");    

  }

  stringArray.add(sb1);

  }

static void duplicate(ArrayList stringArray)

{

  for (int i = 0; i < stringArray.size(); i+=2)

  {

      stringArray.add(i+1,stringArray.get(i));

  }

}

}

Explanation:

  • Get the length of the arraylist  to copy the array into another arraylist .
  • Empty the array so that new content can be stored in it .
  • Use a loop to half of array_list.
  • Use a loop from half of array_list to the end of arraylist and  append all into a single string.