在一个for循环java中添加两个变量。

时间:2021-11-05 14:21:04

Given 2 int arrays, each length 2, return a new array of length 4 containing all their elements. Ex:

给定两个int数组,每个长度为2,返回一个包含所有元素的长度为4的新数组。例:

plusTwo({1, 2}, {3, 4}) → {1, 2, 3, 4}

It is a sample question but I want do some extra practice. If the question did not specify the length of the two arrays. Then I wrote the code as :

这是个例题,但我想多做些练习。如果问题没有指定这两个数组的长度。然后我把代码写成:

public int[] plusTwo(int[] a, int[] b) {
  int[] c=new int[a.length+b.length];
  for(int i=0;i<a.length;i++){
      c[i]=a[i];
      for(int j=a.length;j<a.length+b.length;j++){
          for(int m=0;m<b.length;m++){
              c[j]=b[m];
          }
      }
  }
  return c;
}

My return is {1,2,4,4} I can not point out my mistake.:(

我的返回是{1,2,4,4}我不能指出我的错误。

1 个解决方案

#1


0  

You need to write down what you want to archive and after that try to code this.

您需要写下您想要归档的内容,然后尝试对其进行编码。

  • So first you need an array with size a+b (this is correct)
  • 首先需要一个大小为a+b的数组(正确)
  • Now you want to copy everything from a into the beginning of c (your first loop this is correct)
  • 现在你想要复制所有东西从a到c开头(你的第一个循环是正确的)
  • Now you want to copy everything from b to c but beginning where you stopped copying from a (this is where you code becomes to complex with nested loops)
  • 现在你想要复制从b到c的所有东西,但是从你停止复制的地方开始(这是你的代码变得复杂,嵌套循环的地方)

So your code should look like this:

所以你的代码应该是这样的:

int[] c= new int[a.length+b.length];
for(int i =0; i<a.length;i++) {
   c[i]=a[i];      
}

for(int i = 0; i<b.length;i++) {
   c[i+a.length]=b[i]; 
}

Or be fancy and use System.arraycopy which explains the steps needed a little bit better imho:

或者是花里胡哨和使用系统。arraycopy解释了需要更好一点的步骤

int[] c= new int[a.length+b.length];
System.arraycopy(a, 0, c, 0, a.length); //(copy a[0]-a[a.length-1] to c[0]-c[a.length-1]
System.arraycopy(b, 0, c, a.length, b.length); //copy b[0]-b[b.length-1] to c[a.length]-c[c.length-1]

#1


0  

You need to write down what you want to archive and after that try to code this.

您需要写下您想要归档的内容,然后尝试对其进行编码。

  • So first you need an array with size a+b (this is correct)
  • 首先需要一个大小为a+b的数组(正确)
  • Now you want to copy everything from a into the beginning of c (your first loop this is correct)
  • 现在你想要复制所有东西从a到c开头(你的第一个循环是正确的)
  • Now you want to copy everything from b to c but beginning where you stopped copying from a (this is where you code becomes to complex with nested loops)
  • 现在你想要复制从b到c的所有东西,但是从你停止复制的地方开始(这是你的代码变得复杂,嵌套循环的地方)

So your code should look like this:

所以你的代码应该是这样的:

int[] c= new int[a.length+b.length];
for(int i =0; i<a.length;i++) {
   c[i]=a[i];      
}

for(int i = 0; i<b.length;i++) {
   c[i+a.length]=b[i]; 
}

Or be fancy and use System.arraycopy which explains the steps needed a little bit better imho:

或者是花里胡哨和使用系统。arraycopy解释了需要更好一点的步骤

int[] c= new int[a.length+b.length];
System.arraycopy(a, 0, c, 0, a.length); //(copy a[0]-a[a.length-1] to c[0]-c[a.length-1]
System.arraycopy(b, 0, c, a.length, b.length); //copy b[0]-b[b.length-1] to c[a.length]-c[c.length-1]