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
All character entities end with _____ to signal the browser that everything in between is an entity representing a symbol.
san4es73 [151]

All character entities end <u>with </u><u>;</u><u> to</u> signal the web browser that everything in between is an entity representing a symbol.

<h3>What is a web browser?</h3>

A web browser can be defined as a type of software application (program) that is designed and developed to enable an end user view, access and perform certain tasks on a website, especially when connected to the Internet.

<h3>What is HTML?</h3>

HTML is an abbreviation for hypertext markup language and it can be defined as a standard programming language that is used for designing, developing and creating websites or webpages.

In hypertext markup language (HTML), all character entities generally begin with & and end with ; to signal the web browser that everything in between is an entity which represents a symbol.

Read more on HTML here: brainly.com/question/4056554

#SPJ1

5 0
1 year ago
The ________ utility automatically creates duplicates of your libraries, desktops, contacts, and favorites to another storage lo
olga55 [171]

Answer:

The correct answer for the given question is option(A) i.e File History.

Explanation:

In computer system file history is an backup application which create backup of your data which are stored in your libraries, desktops, contacts, and favorites to another storage location .It creates back up of your data  to another location when your personal files has been changed.

The user can check the file history option in computer system

open control panel >>system and security >> file system

7 0
4 years ago
Can You Get Commission Shortcut For FREE?
ANEK [815]

Answer:

Yeah if you look it up it says you can get it for free

6 0
3 years ago
What are the five steps of ethical hacking?
Paraphin [41]

Answer

Explanation:

Hacking is simply gaining access into a computer through weaknesses that were found in the network system.

We have mainly 5 stages In ethical hacking which although must not be followed sequentially but could produce greater results if it was followed.

These stages includes

1. Reconnaissance

2. Scanning

3. Gaining access

4. Maintaining access

5. Covering tracks.

8 0
4 years ago
The * key on the number keypad is used for _____.
12345 [234]

Answer:

multiply

Explanation:

8 0
3 years ago
Read 2 more answers
Other questions:
  • Where is the typical location of a touchpad inside of a laptop?
    14·1 answer
  • The animation industry is solely reliant on 3-D modeling and 3-D virtualization to create the animated movies in the cinemas.
    9·2 answers
  • In python:
    8·1 answer
  • In general, what are reasons that someone would choose to use lossy compression? Write a brief response below including at least
    10·1 answer
  • Microsoft PowerPoint is popular software that allows you tocreate slides, handouts, notes, and outlines.
    12·1 answer
  • A company has a number of employees. The attributes of EMPLOYEE include Employee ID (identifier), Name, Address, and Birthdate.
    11·1 answer
  • Is ryan patrick cullen gay
    7·2 answers
  • Select the correct answer.
    15·1 answer
  • PLSS HELP ASAP ILL GIVE BRAINLIEST THANKS
    15·2 answers
  • Please help please help
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!