Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从数组中获取某些值并将其添加,然后将该值添加到第二个数组中?_Java_Arrays_Input - Fatal编程技术网

Java 如何从数组中获取某些值并将其添加,然后将该值添加到第二个数组中?

Java 如何从数组中获取某些值并将其添加,然后将该值添加到第二个数组中?,java,arrays,input,Java,Arrays,Input,不允许使用ARRAYLIST 嘿,伙计们!感谢您抽出时间阅读 我现在有一个数组,里面有15个数字,我需要加上点1-5的数字,然后从2-6,然后从3,7等等,一直到15。然后这些总和必须被放入一个新的数组中 有什么建议吗 代码如下 import java.io.*; import java.util.Scanner; public class Asgn7 { public static void main(String[] args) throws FileNotFoundExcepti

不允许使用ARRAYLIST

嘿,伙计们!感谢您抽出时间阅读

我现在有一个数组,里面有15个数字,我需要加上点1-5的数字,然后从2-6,然后从3,7等等,一直到15。然后这些总和必须被放入一个新的数组中

有什么建议吗

代码如下

import java.io.*;
import java.util.Scanner;

public class Asgn7
{
    public static void main(String[] args) throws FileNotFoundException
    {   
        Scanner file = new Scanner(new File("asgn7data.txt"));

        int[] array = new int[file.nextInt()];

        for (int i = 0; i < array.length; i++)
        {
            array[i] = file.nextInt();
            System.out.println(array[i]);
        }

        file.close();

    }

}

您需要的是第二个空数组,并且只需要一个正确的算法就可以实现这一点。在这里,我们只需在接下来的五个整数上循环,并将它们从第二个数组添加到同一个索引中


解决方案
Scanner Scanner=新的扫描仪(新文件(“input.txt”);
int[]inputarray=新的int[10];//因为您不一定知道实际的输入量,所以采用固定的大小,我们将像arraylist一样展开它
//首先,我们将收集输入数据
int当前_指数=0;
while(scanner.hasNextInt()){
if(inputarray.length==当前索引){
//通过创建新阵列来展开阵列
最终整数[]扩展=新整数[inputarray.length*2];
System.arraycopy(inputarray,0,扩展,0,inputarray.length);
输入阵列=扩展;
}
inputarray[current_index++]=scanner.nextInt();
}
//现在我们可以计算了
最终整数[]答案=新整数[当前指数+1-5];
for(int i=0;i
您到底想要什么?请修改您的问题以包含输入文件。我们需要看看那里的信息是如何组织的?您的问题不完整。@Raf,已添加输入文件。@YassinHajaj我有一个从文件中读取的数字数组。我现在需要添加第1-5天、第2-6天、第3-7天等的值,然后将它们除以5,然后将它们存储到另一个数组中。@JonRoy感谢您的努力,但复制粘贴问题中相同的句子并不能帮助我理解问题。你介意重新措辞吗?你为什么要说天?谢谢你,伙计!现在有意义了,我知道我需要两个数组,但这很有意义。谢谢man@JonRoy不客气Jon:)有一件事我不明白,那就是数组[counter++]=file.nextInt();这不应该在while循环中吗?这只是用文件中的数据填充第一个数组。没什么特别的,它只是一个接一个地填充索引,直到文件不再有int:)嗯,当运行代码时,如果我试图计算第一个数组中有多少个变量,它只打印11/15,我想它与.length()4有关,有没有办法解决这个问题?
1
1
4
6
4
7
11
8
10
6
10
13
6
10
15
String ints = "15 1 1 4 6 4 7 11 8 10 6 10 13 6 10 15";
Scanner file = new Scanner(ints);
int[] array = new int[file.nextInt()];
int[] newArray = new int[array.length-4];
int counter = 0;
while (file.hasNextInt()) array[counter++] = file.nextInt();
for (int i = 0 ; i < array.length - 4 ; i++){
    for (int j = 0 ; j < 5 ; j++){
        newArray[i] += array[i+j];
    }
}
System.out.println(Arrays.toString(newArray));
[16, 22, 32, 36, 40, 42, 45, 47, 45, 45, 54]
Scanner scanner = new Scanner(new File("input.txt"));
int[] inputarray = new int[10]; // You take a fixed size because you don't necessarily know the actual amount of input, we will expand this like an arraylist would
// First we will collect the input data
int current_index = 0;
while (scanner.hasNextInt()) {
    if (inputarray.length == current_index) {
        // Expand the array by creating a new one
        final int[] expanded = new int[inputarray.length * 2];
        System.arraycopy(inputarray, 0, expanded, 0, inputarray.length);
        inputarray = expanded;
    }
    inputarray[current_index++] = scanner.nextInt();
}

// now we can calculate
final int[] answers = new int[current_index + 1 - 5];
for (int i = 0; i < answers.length; i++) {
    for (int j = 0; j < 5; j++)
        answers[i] += inputarray[i + j];
}

System.out.println(Arrays.toString(answers));