bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 12, Problem 6E

Explanation of Solution

Method definition for “getCommonStrings”:

/* Method definition for "getCommonStrings"

public static ArrayList<String> getCommonStrings(ArrayList<String> list1, ArrayList<String> list2)

{

    /* Create array for store resultant strings */       

ArrayList<String> resultantStrings = new ArrayList<String>();

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

    {

        for(int j = 0; j < list2.size(); j++)

        {

/* Check if the string in "list1" is equal to string in "list2", then */

            if(list1.get(i).equals(list2.get(j)))

/* Add the given string to "resultantStrings" */

resultantStrings.add(list1.get(i));

        }

    }

    /* Removing duplicate strings using "for" loop */

for(int index = 0; index < resultantStrings.size(); index++)

    {

        int idx = index+1;

/*This loop will performs "idx" is less than "data.size()"  */

        while(idx < resultantStrings.size())

        {

/* If the character "resultantStrings.get(index)" is equal to "resultantStrings.get(idx)", then */

if(resultantStrings.get(index) == resultantStrings.get(idx))

/* Remove the character by using "remove" method */

                resultantStrings.remove(idx);

            //Otherwise

            else

                //Increment the value of "idx"

                idx++;

        }

    }

    return resultantStrings;

}

Complete code:

The complete code for after implementing “getCommonStrings” method is given below:

//Import required package

import java.io.*;

import java.util.*;

//Define "CommonStringsTest" class

public class CommonStringsTest

{

    //Define main function

    public static void main(String[] args)

    {

        //Create an array list for list 1

ArrayList<String> list1 = new ArrayList<String>();

        //Create an array list for list 2

ArrayList<String> list2 = new ArrayList<String>();

        /* Add string to list 1 using "add" method */

        list1.add("John");

        list1.add("Merry");

        list1.add("Rose");

        list1.add("Jansi");

        /* Add string to list 2 using "add" method */

        list2.add("Rose");

        list2.add("Watson");

        list2.add("John");

        list2...

Blurred answer
Students have asked these similar questions
2. Perform resource allocation for the following project. Resource limits are 6 labors and 2 helpers. Legend: Activity Dur Resources G H 2 3 2L 1H 2L OH A 1 3L 1H + B D F J K 3 4 6 2 4 4L 2H 3L OH 4L 1H 2L 2H 4L 2H C E 2 2 I 1 2L 1H 3L 1H 5L 1H
Need Java method please. Thank you.
Need Java method please. Thank you.

Chapter 12 Solutions

Java: An Introduction to Problem Solving and Programming plus MyProgrammingLab with Pearson eText -- Access Card Package (7th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License