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
Marat540 [252]
3 years ago
6

Language/Type: Java ArrayList Collections

Computers and Technology
1 answer:
Sergio039 [100]3 years ago
4 0

Answer:

<h2>Answer a)</h2>

public void swapPairs(ArrayList<String> list) {

   for(int i = 0; i <= list.size() - 2; i += 2) {        

       String val= list.get(i + 1);

       list.set(i + 1, list.get(i));

       list.set(i, val);     }   }          

<h2>Answer b)</h2>

Using the Iterator interface to remove the strings of even length from the list:

public void removeEvenLength( ArrayList<String> list) {

   Iterator<String> iter= list.iterator();

   while( iter.hasNext() ) {

       String str= iter.next();

       if( str.length() % 2 == 0 ) {

           iter.remove();         }     }   }

Not using Iterator interface:

public static void removeEvenLength(ArrayList<String> list) {  

   for (int i = 0; i < list.size(); i++) {

       String str= list.get(i);

       if (str.length() % 2 == 0) {

           list.remove(i);

           i--;         }     }  }

<h2>Answer c)   </h2>

public static void removeDuplicates(ArrayList<String> list) {  

   for (int i = 0; i < list.size() - 1; i++) {

       if (list.get(i).equals(list.get(i + 1))) {

           list.remove(i + 1);

           i--;         }     }    }

<h3></h3>

Explanation:

a) The method swapPairs(ArrayList<String> list) ArrayList is used to store elements of String type. The for loop has a variable i that is initialized by 0. It moves through the list. This loop's body stops executing when the value of i exceeds the size-2 of the list. The size() method returns the size of the list. This means i stops moving through the list after the last pair of values is reached. In the loop body, get(i+1) method is used to obtain the element in the list at a index i+1. Lets say in the list {"four", "score", "and", "seven", "years", "ago"}, if i is at element "four" then i+1 is at "score". Then "score" is stored in val variable. Then set() method is used to set i-th element in an ArrayList object at the i+1 index position. The second set(i,val) method is used to set the value in val variable to i-th position/index. So this is how score and four are switched.

b) The method removeEvenLength( ArrayList<String> list) takes ArrayList of Strings as parameter. Iterator is an interface which is used to traverse or iterate through object elements and here it is used to remove the elements. The iterator() method is used to traverse through the array list. The while loop will keep executing till all the elements of the ArrayList are traversed. hasNext() method is used to return True if more elements in the ArrayList are to be traversed. next() method is used to return next element in the ArrayList and store that element in str variable. Then the if statement is used to check if the element in str variable is of even length. The length() function returns the length of the element in ArrayList and modulus is used to check if the length of the element is even. If the condition evaluates to true then the remove() method is used to remove that element of even length from the list.

c) The method removeDuplicates(ArrayList<String> list) takes ArrayList of Strings as parameter and eliminates any duplicates from the list. It has a for loop that moves through the list and it stops executing when the value of i exceeds the size - 1 of the list. The if condition has two method i.e. get() and equals(). The get() method is used to obtain the element at the specified index position and equals() function is used to compare the two string elements. Here these two strings are obtained using get() method. get(i) gets element at i-th index and get(i+1) gets element at i + 1 position of the list. If both these elements are equal, then the element at i+1 index is removed. This is how all duplicates will be removed.

You might be interested in
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
scoray [572]
,,,,,,,,,,,,,,,,,,,,,,
4 0
3 years ago
If electricity comes from electrons, does morality come from morons?
Vsevolod [243]
Lol morons like to give lessons
8 0
3 years ago
Read 2 more answers
If you were going to construct a table that only included shape names with number prefixes, which shape would you include
NikAS [45]
The answer would be A. square
3 0
4 years ago
C++Assign to maxSum the max of (numA, numB) PLUS the max of (numY, numZ). Use just one statement. Hint: Call FindMax() twice in
neonofarm [45]

Answer:

Replace the comment with:

maxSum =FindMax(numA,numB)+FindMax(numY,numZ);

Explanation:

Required

The statement to add up the two maximum from the functions

To do this, we have to call FindMax for both arguments i.e.

FindMax(numA,numB) and FindMax(numY,numZ) respectively

Next, use the + sign to add up the returned value.

So, the complete statement is:

maxSum =FindMax(numA,numB)+FindMax(numY,numZ);

<em>See attachment for complete program</em>

6 0
3 years ago
Martha is developing a software program in C++ and has a question about how to implement a particular feature. She performs an o
VLD [36.1K]

Answer:

A blog

Explanation:

A blog is a regularly updated website or web page, typically one run by an individual or small group, that is organised by posts.

7 0
3 years ago
Other questions:
  • What is the formula for calculating the average of cells<br> C2 through C30?
    15·1 answer
  • Whaat was the first sound recording machine
    12·2 answers
  • "when a dynamic web page is requested, the web server passes the request to"
    15·1 answer
  • Framing can create which of the following in a photograph? Mystery Saturation Aperture All of the above
    10·2 answers
  • A common type of non-volatile memory is _____.
    12·1 answer
  • Write an application that inputs three numbers (integer) from a user. (10 pts) UPLOAD Numbers.java a. display user inputs b. det
    10·1 answer
  • Define the missing method. use "this" to distinguish the local member from the parameter name.
    11·2 answers
  • The ____ documents a system at the end of the design phase, identifies any changes since the beginning of the project, and inclu
    10·2 answers
  • A garments manufacturing company buys various types of natural and synthetic materials to produce clothes. Which material is a s
    6·2 answers
  • In what ways can information be slanted in a news report?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!