java - 如何将一个数组分成三个不同的部分?

标签 java arrays

好的,大家好,我对 Java 编程还很陌生,而且我在数组方面确实遇到了麻烦。我的程序应该读取一个 .txt 文件,其中每行包含 3 个数字(这是一个关于卡路里的问题,第一个数字表示早餐,第二个数字表示午餐,第三个数字表示晚餐)

所以我创建了一个 Array.list (卡路里),因为它将从长度未知的文件中读取。到目前为止,它现在有效,我将值放入一个数组中,但我想将该数组拆分为三个单维数组。早餐值的数组,午餐值的另一个数组,晚餐值的最后一个数组。

我的问题是我只是不知道如何划分主数组的长度来为其他 3 个不同的数组分配大小。 (我尝试了类似 array.length/3 的方法,但它给了我一个 IndexOutOfBounds 错误)我知道它非常困惑,一切:(但我几乎不明白这一点,如果你至少能给我一个想法,我将非常感激!

import java.util.*; 
import java.util.ArrayList;
import java.io.*;

public class lab {

public static void main(String[] args) {
    readData("ARRAYLAB1.txt");   //read file arraylab1.txt
}
static void readData(String filename) {
    try {

        List<Integer> calories = new ArrayList<Integer>();  
        // Defining an integer Array List

        Scanner myfile = new Scanner(new FileReader("ARRAYLAB1.txt")); 
        // Reading file using Scanner

        while (myfile.hasNext()) {     
            calories.add(myfile.nextInt());   // Read file content using a while loop
            } 

        int[] array = new int[calories.size()];    //Pass the array list to an array
        for(int i = 0; i < calories.size(); i++) 
            array[i] = calories.get(i);


       int size = array.length / 3;  //This didn't work 

       int[] breakfast = new int[size];   <---  index out of bounds error 
       int[] lunch = new int[size];
       int[] dinner = new int[size]; 


       //the rest just assigns each value to their respective array 
        int counter = 1;
        int j = 0;
        int k = 0;
        int x = 0;
        for (int i = 0; i < array.length; i++) {
            if (counter == 1) {
                breakfast[j] = array[i]; 
                counter++;
                j++;
                continue;
                }
            if (counter == 2) {
                lunch[k] = array[i]; 
                counter++;
                k++;
                continue;
            }
            if (counter == 3) {
                dinner[x] = array[i]; 
                counter = 1; 
                x++;
                continue;
            }
        }


        myfile.close(); // close the file


    }  catch (Exception e) {     // Defined it just in the case of error
        e.printStackTrace();   
    }
  } 
}

最佳答案

阿奇特一语中的。如果您没有精确的三倍数,则当您将值分配给(早餐/午餐/晚餐)数组时,实际上会发生索引越界。为了避免这种情况,您可以使用称为数组列表的动态大小的数组(就像用于卡路里一样)。并避免除以 3 的问题。

代码变为:

List<Integer> calories = new ArrayList<Integer>();  

Scanner myfile = new Scanner(new FileReader("ARRAYLAB1.txt")); 

while (myfile.hasNext()) {     
    calories.add(myfile.nextInt());   // Read file content using a while loop
} 

List<Integer> breakfast = new ArrayList<Integer>();
List<Integer> lunch = new ArrayList<Integer>();
List<Integer> dinner = new ArrayList<Integer>();

//the rest just assigns each value to their respective array 
int counter = 1;
for (int i = 0; i < calories.size(i); i++) {
    if (counter == 1) {
        breakfast.add(calories.get(i)); 
        counter++;
        continue;
    }
    if (counter == 2) {
        lunch.add(calories.get(i)); 
        counter++;
        continue;
    }
    if (counter == 3) {
        dinner.add(calories.get(i)); 
        counter = 1; 
        continue;
    }
}

关于java - 如何将一个数组分成三个不同的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18754288/

相关文章:

java - EmailSender 读取响应异常

java - Java 中的图像 slider - Swing

java - 在java中创建一个带有2个键(索引和键)的映射

java - 在保持非阻塞的情况下有序执行许多 CompletableFuture.allof()

javascript - 访问对象中数组的值

javascript对象解析并创建新的对象数组

javascript - 如何通过按钮单击事件更改javascript中的数组所有值?

java - Maven 拉取旧版本的传递依赖

ios - Firebase 查询未触发以调用它们

java - 查找数组中最小元素的索引 (Java)