Java:使用第一个数组的前3个整数,然后是第二个数组的3个整数,将2个数组组合成第三个数组

时间:2018-10-01 21:28:27

标签: java arrays netbeans

我有3个数组A,B和C。A和B的大小为6,C的大小为12。我必须将A和B合并为C,但要使用A的前三个整数,然后使用第一个整数B的3个整数,然后是A的后3个整数,最后是B的最后3个整数。例如:int A [] = {1,2,3,7,8,9},B [] = {4, 5,6,10,11,12} 然后用C [] = {1,2,3,4,5,6,7,8,9,10,11,12}填充C

到目前为止,这是我的代码:

   public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] A = new int[6];
    int[] B = new int[6];
    int[] C = new int[12];

    int count = 0;

    for (int i = 0; i < 6; i++) {
        System.out.println("Array A: ");
        A[i]=sc.nextInt();
        System.out.println("Array B: ");
        B[i]=sc.nextInt();
    }

    System.out.println("");
    System.out.print("|");
    for (int i = 0; i < 6; i++) {
        System.out.print(A[i]+"|");
    }
    System.out.println("");
    System.out.print("|");
    for (int i = 0; i < 6; i++) {
        System.out.print(B[i]+"|");
    }

    while(count < 3){
    count++;
    {

我真的迷失了这个

3 个答案:

答案 0 :(得分:1)

这是一个相对简单的问题,可以通过几种方法解决。因为这是您提供的静态示例,所以最简单的操作是

    c[0] = a[0];
    c[1] = a[1];
    c[2] = a[2];
    c[3] = b[0];
         .
         .
         .
    c[11] = b[5]

这不能很好地扩展,并且很难维护,但是从技术上讲,您可以做到。

接下来,我将仅研究循环。在这篇文章的上一个版本中,我没有看到它们,因为我看到了它们的其他答复,但是由于我认为可以改进它们,因此添加了它们:

    //  Using loops     
    final int[] c = new int[12];
    for (int i = 0; i < 3; ++i) {
        c[i] = a[i];
        c[i + 3] = b[i];
        c[i + 6] = a[i + 3];
        c[i + 9] = b[i + 3];
    }

这也是非常简单和有效的。我们只需要进行3次循环迭代即可覆盖数组的两个部分,并使用偏移量来避免为每个部分制作多个for循环。这种简单的知识来自经验,因此,我建议通过更多示例进行研究。

接下来,我们将介绍一些更有趣的选项。如果您不熟悉Java API,我建议您首先搜索各种内容,例如“将Java复制数组的一部分”或“将Java插入数组插入另一个数组”。

此类搜索导致诸如以下的帖子

Java copy section of array

Insert array into another array

How to use subList()

从那里,您可以构建一个合理定义的答案,例如:

    //  Using System::arraycopy<T>(T[], int, T[], int, int), where T is the array-type.
    final int[] c = new int[12];
    System.arraycopy(a, 0, c, 0, 3);
    System.arraycopy(b, 0, c, 3, 3);
    System.arraycopy(a, 3, c, 6, 3);
    System.arraycopy(b, 3, c, 9, 3);

这可能足以满足您的大多数需求。

但是,通过该过程,我学到了另一种使用流的方法!

    // Using java.util.stream
    final List<Integer> l_a = Arrays.stream(a).boxed().collect(Collectors.toList());
    final List<Integer> l_b = Arrays.stream(b).boxed().collect(Collectors.toList());

    final List<Integer> l_c = new ArrayList<Integer>() {{
        addAll(l_a.subList(0, 3));
        addAll(l_b.subList(0, 3));
        addAll(l_a.subList(3, 6));
        addAll(l_b.subList(3, 6));
    }};

    final int[] c = l_c.stream().mapToInt(i -> i).toArray();

尽管最后一个例子可能不如第二个例子那么优雅,但是通过研究问题和潜在解决方案的过程已经教会了我一些我现在可以随身携带的东西。

希望这会有所帮助!

答案 1 :(得分:0)

int[] A = new int[]{1,2,3,7,8,9};
int[] B = new int[]{4,5,6,10,11,12};
int[] C = new int[12];

int aIndex= 0;
int bIndex= 0;
for(int i=0; i<C.length; i++) {
    if((i >= 0 && i < 3) || (i >= 6 && i < 9))
        C[i] = A[aIndex++];
    else
        C[i] = B[bIndex++];
}

答案 2 :(得分:0)

下面是一些示例代码,可以解决您的问题:

int[] a = {1,2,3,7,8,9};
int[] b =  {4,5,6,10,11,12};
int[] c = new int[12];
for(int x = 0; x < c.length; x++) {
    if(x <= 2) {
       c[x] = a[x];
       }
    else if(x >= 3 && x<=5) {
            c[x] = b[x-3];
        }
    else if (x >= 6 && x <= 8) {
        c[x] = a[x -3];
        }
    else if(x>=6 && x<=11) {
        c[x] = b[x -6];

        }
    }
System.out.println(Arrays.toString(c));

只要x <= 2,我们就可以遍历数组,以获得a数组的前三个索引。然后,只要x为> = 3 && <= 5,我们就可以迭代b数组的前三个索引。然后,我们可以迭代a的最后三个值,然后是b的最后三个值。我们必须确保执行“ b [x-6]”,因为x显然将大于b数组中具有的索引数。输出如下:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]