开发者

Best approach for storing three teirs of data - multi-d array?

开发者 https://www.devze.com 2023-02-22 15:41 出处:网络
I\'m operating on a file that has this kind of format: LAWS303 RHLT11084AITKENWU LAWS314 RHLT3152PARADZA VISSER

I'm operating on a file that has this kind of format:

LAWS303 RHLT1   10  84  AITKEN  WU  
LAWS314 RHLT3   15  2   PARADZA VISSER  
LAWS329 EALT006 6   62  AITKEN  WILSON  
LAWS334 HMLT105 2   43  ANDREW  INKSTER 
LAWS334 HMLT206 2   62  JUL开发者_开发知识库IAN  YOUNG   
LAWS340 RHLT1   11  87  AL  YANG    

The goal of this program is that for each day (the third column) of the month, each course code (first column) should be printed along with the total number of students attending (fourth column) for the course on that day. From my thinkering, this involves either reading the file a lot of times (ew!) or loading the three salient values (day, course, headcount) into some sort of array and operating on that instead. Despite being fairly familiar with what a multi dimensional array is, this one has repeatedly caused my head to implode. I've got the pseudo code for this program written out in front of me and my mind draws a blank when it comes to the line that defines the array.

The dayOfMonth can remain a string because it'll only be compared to another string. The courseCode obviously needs to be a string, too. However, the headCount ideally would be numeric; it'll be added to as each line of the file is processed. The relationship between the three is basically that there can be many courseCodes per dayOfMonth, but only one headCount per courseCode as I'll be adding to it as I read it all into the array.

So, in derpspeak, this is how it should roughly look:

{String dayOfMonth = {{String courseCode}, {int headCount}}}

The two issues I have here, are... a) that I'm not sure how to actually code this kind of funky array in there and b) as I can't really wrap my brain around it to begin with, there's a really good chance that I've essentially just designed something completely wrong for what I need. Or impossible. Both?

For example, the array will start off empty. I'd want to add a dayOfMonth, courseCode and headCount to start it off. But I couldn't just go array.add(dayOfMonth) because it's expecting an array, leading me to suspect I should be using something else. Argh!

Oh god my brain.


This looks like homework, so my answer will consist of hints.

Hint #1 - there are some entities in those rows. Work out what they are and write a class for each one.

Hint #2 - Use List types not arrays. Arrays have to be preallocated with the right number of elements.

Hint #3 - Use Map types (e.g. HashMap or TreeMap) to represent mappings from one kind of thing to another kind of thing.


If you want to store and retrieve the values then use @Stephan C's input. Here is code snippet to print values using sysout. You can modify to hold the values as you wish.

BufferedReader reader = new BufferedReader(new FileReader("< your file here >"));
        String string = reader.readLine();
        while (string != null) {
            StringTokenizer tokenizer = new StringTokenizer(string);
            String print = "";

            if (tokenizer.countTokens() > 4) {
                print = tokenizer.nextToken();
                tokenizer.nextToken();
                print = tokenizer.nextToken() + " " + print;
                print = print + " " + tokenizer.nextToken();
            }
            System.out.println(print);
            string = reader.readLine();
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号