如何在循环中将数组元素从一个复制到另一个?

huangapple go评论103阅读模式
英文:

How can i copy array elements from one to another in loop?

问题

我是Java的新手,但我已经学习了一段时间的C,有一个非常愚蠢的问题,我自己无法回答。我想从一个数组中复制元素(在数字10之后出现的元素)到另一个数组中(该数组将包含来自第一个数组中的数字10之后的数字)。我大致知道如何做,但我找不到适合我特定情况的确切答案。也许我的代码不正确,我应该重写它,但我想尝试坚持下去。我知道可能有许多答案可以解决这个问题,但我试图避免查看它们(因为我想自己开始解决它们)并创建自己的解决方案。

我的代码没有复制第一个数组中的每个元素(位置),而是将最后一个元素复制到第二个数组中的所有位置。

我希望我提出的问题清晰,并且我的代码是可读的。提前感谢大家。和平与爱!

所以,这是代码:

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int n; // 第一个数组的大小
        int flag=0;

        System.out.println("输入数组大小:");
        n=in.nextInt();

        int [] arr;
        arr=new int[n];
        
        System.out.println("输入元素:");
        for(int i=0;i<n;i++){
            arr[i]=in.nextInt();
        }
        System.out.println("您的数组是:"+Arrays.toString(arr));

        for(int i=0;i<n;i++){

            if(arr[i]==10)
                break;
                 flag = arr[i]; // 在这里我定位到数字10的位置
        }

        int size=n-flag; // 这是一个有趣的部分,我在“计算”第二个数组的大小)

        int[] new_arr;
        new_arr= new int[size];

        for(int i=flag;i<n;i++){ // 我认为问题可能出在这里!
          for(int j=0;j<size;j++){
              new_arr[j]=arr[i];
          }
        }
        System.out.println("让我们试试:"+Arrays.toString(new_arr));
    }
}

注意:上面的代码中有一些HTML实体字符(如&quot;),这些应该被替换为正常的引号字符(")。此外,代码中的一些注释也可以进行翻译和修复,以确保代码的清晰性。

英文:

I am new to Java, but I've studied C for sometime and have very srupid question which i can't answer on my own. I want to copy elements from one array( elements that come after number 10) to another( which will contain numbers after 10 from the first array) I have general idea how to make it, but i can't find exact answer for my specific case. Maybe my code isn't right and i should rewrite it, but i want to try to stick with it. I know there must be many answers for that exercise, but i am trying to avoid looking at them( because i want to start resolving them on my own) and create my own solutions.

My code does not copy each element( position) from 1st array, it copies the last element to all positions in the second array.

I hope i made my question clear and my code is readable. Thank you all in advance. Peace and love!

So, here comes the code

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n; // Size of the first array
int flag=0;
System.out.println(&quot;Enter array size &quot;);
n=in.nextInt();
int [] arr;
arr=new int[n];
System.out.println(&quot;Enter elements &quot;);
for(int i=0;i&lt;n;i++){
arr[i]=in.nextInt();
}
System.out.println(&quot;Your array is &quot;+Arrays.toString(arr));
for(int i=0;i&lt;n;i++){
if(arr[i]==10)
break;
flag = arr[i]; // HERE I AM LOCATING 10&#39;S POSITION
}
int size=n-flag; // This is a funny part, i am &quot;calculating&quot;  for the second array&#39;s size) 
int[] new_arr;
new_arr= new int[size];
for(int i=flag;i&lt;n;i++){ // I think here must be the problem! 
for(int j=0;j&lt;size;j++){
new_arr[j]=arr[i];
}
}
System.out.println(&quot;Lets try &quot;+Arrays.toString(new_arr));
}
}`
</details>
# 答案1
**得分**: 4
你可以只使用```System.arraycopy(source_arr, sourcePos, dest_arr, destPos, len);```。这是来自java.lang.System的一个方法。
<details>
<summary>英文:</summary>
you can just use  ```
System.arraycopy(source_arr, sourcePos, dest_arr, destPos, len);```
Its a method from java.lang.System
</details>
# 答案2
**得分**: 0
"首先,位置10就是位置10,无需查找位置10的值...
也不需要两个循环,我们只需从位置10开始迭代所有元素并复制它们。
所以我们只需这样做:
```java
int start_pos=10;
int size=n-start_pos;
int[] new_arr = new int[size];
for(int i=start_pos;i<n;i++){ 
new_arr[i-start_pos]=arr[i];
}

而且,正如其他人所说,更一般地,你可以使用System.arraycopy(),甚至更好的方法是使用列表并调用subList方法。"

英文:

Well first, position 10 is position 10, no need to find the value at position 10...

There also no need for 2 loops, we just iterate on all the elements starting from 10 and copy them.

So we just do

int start_pos=10;
int size=n-start_pos;
int[] new_arr = new int[size];
for(int i=start_pos;i&lt;n;i++){ 
new_arr[i-start_pos]=arr[i];
}
}

And yes like other said it, more generally you would use System.arraycopy() or even b better work with lists and call the subList method.

huangapple
  • 本文由 发表于 2020年7月22日 00:10:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63018580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定