1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
notka56 [123]
4 years ago
10

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

Computers and Technology
1 answer:
nata0808 [166]4 years ago
5 0

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.
You might be interested in
Design a BCD-to-Gray code decoder. Your decoder will have 4 inputs: A, B, C and D, representing a 4-bit BCD code (A being the MS
ra1l [238]

Answer:

Binary to Gray Code Converter

The logical circuit which converts the binary code to equivalent gray code is known as binary to gray code converter. An n-bit gray code can be obtained by reflecting an n-1 bit code about an axis after 2n-1 rows and putting the MSB (Most Significant Bit) of 0 above the axis and the MSB of 1 below the axis.

The 4 bit binary to gray code conversion table is given in attached file.

3 0
3 years ago
Read 2 more answers
Different ____________ are assigned to track multiple reviewers of a document.
Lemur [1.5K]
It could be c or d because when you are chatting to someone through say Skype you use fonts and colors to identify who you are talking to. But in most cases it would be d
3 0
3 years ago
What is the relationship between interrupt and buffer <br>​
Jlenok [28]

Answer:

Operating systems have some code called an 'interrupt handler', which prioritises the interrupts and saves them in a queue. Buffers are used in computers as a temporary memory area, and they are essential in modern computers because hardware devices operate at much slower speeds than the processor.

8 0
2 years ago
Hexadecimal (19BACDFE)H into Octal. Please write step by step.
lubasha [3.4K]
The H idk, but till E is i think 3156546776
4 0
4 years ago
Derek’s supervisor is concerned that the company’s security system does not comply with the new standards the company has decide
miv72 [106K]

Answer:

Access Control List is the correct answer to the following question.

Explanation:

The following answer is correct because by the ACL we can access and refusal of services that is controlled. It used to filter traffic for the network and it also used give the extra securities to protect the network. AVL provides an authorization attached to the object that define which persons are allowed for access to the object and operations.

8 0
3 years ago
Other questions:
  • If the object instance is created in a user program, then the object instance can access both the public and private members of
    9·1 answer
  • How many bones are in a human body
    7·2 answers
  • You would like to create a graph that shows how much of your total investments are in stocks, savings, and mutual funds. You sho
    5·2 answers
  • Write a loop statement to count and display the number of positive integers in the array. That is, your loop should display the
    14·1 answer
  • Draw a flowchart and write pseudocode to represent the logic of a program that allows the user to enter values for the width and
    15·1 answer
  • jane feels listening to music at night helps her to sleep. should jane listen to music the night before a test ​
    10·1 answer
  • Which platform is the best? (X-box, PC, Playstation, Nintendo Switch, Wii)
    10·1 answer
  • Why are various creative works split up into different types under copyright law?
    9·1 answer
  • What does error code 18 indicate?
    11·1 answer
  • List analog devices, Digital devices and hybrid devices
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!