gpt4 book ai didi

java - 在java中压缩两个数组

转载 作者:行者123 更新时间:2023-11-30 06:10:37 24 4
gpt4 key购买 nike

我想加入两个数组,但不喜欢通常的方式

{1,2,3} {4,5,6} 
{1,2,3,4,5,6}

我也想加入他们的行列

{1,2,3} {4,5,6} 
{1,4,2,5,3,6}

有什么建议吗?

我不想连接两个数组,而是压缩它们

最佳答案

This Program also work for if array size of both is not equals.. Happy to help

  public class Assignment{

public static void main(String[] args){
int [] arr1 = new int[]{1,2,3};
int [] arr2 = new int[]{4,5,6,7,8};
int [] arr3 = new int[arr1.length + arr2.length];
int biglength = 0;
if(arr1.length > arr2.length){
biglength = arr1.length;
}else{
biglength = arr2.length;
}
for(int i=0,j=0; i< biglength; i++){
if(i<arr1.length && i<arr2.length){
arr3[j++] = arr1[i];
arr3[j++] = arr2[i];
}else if(i<arr1.length){
arr3[j++] = arr1[i];
}else{
arr3[j++] = arr2[i];
}
}
for(int j= 0 ; j<arr3.length; j++){
System.out.print(arr3[j]);
}
}
}

关于java - 在java中压缩两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35643430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com