68.4k views
1 vote
Write a method markLength4 that takes an ArrayList of Strings as a parameter and that places a string of four asterisks "****" in front of every string of length 4. For example, suppose that a variable called list contains the following values: {"this", "is", "lots", "of", "fun", "for", "every", "Java", "programmer"} And you make the following call: markLength4(list); then list should store the following values after the call: {"****", "this", "is", "****", "lots", "of", "fun", "for", "every", "****", "Java", "programmer"} Notice that you leave the original strings in the list, "this", "lots", "Java", but include the four-asterisk string in front of each to

User Ho Luong
by
4.8k points

1 Answer

5 votes

Answer:

kindly check explainations for code output.

Step-by-step explanation:

The program code below.

import java.util.ArrayList;

class MarkLength4

{

public static void main(String args[])

{

ArrayList s1=new ArrayList();

s1.add("this");s1.add("is");s1.add("lots");

s1.add("of");s1.add("fun");s1.add("for");

s1.add("every");s1.add("java");

s1.add("programmer");

int i;

System.out.println("Before Marking Length 4:");

System.out.print("s1=[");

for(i=0;i<s1.size();i++)

{

System.out.print(s1.get(i)+" ");

}

System.out.print("]\\");

markLength4(s1);

System.out.println("After Marking Length 4:");

System.out.print("s1=[");

for(i=0;i<s1.size();i++)

{

System.out.print(s1.get(i)+" ");

}

System.out.print("]\\");

}

public static void markLength4(ArrayList s1)

{

ArrayList t1=new ArrayList();

int size=s1.size();

int i;

for(i=0;i<size;i++)

{

String t=s1.get(i).toString();

if(t.length()==4)t1.add("****");

t1.add(s1.get(i));

}

s1.clear();

s1.addAll(t1);

}

}

Check attachment for output.

Write a method markLength4 that takes an ArrayList of Strings as a parameter and that-example-1
User Shaun Rowan
by
5.1k points