Question

JAVA need help creating method removeLongestString

List<String> listString = new ArrayList<String> ;

listString.clear ; 1istString.add (three) listString.add (four); remove LongestString(listString); System.out.println( li

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please go through code and output.

CODE:


import java.util.*;
import java.util.List;
import java.util.Arrays;


public class RemoveLongestString {
  
   public static void removeLongestString(List<String> listString)
   {
       int size = 0;
       int count = 0;
       String temp = "";
       for(int i=0; i<listString.size(); i++) // check max length and register it with count
       {
           temp = listString.get(i);
           if(size < temp.length())
           {
               size = temp.length();
               count = i;
           }
          
       }
       listString.remove(count); // remove max length string
   }
   public static void main(String[] args)
   {
       List<String> listString = new ArrayList<String>();
       listString.clear();
       listString.add("three");
       listString.add("four");
       removeLongestString(listString);
       System.out.println(listString);
   }
}

OUTPUT:

[four]

Add a comment
Know the answer?
Add Answer to:
JAVA need help creating method removeLongestString List<String> listString = new ArrayList<String> ; listString.clear ; 1istString.add ("three")...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following code segment. ArrayList<String> towns = new ArrayList<String> (); towns.add("Baltimore"); towns.add("Bethesda"); towns.add("Wheaton"); towns.add("Greenbelt"); towns.add("Rockville");...

    Consider the following code segment. ArrayList<String> towns = new ArrayList<String> (); towns.add("Baltimore"); towns.add("Bethesda"); towns.add("Wheaton"); towns.add("Greenbelt"); towns.add("Rockville"); for (int k = 1; k< towns.size(); k ++) System.out.print(towns.get(k) + " "); What is printed as a result of executing the code segment? Baltimore Bethesda Wheaton Greenbelt IndexOutOfBoundsException message Baltimore Bethesda Wheaton Greenbelt Rockville Bethesda Wheaton Greenbelt Rockville Refer to the doSomething method: public static void doSomething (ArrayList list, int i, intj) SomeType temp = list.get(i); list.set(i, list.get()); list.set(j, temp); Which best describes...

  • Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30;...

    Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30; i-i+3) if(i%) values.add(i); return values: What is returned after the method call nums()? a. [12] b. [13] c. [14] d. [15] e. [16] O Which of the following represents the final output of the code segment below? int k: int[]A; A new int[3]: fork-0; k<A.length;k++) A[k]-A.length-k; forſk-0; k<A.length-1; k++) A[k+1]-A[k) for(int i-0;i<A.length;i++) System.out.print(A[i]+" "); a. 222 O b. 333 c. 444 d. 555 O...

  • Need help with this. Java please! Write a recursive method that counts the number of bowling...

    Need help with this. Java please! Write a recursive method that counts the number of bowling pins given the number of pins in the back row: 1. public static int numPins (int num) numPins(0)-> 0 numPins(1) → 1 numPins(2)-> 3

  • question 27’s end is in the next page 24 Suppose you have created a list using the statement ArrayList String> NYIT CS Courses new ArrayList(31 What would java do when you try to insert more...

    question 27’s end is in the next page 24 Suppose you have created a list using the statement ArrayList String> NYIT CS Courses new ArrayList(31 What would java do when you try to insert more than 31 courses? ANS: LinelI) check one with x: continue process as usual I Ijava automatically expand array space you manually expand more array spaces 25 Suppose NYIT CS COurscs is an object of the class Array List Suring Write Java statements that will display...

  • Please explain clearly. Thank you for your time and help will upvote! 6. Suppose a Java...

    Please explain clearly. Thank you for your time and help will upvote! 6. Suppose a Java method receives a List<Integer> and reverses the order of the items it contains by removing each item from the front of the list and pushing it onto a Stack<Integer), and then popping the items from the stack and inserting each item to the end of the list. What is the expected Big- running time if: a. If an ArrayList is passed. Explain your answer....

  • Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int...

    Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • edhesive Term 2 - Unit 5 Week 4 19. Consider the following code segment: ArrayList<Light bulbs...

    edhesive Term 2 - Unit 5 Week 4 19. Consider the following code segment: ArrayList<Light bulbs - new ArrayList<light> (3 bulbs.add(new Light() bulb, remove (0) bulbs.add(new Light () Light b new Light () bulbs.add(1, b); bulbs.add(new Light()); bulbs.remove(0); bulbs.add(new Light()); bulbs.remove (2); bulbs.add(new Light(); After running the code, what is the size of bulbs? h 3 d 5 20. An ArrayList can hold _ a. Only primitive types b. Both class and primitive types c Only class types d. Only...

  • JAVA - Given two List objects (input and output), write a generic method that 1) clears...

    JAVA - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double). import java.util.List; import java.util.ArrayList; public class Collections { // Modify method signature if needed & implement the body of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT