A. java 中如何將 "字元串數組" 合並成 "一個字元串" 例如String [] ss = new String [n] 字元串數組中有n個
1.先建立一個StringBuffer對象
2.然後對你想合並的該字元串數組String [] ss遍歷,遍歷的時候,通過使用StringBuffer的append()方法,版將每個ss[i]中權的值加入到StringBuffer中
3.如果想得到String,就調用StringBuffer的toString()方法!
B. java如何把兩個二維數組合並成一個,高效的方法,希望大家給點意見
以String為例的一個示例。
import java.util.*;
public class Test {
public static void main(String[] args) {
String[][] strs1 = {{"start", "htr", "fa"},
{"543", "gfd", "gs"},
{"jh", "k", "gsk"},
{"675", "hfhgf"}};
String[][] strs2 = {{"fdsa", "432", "fds"},
{"j","hgf34"},
{"765", "gfj", "456", "jkl"},
{"lkjhl", "gfds", "hgfh", "end"}};
String[][] strs3 = unite(strs1, strs2);
for(String[] list : strs3) {
for(String s : list) {
System.out.print(s + " ");
}
System.out.println();
}
}
public static String[][] unite(String[][] os1, String[][] os2) {
List<String[]> list = new ArrayList<String[]>(Arrays.<String[]>asList(os1));
list.addAll(Arrays.<String[]>asList(os2));
return list.toArray(os1);
}
}
C. java String數組合並
String tr = "";
for (int i = 0; i < str.length; i++) {
tr = tr + ',' + str[i];
}
tr = tr + ',' + tem[4];
tr = tr + ',' + tem[1];
tr = tr + ',' + tem[0];
D. java兩個數組合並用for循環
//兩個數組
String[] str1 = {"a","b","c"};
String[] str2 = {"d","e","f"};
//創建一個要接收的數組
String[] str3= new String[str1.length+str2.length];
//先把第一個數組放進去
for(int x=0;x<str1.length;x++){
str3[x] = str1[x];
}
for(int y=0;y<str2.length;y++){
str3[str1.length+y]=str2[y];
}
for(int y=0;y<str3.length;y++){
System.out.println(str3[y] + " ");
}
如有幫助請採納(不懂請提問),可以看我主頁,歡迎來交流學習;
E. Java如何合並兩個數組
java數組合並問題
三種字元數組合並的方法
public static String[] getOneArray() {
String[] a = { "0", "1", "2" };
String[] b = { "0", "1", "2" };
String[] c = new String[a.length + b.length];
for (int j = 0; j < a.length; ++j) {
c[j] = a[j];
}
for (int j = 0; j < b.length; ++j) {
c[a.length + j] = b[j];
}
return c;
}
public static Object[] getTwoArray() {
String[] a = { "0", "1", "2" };
String[] b = { "0", "1", "2" };
List aL = Arrays.asList(a);
List bL = Arrays.asList(b);
List resultList = new ArrayList();
resultList.addAll(aL);
resultList.addAll(bL);
Object[] result = resultList.toArray();
return result;
}
public static String[] getThreeArray() {
String[] a = { "0", "1", "2", "3" };
String[] b = { "4", "5", "6", "7", "8" };
String[] c = new String[a.length + b.length];
System.array(a, 0, c, 0, a.length);
System.array(b, 0, c, a.length, b.length);
return c;
}
1.兩個字元數組合並的問題
public String[] getMergeArray(String[] al,String[] bl) {
String[] a = al;
String[] b = bl;
String[] c = new String[a.length + b.length];
System.array(a, 0, c, 0, a.length);
System.array(b, 0, c, a.length, b.length);
return c;
}
2.字元數組和整形數組合並問題
public int[] getIntArray(int[] al,String[] bl) {
int[] a = al;
String[] b = bl;
int[] ia=new int[b.length];
for(int i=0;i<b.length;i++){
ia[i]=Integer.parseInt(b[i]);
}
int[] c = new int[a.length + ia.length];
System.array(a, 0, c, 0, a.length);
System.array(ia, 0, c, a.length, ia.length);
return c;
}
F. java怎麼將2個數組的數據合並
不記得有可以合並兩個數組的方法,感覺基本只能手動了,如:
intindex=0;
intc[]=newint[a.length+b.length];
for(inti=0;i<a.length;i++){
c[index++]=a[i];
}
for(inti=0;i<b.length;i++){
c[index++]=b[i];
}