本文目錄一覽:
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;ilenght;i++){
if(iaL){//
c[i]=a[i];
}
else{
c[i]=b[i-aL];
}
}
for(int i=0;ic.length;i++){
System.out.print(c[i]+” “);
}
}
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.copyOf(str1,strLen1+ strLen2);//擴容
System.arraycopy(str2, 0, str1, strLen1,strLen2 );//將第二個數組與第一個數組合併
System.out.println(Arrays.toString(str1));//輸出數組
}
}
java中怎麼合併兩個數組 簡單明了的
int[] arr1 = {1,2,3,4,11};
int[] arr2 = {6,7,8,9,10};
int newLength = arr1.length + arr2.length;
int[] arr_target = new int[newLength];
//參數:源數組,源數組起始位置,目標數組,目標數組起始位置,複製長度
System.arraycopy(arr1, 0, arr_target, 0, arr1.length);
System.arraycopy(arr2, 0, arr_target, arr1.length, arr2.length);
//輸出合併後數組
for (int i : arr_target) {
System.out.println(i);
}
//排序
Arrays.sort(arr_target);
//輸出排序數組
for (int i : arr_target) {
System.out.println(i);
}
//逆序
int[] arr_reverse = new int[newLength];
int flag = 0;
for (int i : arr_target) {
arr_reverse[newLength – flag – 1] = i;
flag++;
}
//輸出逆序數組
for (int i : arr_reverse) {
System.out.println(i);
}
數組合併不一定非得遍歷
具體的輸出題主自己再修改吧
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.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(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.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(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;ib.length;i++){
ia[i]=Integer.parseInt(b[i]);
}
int[] c = new int[a.length + ia.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(ia, 0, c, a.length, ia.length);
return c;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/303050.html