1 //设计一个类,其功能是实现两个列表,交错合并列表元素,形成一个新列表 2 //eg给定列表[a,B,C]和[1,2,3],合并后形成[a,1,B,2,C,3] 3 package classwork9; 4 5 import java.util.Arrays; 6 7 public class Hebing { 8 private String[] str; 9 private Integer[] num; 10 public Hebing() { 11 12 } 13 14 public Hebing(String[] str, Integer[] num) { 15 this.str = str; 16 this.num = num; 17 } 18 19 20 public void testMethod() { 21 // 计算两个数组的总长度,用于设置新数组的长度; 22 int size = str.length + num.length; 23 // 声明一个新数组,用于存放交替合并后的内容; 24 String[] str2 = new String[size]; 25 int j = 0; 26 // 遍历输出两个给定数组的值,交替赋值到新数组; 27 for (int i = 0; i < size / 2; i++) { 28 str2[j] = str[i]; 29 str2[j + 1] = num[i].toString(); 30 j += 2; 31 } 32 // 打印交替和并结果; 33 System.out.println(Arrays.toString(str2)); 34 } 35 36 } 37 package classwork9; 38 39 public class HebingTest { 40 41 public static void main(String[] args) { 42 String[] str=new String[]{"A","B","C","D"}; 43 Integer[] num=new Integer[]{1,2,3,4}; 44 Hebing a=new Hebing(str,num); 45 a.testMethod(); 46 } 47 48 }
设计一个类,其功能是实现两个列表,交错合并列表元素,形成一个新列表
原文:https://www.cnblogs.com/dss-99/p/14127401.html