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
In a _____ network, each device on the network is attached to a central router. If the router fails, then the other devices will
Damm [24]

Answer:

In a STAR TOPOLOGY network, each device on the network is attached to a central router. If the router fails, then the other devices will be unable to communicate, but if only one connected device fails, then all other devices will still be able to communicate.

Explanation:

In this type of topology all the computers are connected to a single router through a cable. This router is the central node and all others nodes are connected to the central node.

7 0
3 years ago
Four categories of installer apps
Naddika [18.5K]

Answer:

I find 5 categories

Explanation:

1 Overview

2 Necessity

3 Types

4 Attended installation

4.1 Silent installation

4.2 Unattended installation

4.3 Headless installation

4.4 Scheduled or automated installation

4.5 Clean installation

4.6 Network installation

5 Installer

5.1 Bootstrapper

5.2 Common types

5.3 System installer

7 0
3 years ago
Please tell fast plzzzzzz.​
lana [24]

Answer:

True

Explanation:

4 0
3 years ago
Give me a code that draws a house with a roof in python. Please Answer!!!
Varvara68 [4.7K]

import turtle

window = turtle.Screen()

tr = turtle.Turtle()

tr.forward(100)

tr.left(90)

tr.forward(100)

tr.left(90)

tr.forward(100)

tr.left(90)

tr.forward(100)

tr.back(100)

tr.left(120)

tr.forward(75)

tr.right(78)

tr.forward(60)

window.mainloop()

In my code, we use the turtle module for the graphics to draw the house with a roof.

8 0
3 years ago
What is the use of tag in XML sitemap?
podryga [215]

Answer:

It works as a roadmap which tells search engines what content is available on the website and leads search engines to most important pages on the site. The standard XML tag used for sitemaps can be described as a schema and is used by all the major search engines.

Explanation:

mark me brainliest!!

8 0
2 years ago
Other questions:
  • 7.12 LAB: Contains the character
    12·2 answers
  • Another html/css assignment, drop css code below, thank you
    15·1 answer
  • Which composer below was not part of the classical period? <br> A. Beethoven B. Bach<br> C. Mozart
    6·1 answer
  • Write a calculate_sq_inches_of_good_pizza function that accepts the diameter of a pizza and returns the area of the pizza minus
    7·1 answer
  • I like the impact that the increasing number of social grants may have on the things mothers​
    14·1 answer
  • 2. Explain the difference between a JMP instruction and CALL instruction
    6·1 answer
  • Select the statements that are true regarding IP addresses. Check All That Apply assigned to your home network by your ISPassign
    13·1 answer
  • when demonstrating 2022 altima’s parking assistance, what steering feature should you mention when pointing out how easy the veh
    10·1 answer
  • 9.11: Array Expander
    15·1 answer
  • 30 POINTS FOR THE CORRECT ANSWERS
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!