Write a method markLength4 that takes an ArrayList of Strings as a parameter and
ID: 3650236 • Letter: W
Question
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 mark it.
Explanation / Answer
Please rate...
Program MarkLength4.java
========================================================
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);
}
}
==========================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.