for extra coding space for the . . (20 points) Fill in the skeleton below for a
ID: 3738908 • Letter: F
Question
for extra coding space for the . . (20 points) Fill in the skeleton below for a method named concatenate string as parameters. The method that takes two arrays of should return a new array of String where each element is the corresponding elements at the same position from each of the two arrays passed in "ce"), and the array art 2- as parameters. For example, if the array arr 1- ( "aa", -bb", ("xx", "yy", "zz"),then the call concatenate (arri,ar r 2 ) should return the array aaxx", "bbyy", "Cczz"). Note that if the two arrays are not the same length, then assume that the extra elements are concatenated with the empty String-in this case the array returned will be the length of the longer array and some of the elements of the longer array will be unchanged in the new array. For example, if the array arr 1- { "aa" , "bb") , and the array arr2-1-xx-· -yy zz",then the call concatenate (arrl,arr2) should return the array ("aaxxbbyy. zz"). The two original arrays must NOT be changed by this method and you must NOT prompt the ser for input in this method. rivate static Stringt] concatenate (Stringt arr1, stringtl arr2)Explanation / Answer
StringConcatenateTest.java
import java.util.Arrays;
public class StringConcatenateTest {
public static void main(String[] args) {
String arr1[] = {"abc","bvf","eee"};
String arr2[] = {"vvvv","ee"};
System.out.println(Arrays.toString(concatenate(arr1, arr2)));
}
private static String[] concatenate(String arr1[], String[] arr2) {
String s[] = new String[arr1.length+arr2.length];
int i;
for(i=0;i<arr1.length;i++){
s[i]=arr1[i];
}
for(int j=0;j<arr2.length;j++){
s[i++]=arr2[j];
}
return s;
}
}
Output:
[abc, bvf, eee, vvvv, ee]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.