交叉合并:如字符串一为:abcd 字符串二为:1234。则结果为:a1b2c3d4。
import java. util. ArrayListimport java. util. Listpublic class A {
public static void main(string[] args){
string a[]={"a","b","c"}
string b[]={"1","2","3"}
CombineTwoArray(a,b)
system. out. println(CombineTwoArray(a,b))
}
public static List<string>combineTwoArray(string[] first, String[] second)t List<string>outlist=new ArrayList<string>()
int index=0
while(index<first. length &&index<second. length){
outlist. add(first[ index])outlist. add(second[ index])
++indexif(index>=first. length){
for (int i=indexi<second. lengthi++){
outlist. add(second[i])
}
} else{
for (int i=indexi<first. lengthi++){
outlist. add(first[i])
}
return outlist
}
}
js的写法:
<!DOCTYPE html pUBLIC"-//w3c//DTD XHTMT 1.0
Transitional//EN""http://www.w3.org/TR/xhtm11/DrD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtm1">
<head>
<title>第十五题:交叉合并字符串</title>
<style>
#form{
margin:0auto;width:500px;top:100px;height:300px;
</style>
<script type="text/javascript">
var reg="*[A-za-20-9]+$";function check(){
var strl =document.all.strl.value;var str2 =document.all.str2.value;if(str1==""){
alert(“字符串一不允许为空!");document.all.strl.focus();return;
}else if(!/^[A-2a-20-9]+$/.test(str1)){
alert("字符串一只能是数字和字母的组合");document.all.str1.focus();return;
}else if(str2==""){
alert(”字符串二不允许为空!");document.all.str2.focus();
return;
}else if(!/^[A-Za-20-9]+5/.test(str2)){
alert(“字符串二只能是数字和字母的组合");document.a1l.str2.focus();return;var string="";var font1="<font style='color:red;'>";var font2="<font style='color:blue;'>";var fontend="</font>";if(str1.1ength>=str2.1ength){
for(vari=0;i<str1.1ength;i++){
if(i<str2.length){
string +=font1+strl[i]+fontend+ font2+ str2[i]+fontend;
}else{
string+=font1+strl[il;
}else{
for(var i=0;i<str2.1ength;i++){
if(i<strl.length){
document.all.result.innerHTML=string;
</script>
</head>
<body>
<div id="form">
<center><h2>第十五题:交叉合并字符串</h2></center>
字符串1:<input type="text"id="str1"/><br/>字符串2:<input type="text"id="str2"/><br/>
<input type="button"value="交叉合并字符串”onclick="check();"/>
<br/>
<div id="result"></div>
</div>
</body></html>
string+=font1+strl[i]+fontend+font2+str2[i]+fontend;
}else{
string+=font2+str2[i]+fontend;
扩展资料:
/**
* 合并两个函数
* @param functionA 先执行
* @param functionB 执行完 functionA 后返回
* @returns {*}
*/
function mergeFunction (functionA, functionB) {
if (!functionA || !functionB) return
var merge = functionB
functionB = (function () {
merge.call(this)
functionA.call(this)
})()
return functionB = merge
}
楼上的答案确认是对的,再给你一个答案,我觉得我写的比楼上的啰嗦。
/*var datas = [
['红色', '蓝色'],
['XL'],
['图片', '文字']
]
*/
/*
var datas = [
['红色', '蓝色'],
['XL'],
['图片', '文字'],
[1,2,3,4,5,6,7,8]
]
var datas = [
['红色', '蓝色'],
['XL','MS'],
['图片', '文字']
]
*/
var datas = [
['红色', '蓝色'],
['XL','MS'],
['图片', '文字'],
['WIN','LINUX','UNIX','OSX']
]
var merge1 = function(arr1,arr2) {
var result = []
for(var i=0i<arr1.lengthi++) {
for(j=0j<arr2.lengthj++) {
var tmp = []
tmp.push(arr1[i])
tmp.push(arr2[j])
result.push(tmp)
}
}
return result
}
var merge2 = function(arr1,arr2) {
var result = []
for(var i=0i<arr2.lengthi++) {
for(var j=0j<arr1.lengthj++) {
var tmp = copy(arr1[j])
tmp.push(arr2[i])
result.push(tmp)
}
}
return result
}
var copy = function(arr) {
var newArr = []
return newArr.concat(arr)
}
var iterator = function(data) {
return {
data:data,
len:data.length,
current:0,
hasNext:function(){
return this.current < this.len
},
next:function() {
if(!this.hasNext()) {
return null
}
var i = this.current
this.current++
return this.data[i]
}
}
}
var it = iterator(datas)
var a1=null,a2=null
while(it.hasNext()) {
if(a1 == null) {
a1 = it.next()
}
a2 = it.next()
if(a2 == null) {
break
}
if(a1[0] instanceof Array) {
a1=merge2(a1,a2)
}else{
a1 = merge1(a1,a2)
console.log(a1)
}
}
console.log(a1)
在Java中调用js文件中的function,传入调用参数,并获取返回值js文件中的merge函数将两个参数a,b相加,并返回c。
// expression.js
function merge(a, b) {
c = a * b
return c
}
在Java代码中读取js文件,并参数两个参数,然后回去返回值。
package com.sinaapp.manjushri
import java.io.FileReader
import javax.script.Invocable
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
/** * Java调用并执行js文件,传递参数,并活动返回值 * * @author manjushri */
public class ScriptEngineTest {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager()
ScriptEngine engine = manager.getEngineByName("javascript")
String jsFileName = "expression.js" // 读取js文件
FileReader reader = new FileReader(jsFileName) // 执行指定脚本
engine.eval(reader)
if(engine instanceof Invocable) {
Invocable invoke = (Invocable)engine // 调用merge方法,并传入两个参数
// c = merge(2, 3)
Double c = (Double)invoke.invokeFunction("merge", 2, 3)
System.out.println("c = " + c)
}
reader.close()
}
}
输出结果:
c = 5.0