Zipping arrays Write two static methods named zip (in a class named Zip) that ta
ID: 673495 • Letter: Z
Question
Zipping arrays Write two static methods named zip (in a class named Zip) that takes two int sequences and returns a single int sequence with the two arguments "zipped" together. One method will work on arrays, the other on ArrayLists. Zipping two sequences means: take the first element of the first sequence, add the first clement of the second equence, then add the second clement of the first scqcucncc, and the second element of the sccond sequence, and so forth and so on while each input has elements. The following isn't valid Java, but is intended as example of what zip() should do. 2ip( [1,2,3] , [11,12,13] ) [1,11,2,12,3,13] 2ip( [1,2,3,4] , [11,12] ) [1,11,2,12,3,4] zip( [1,2,3] , null ) [1,2,3] One or both arguments may be null, which should be treated as an empty array. If both arc null return a zero-length sequence. The class Zip has two starter methods you should fill out with your solution. For the method that uses arrays, do not use any ArrayLists. Similarly, for the version that uses ArrayLists, do not use arrays. Do test your methods thoroughly and submit your tests along with your solution. You can, although don't need to, make use of the ZipTester class (in the framework code). It includes some framework code to test two sequences and print out the result, a method to get an array from the keyboard, and so forth.Explanation / Answer
package Cryptography;
import java.util.*;
public class Zip
{
public static int[] zip(int arr1[],int arr2[])
{
int result[]=new int[arr1.length+arr2.length];
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
System.out.println(" Now zipping the contents...");
int p=0,q=0,r=0;
while(p<arr1.length||q<arr2.length)
{
if(p<arr1.length)
{
result[r++]=arr1[p++];
}
if(q<arr2.length)
{
result[r++]=arr2[q++];
}
}
return(result);
}
public static void main(String args[])
{
int a1[]={1,2,3,4,5};
int b1[]={9,10,11,12,13};
System.out.println(Arrays.toString(zip(a1,b1)));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.