从文件加载一个二维数组 [英] Loading a 2D Array from a File

查看:141
本文介绍了从文件加载一个二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一点点帮助。在节目中我的工作,我们都应该在一个文本文件名为prog2.dat其内容是重新present 10名员工工作时间阅读。下面是该文件的样子。

need a little help. In the program I'm working on we are supposed to read in a text file titled prog2.dat whose contents are to represent the hours worked by 10 employees. Here is what the file looks like.

10
8  4  7  3  8  6  3 
2  7  6  3  5  2  1  
1  2  3  8  6  4  4 
3  2  8  8  8  5  1 
4  3  2  1  3  8  6 
8  5  6  7  5  5  4 
1  8  7  4  2  8  6 
1  5  4  6  5  3  3
4  3  2  1  2  3  4
1  8  7  6  5  6  5

当在读取该文件,它被存储在一个二维数组,这就是我在遇到麻烦。我与二维数组的经验是非常小,我以前从来没有加载到从文件。

When the file is read in, it's to be stored in a 2D array, this is where I'm having trouble. My experience with 2D arrays is very minimal and I've never loaded one from a file before.

这不是程序的结束,但一切是的东西,我知道怎么做不应该是一个问题,它刚开到这一点。

That's not the end of the program, but everything else is stuff that I know how to do so shouldn't be a problem, it's just getting to that point.

下面是code我有这么远。<​​/ P>

Here is the code I have so far.

import java.util.Scanner;

public class Program2 {
    public static void main(String[] args) {
        int[][] hoursArray = new int[i][j];
        int employeeNum;
        int i = 0;
        int j = 0;
        java.io.File file = new java.io.File("../instr/prog2.dat");
        Scanner fin = new Scanner(file);
             employeeNum = fin.nextInt();
             while (fin.hasNextLine && i < hoursArray.length) {
                hoursArray[i] = fin.nextInt();
                i++; 
            }

我知道这是不完整的,但是这是我迄今。任何帮助将大大AP preciated。

I know this is incomplete, but that's what I have so far. Any help would be greatly appreciated.

推荐答案

使用的java.util.List你不必担心前手知道尺寸(长度)。您可以随时转换回用List.toArray(整数[])的数组。

By using java.util.List you don't need to worry about knowing size (length) before hand. You can always convert back to an array with List.toArray(Integer[]).

但是,如果你在使用数组坚持(也许这就是你的任务要求),那么你需要知道你想要的二维数组的尺寸,然后才能开始使用它。你知道有多少员工有,作为第一行告诉你。你知道这将是为每个员工7个号码,因为它是7天的一周。 (我假设这是你的任务要求。)所以,你将不得不

But if you are insisting on using Arrays (maybe this is your assignment requirement), then you need to know about the dimension of the 2d array you want before you can start using it. You know how many employee there is, as the first line tells you. You know it is going to be 7 numbers for each employee as it is for 7 days in the week. (I am assuming this is in your assignment requirement.) So you going to have

Integer[][] hoursArray = new Integer[employeeNum][7];

然后你会依次读取每个号码。我建议你​​保持两个计数器,开斋节员工ID和d为一天。既初始化为0那么d得到由1每一个号码被读出时间后递增,直到ð等于7,则d复位到0,和EID被增加1。因此,现在的数目可以发生到阵列时阅读像这样

then you are going to read each number in turn. I suggest you keep two counters, eId for employee id, and d for day. Both initialised to 0. Then d gets incremented by 1 after each time a number is read, until d equals to 7, then d reset back to 0, and eId gets incremented by 1. So now the number can be place into the array when read like so

hoursArray[eId][d] = fin.nextInt();

当然,你仍然需要处理任何输入错误,但我现在留给你。

Of course you still need to handle any input errors, but I leave it to you now.

要了解更多关于Java中的二维数组,我建议看看这个创建二维数组

To understand more about 2d arrays in java, I suggest have a look at this Creating Two-Dimensional Array

这篇关于从文件加载一个二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆