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
PushBullet is an app to talk with people. Look it up and let me know u got it.
lisov135 [29]

Answer:

k

Explanation:

5 0
3 years ago
Read 2 more answers
Which functions are available in a Word table? Check all that apply.
dexar [7]
All of them are available
3 0
3 years ago
Consider a situation where we have a file shared between many people.  If one of the people tries editing the file, no other pe
Sergeu [11.5K]

Explanation:

I am a collection of this process from Trash on the computer system and the program that included a new twist on the computer and the skin that appears after loading and the Windows operating system it was much that appear on identifying long horizontal bar at the bottom of the screen and water pollution and its control car parts of the computer with enough letters and adware if for the collection of the fat calculation in the complete committee that in the water method is a product of a screensaver been disciplined displayed at the subject of the right click an icon To Number Sau new file getting opened on you can getting opened on a forgiving of an operational effectiveness of the program is a false false false

7 0
3 years ago
Jerry suspects that information about his Internet usage was sent to external websites without his knowledge. He is wary about t
balu736 [363]
I would say some kind of anti cookie software. Cookies are what the websites store to track info about you and your preferences. If you blocked their data collectors or turned of cookies for websites, you would likely stop getting ads and newsletters.
3 0
3 years ago
Insect population An insect population doubles every generation. Write a while loop that iterates numGeneration times. Inside th
yuradex [85]

Answer:

function currentPopulation = CalculatePopulation(numGeneration, initialPopulation)

    i = 1;

    currentPopulation = initialPopulation;

    while(i <= numGeneration)

         currentPopulation = 2* currentPopulation;

         i= i+1;

    end

end

Explanation:

  • Assign 1 to i as an initial loop value .
  • Assign  initialPopulation to currentPopulation  variable.
  • Run the while loop until i is less than numGeneration.
  • Inside the while loop, double the currentPopulation  variable.
  • Inside the while loop, Increment the i variable also.

6 0
3 years ago
Other questions:
  • What type of stud is placed below a windowsill to support its weight
    12·1 answer
  • To extend the bottom border of a hyperlink across the complete width of a navigation list, change the ____ property of each hype
    9·1 answer
  • Type the correct answer in the box. Spell all words correctly.
    13·1 answer
  • In preparing categorical variables for analysis, it is usually best to​ Select one:
    6·1 answer
  • _________ involves connecting geographically remote computers into a single network and combining the computational power of all
    6·2 answers
  • Explain the importance of calculating a robot’s ability to extend its arm
    15·1 answer
  • What is ambient sound, and what is its purpose in film? When can it be a drawback ?
    7·1 answer
  • Amazon Web Services and Microsoft Azure are some of the most widely used _______.
    7·1 answer
  • Which of the following audio file formats is best
    13·1 answer
  • What is the most secure method for controlling privileged access to a system available for use in a public area?a. Database view
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!