java数组拼接
① 在java里java字符串数组合并 怎么合并成一个数组
具体如下:
java字符串数组合并,可以使用array.复制方法,如下代码:
package com.qiu.lin.he;
import java.text.ParseException;
import java.util.Arrays;
public class Ceshi {
public static void main(String[] args) throws ParseException {
String[] str1 = { "J", "a", "v", "a", "中" };
String[] str2 = { "如", "何", "把", "两", "个", "数", "组", "合", "并", "为",
"一", "个" };
int strLen1 = str1.length;// 保存第一个数组长度
int strLen2 = str2.length;// 保存第二个数组长度
str1 = Arrays.Of(str1, strLen1 + strLen2);// 扩容
System.array(str2, 0, str1, strLen1, strLen2);// 将第二个数组与第一个数组合并
System.out.println(Arrays.toString(str1));// 输出数组
}
}
② Java中如何把两个数组合并为一个
import java.util.Arrays;
//Java中如何把两个数组合并为一个
public class gog {
public static void main(String[] args) {
String [] str1 = {"J","a","v","a","中"};
String [] str2 = {"如","何","把","两","个","数","组","合","并","为","一","个"};
int strLen1=str1.length;//保存第一个数组长度
int strLen2=str2.length;//保存第二个数组长度
str1= Arrays.Of(str1,strLen1+ strLen2);//扩容
System.array(str2, 0, str1, strLen1,strLen2 );//将第二个数组与第一个数组合并
System.out.println(Arrays.toString(str1));//输出数组
}
}
③ java,数组拼接,求大神帮忙!
publicstaticvoidmain(String[]args){
Listlist=newLinkedList();
intsize=3;
int[]a={2,5,6,7,9,5,7};
int[]b={1,7,4,3,4};
list.add(a);
list.add(b);
int[]ints=concatArr(list,size);
StringBuilderrs=newStringBuilder();
for(intanInt:ints){
rs.append(anInt+",");
}
System.out.println(rs.substring(0,rs.length()-1));
}
publicstaticint[]concatArr(Listlist,Integersize){
Listrs=newLinkedList();
intcount=list.size();
intindex=0;
while(count>0){
for(Objecto:list){
for(inti=index;i<index+size;i++){
try{
int[]arr=(int[])o;
rs.add(arr[i]);
}catch(Exceptione){
count--;
break;
}
}
}
index+=size;
}
int[]rsArr=newint[rs.size()];
for(inti=0;i<rs.size();i++){
rsArr[i]=(int)rs.get(i);
}
returnrsArr;
}
④ 如何在java里java字符串数组合并成一个数组
java里java字符串数组合并成一个数组方法如下:
//方法一 Arrays类
String[] a = {"A","B","C"};
String[] b = {"D","E"};
// List<String> list = Arrays.asList(a); --OK
// List<String> list = Arrays.asList("A","B","C"); --OK
// list.add("F"); --UnsupportedOperationException
// list.remove("A"); --UnsupportedOperationException
// list.set(1,"javaee");--OK (因为是把数组转为集合,其本质还是数组,数组长度固定不变,但内容可以改变)
// 结论:虽然可以把数组转为集合,但是集合长度不能改变
List list = new ArrayList(Arrays.asList(a));
list.addAll(Arrays.asList(b));
String[] str = new String[list.size()];
list.toArray(str);
for(int x=0;x<str.length;x++){
System.out.print(str[x] + " ");
}
//方法二 循环遍历
// 两个数组合并
String[] str1 = {"Hello","world","java"};
String[] str2 = {"Veriable","syntax","interator"};
String[] newStr = new String[str1.length+str2.length];
//newStr = str1;数组是引用类型
for(int x=0;x<str1.length;x++){
newStr[x] = str1[x];
}
for(int y=0;y<str2.length;y++){
newStr[str1.length+y]=str2[y];
}
for(int y=0;y<newStr.length;y++){
System.out.println(newStr[y] + " ");
}⑤ Java数组拼接字符串
1、代码如下
publicstaticvoidmain(String[]args){
Stringstr[]={"a","b","c","d","e","f","g","h","i"};
System.out.println("字符串数组长度:"+str.length);
System.out.println("拼接后的字符串长度:"+method(str).length());
System.out.println("拼接后的字符串:"+method(str));
}
publicstaticStringmethod(String[]str){
Stringrs="";
for(Strings:str){
rs+=s;
}
returnrs;
}
2、效果如图
⑥ Java怎么实现两个数组的拼接
我已经写完代码。
importjava.util.ArrayList;
importjava.util.List;
/**
*
*开发公司:SOJSON在线工具
*版权所有:©www.sojson.com
*博客地址:http://www.sojson.com/blog/
*<p>
*
*注释写这里
*
*<p>
*
*区分责任人日期说明<br/>
*创建周柏成2017年4月2日<br/>
*
*@authorzhou-cheng
*@[email protected]
*@version1.0,2017年4月2日<br/>
*
*/
publicclassTest{
publicstaticvoidmain(String[]args){
//定义2个数组
int[]array1={1,2,3,4};
int[]array2={4,5,6,7};
//2个数组的长度
intlength=array1.length+array2.length;
//开始合并
//定义好一个长度为array1+array2的长度(可以给,可以不给)
List<Integer>list=newArrayList<Integer>(length);
for(inta:array1){
list.add(a);
}
for(inta:array2){
list.add(a);
}
//把List转换成array【完成】
Integer[]newArray=list.toArray(newInteger[length]);
for(Integerelement:newArray){
//输出每一个元素
System.out.println(element);
}
}
}
⑦ JAVA怎么合并两个数组呢
int[] s ={4, 7, 2, 3, 1 ,10, 6, 5 ,9 ,8}; int[] s2 = {4,6,2,10,24,9,30,7}; int a[]=new int[s.length+s2.length]; //定义一个长度为s加s2长度的数组 System.array(s,0,a,0,s.length); //将数组s的元素复制到a中 System.array(s2,0,a,s.length,s2.length); //将数组s2的元素复制到a中 for(int i=0;i<a.length;i++) //输出新的数组元素a System.out.println(a[i]);
⑧ java字符串数组合并 怎么合并成一个数组
java字符串数组合并,可以使用array.复制方法,如下代码:
packagecom.qiu.lin.he;
importjava.text.ParseException;
importjava.util.Arrays;
publicclassCeshi{
publicstaticvoidmain(String[]args)throwsParseException{
String[]str1={"J","a","v","a","中"};
String[]str2={"如","何","把","两","个","数","组","合","并","为",
"一","个"};
intstrLen1=str1.length;//保存第一个数组长度
intstrLen2=str2.length;//保存第二个数组长度
str1=Arrays.Of(str1,strLen1+strLen2);//扩容
System.array(str2,0,str1,strLen1,strLen2);//将第二个数组与第一个数组合并
System.out.println(Arrays.toString(str1));//输出数组
}
}
运行结果如下:
⑨ JAVA怎么合并两个数组
三种字符数组合并的方法
publicstaticString[]getOneArray(){
String[]a={"0","1","2"};
String[]b={"0","1","2"};
String[]c=newString[a.length+b.length];
for(intj=0;j<a.length;++j){
c[j]=a[j];
}
for(intj=0;j<b.length;++j){
c[a.length+j]=b[j];
}
returnc;
}
publicstaticObject[]getTwoArray(){
String[]a={"0","1","2"};
String[]b={"0","1","2"};
ListaL=Arrays.asList(a);
ListbL=Arrays.asList(b);
ListresultList=newArrayList();
resultList.addAll(aL);
resultList.addAll(bL);
Object[]result=resultList.toArray();
returnresult;
}
publicstaticString[]getThreeArray(){
String[]a={"0","1","2","3"};
String[]b={"4","5","6","7","8"};
String[]c=newString[a.length+b.length];
System.array(a,0,c,0,a.length);
System.array(b,0,c,a.length,b.length);
returnc;
}
Reference:http://www.cnblogs.com/changhong/articles/2242323.html
⑩ java怎么将2个数组的数据合并
concat()方法是对字符串的操作,不是对整数或数组。
concat()用法:
String a="abc";
String b="edf";
String c=a.concat(b);
c的值为“abcdef"
数组可以用for循环合并:
public static void main(String[] args){
int a[]={1,7,9,11,13,15,17,19};
int b[]={2,4,6,8,10};
int aL=a.length;
int bL=b.length;
int lenght=aL+bL;
int[] c=new int[lenght];
for(int i=0;i<lenght;i++){
if(i<aL){//
c[i]=a[i];
}
else{
c[i]=b[i-aL];
}
}
for(int i=0;i<c.length;i++){
System.out.print(c[i]+" ");
}
}