连个字符串交叉组合(第一篇,试试手)

String str1 = "ABCDE";

String str2 = "abcde44";

  // 字符转换成数组   char[] arr1 = str1.toCharArray();   char[] arr2 = str2.toCharArray();

  System.out.println(arr1);   System.out.println(arr2);

// 获取数组长度
  int i = arr1.length;
  int j = arr2.length;
  int count ;
  
  // 遍历数组
  if (i > j) {
   count =i;
  } else {
   count = j;
  }
  List<Character> list = new ArrayList<Character>();
  
  for (int x=0;x<count;x++){
   if(x<i){
    list.add((arr1[x]));
   }
   if(x<j){
    list.add((arr2[x]));
   }
  }
  
  //定义最终字符串
  String s="";
  for (Character cha : list) {
   s=s+cha;
  }
  
  //获取结果
  System.out.println(s);
 }

posted @ 2015-10-18 17:12  天涯已可  阅读(261)  评论(0编辑  收藏  举报