【问题标题】:Java: Combine 2 arrays into a third array using the first 3 integers of the first array then 3 integers from the second oneJava:使用第一个数组的前 3 个整数,然后使用第二个数组的 3 个整数将 2 个数组组合成第三个数组
【发布时间】:2018-10-01 21:28:26
【问题描述】:

我有 3 个数组 A、B 和 C。A 和 B 的大小为 6,C 的大小为 12。我必须将 A 和 B 组合成 C,但使用 A 的前 3 个整数,然后是第一个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++;
    {

我真的迷路了这个

【问题讨论】:

  • 从您的代码看来,您可以拥有多个for-loops。在您的第一个 for-loop 中,从 0 循环到 2,存储第一个数组中的前 3 个。在您的第二个for-loop 中,从 0 循环到 2,存储第二个数组中的前 3 个。在您的第三个for-loop 中,从 3 循环到 5,将最后一个 3 存储在第一个索引中,依此类推。
  • 首先尝试完成第一步:将A的前三个元素复制到C的前三个位置。

标签: java arrays netbeans


【解决方案1】:

这是一个相对简单的问题,可以通过几种方式解决。最简单的方法是,因为这是您给出的静态示例,因此可以执行类似的操作

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

这不能很好地扩展,并且难以维护,但在技术上可以满足您的要求。

接下来,我会简单地看一下 for 循环。在这篇文章的前一个版本中,我没有将它们包括在内,因为我看到了他们的其他回复,但后来添加了这个,因为我认为它们可以改进:

    //  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,我建议您先在 Google 上搜索各种内容,例如“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();

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

希望这会有所帮助!

【讨论】:

    【解决方案2】:
    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++];
    }
    

    【讨论】:

      【解决方案3】:

      这里有一些示例代码可以解决您的问题:

      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 = 3 &&

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

      【讨论】:

        猜你喜欢
        • 2012-06-05
        • 2012-03-31
        • 1970-01-01
        • 2016-03-08
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多