如何在Java中超过一定长度后添加数组值

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

How to add array values after exceeding a certain length in java

问题

我正在努力找出在数组值超过一定长度时添加这些值的最佳方法。该程序将允许用户将值输入数组中。一旦用户输入了数字的数量(可以是任意数量),这些数字不应超过6列,如果超过6列,则应从数组的右侧向左侧添加。请参见以下示例:

用户输入的12个数字:

arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};

应创建一个新数组,其内容如下所示:

newArrNumbers = int[] { 2, 4, 6, 8, 10, 12};

从而仅保留了6个插槽。

我不知道如何编写这段代码。我只知道可以使用类似下面的if语句来实现。我是一个初学者,希望从有经验的人那里了解解决方案。

if (arrNumbers.length > 6)
英文:

I am trying to work out the best way to add array values when they exceed a certain length. The program will allow a user to input values into the array. Once the user enters the amount of numbers (can be as much as they want). The numbers should not exceed 6 columns, and if they do they should be added from right to left of the array. See below

The 12 numbers inputted by the user:

arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};

A new array should be made which will look like this:

newArrNumbers = int [2, 4, 6, 8, 10, 12]

Resulting in only 6 slots.

I have no idea on how to code this. I only know that this could be possible with an if statement like below. I am a beginner and would like to know a solution from someone experienced.

if (arrNumbers.length > 6)

答案1

得分: 1

你可以轻松迭代遍历数组的其余部分。

int sum = 0;
for (int i = 6; i < arrNumbers.length; i++) {
    sum += arrNumbers[i];
}
arrNumbers[5] = sum;
英文:

You can easily iterate through the rest of Array.

int sum=0;
for(int i=6;i&lt;arrNumbers.length;i++){
sum+=arrNumbers[i];
}
arrNumbers[5]=sum;

</details>



# 答案2
**得分**: 1

我认为你正在寻找的是这个:

``` java 
int[] arrNumbers = new int[] {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
int[] res = new int[6];

// 遍历初始数组并进行分块。根据要求,每块为6个元素。
for (int i = 0; i < arrNumbers.length; i += 6) {
    // 创建包含前6个数字的子数组。
	int[] ints = Arrays.copyOfRange(arrNumbers, i, Math.min(arrNumbers.length, i + 6));
    // 将第一个块添加到结果数组中。重复直到完成。
	for (int j = 0; j < ints.length; j++) {
		res[j] += ints[j];
	}
}

结果:res: {2, 4, 6, 8, 10, 12}

这适用于arrNumbers变量的任何数组大小。

英文:

I think what you are looking for is this

int[] arrNumbers = new int[] {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
int[] res = new int[6];

// iterate over the initial array and chunk it. 6 will be our chunk per requirements.
for (int i = 0; i &lt; arrNumbers.length; i += 6) {
    // create a sub array for the first 6 numbers.
	int[] ints = Arrays.copyOfRange(arrNumbers, i, Math.min(arrNumbers.length, i + 6));
    // add the first chunk to our resulting array. Repeat until needed.
	for (int j = 0; j &lt; ints.length; j++) {
		res[j] += ints[j];
	}
}

Result: res: {2, 4, 6, 8, 10, 12}

This will work for any array size for the arrNumbers variable.

答案3

得分: 1

一个更加简洁高效的答案:

int[] arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
int[] newArrNumbers = new int[6];

for(int i = 0; i < arrNumbers.length; i++){
    newArrNumbers[i % 6] += arrNumbers[i];
}

输出: `[2, 4, 6, 8, 10, 12]`
英文:

A more concise and efficient answer:

    int[] arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
    int[] newArrNumbers = new int[6];

    for(int i  = 0; i &lt; arrNumbers.length ; i++){
        newArrNumbers[i % 6] += arrNumbers[i];
    }

output: [2, 4, 6, 8, 10, 12]

huangapple
  • 本文由 发表于 2020年9月1日 17:11:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63684697.html
匿名

发表评论

匿名网友

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

确定