The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with multi dimensional array

2 replies on 1 page. Most recent reply: Mar 15, 2007 2:38 AM by Rich Liu

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Yuko Takasaki

Posts: 1
Nickname: takasaki
Registered: Mar, 2007

Help with multi dimensional array Posted: Mar 7, 2007 11:43 PM
Reply to this message Reply
Advertisement
I have a space deleminited text file I need to read in and store in a multi dimensional array...
ex.
toothpaste shampoo chocolate
book pencil
paper eraser book shampoo

I need to be able to read in the file and store it in a multi dim array. I read in each line and store
it in a string and then use the split function to parse it... how do I assign it to an array.
Any help would be appreciated.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help with multi dimensional array Posted: Mar 8, 2007 2:49 AM
Reply to this message Reply
If you split the line, then you get a array of Strings.

String[] lineContene = line.split(...)

You could now just create an array like this:

String[][] content = new String[nLines][nColumn];

But you a) don't know how many lines you have and you b) don't know what's the maximum word count is. As a matter of fact, in Java every row in this array could have a different number of columns, but that would cause some problems later.

In my opinion you should start with an Vector.
    Vector<String[]> contentTmp = new Vector<String[]>();
//then you read the file and add every array of words to the list.
    while (...) {//while reading from the file get's a valid line
        contentTmp.add(line.split(...));
    }
    //Now we calculate the maximum word count
    int nWords = 0;
    for (String[] lineContent : contentTmp)
        nWords = Math.max(nWords, lineContent.length);
    String[][] content = new String[contentTmp.size(), nWords);
     //Now assign the words
    for (int lineNr = 0; lineNr < content.length; lineNr++) {
        for (int wordNr = 0; wordNr < contentTmp.get(lineNr).length; wordNr++) {
            content[linrNr][wordNr] = contentTmp.get(lineNr)[wordNr]
        }
    }


I may have made some errors writing this, but it should lead you the right direction.

Rich Liu

Posts: 1
Nickname: richl74
Registered: Mar, 2007

Re: Help with multi dimensional array Posted: Mar 15, 2007 2:38 AM
Reply to this message Reply
        String data = "toothpaste shampoo chocolate\n" + "book pencil\n" + "paper eraser book shampoo";
        String[] lines = data.split("\n");
        LinkedList<String[]> linkedList = new LinkedList<String[]>();
        int maxElementNum = 0;
        for(int i = 0; i < lines.length; i++) {
            String[] elements = lines[i].split(" ");
            if(maxElementNum < elements.length) {
                maxElementNum = elements.length;
            }
            linkedList.add(elements);
        }
        String[][] output = new String[linkedList.size()][maxElementNum];
        for(int i = 0; i < linkedList.size(); i++) {
            output[i] = linkedList.get(i);
        }
        System.out.println("This should return toothpaste: " + output[0][0]);
        System.out.println("This should return shampoo: " + output[0][1]);
        System.out.println("This should return book: " + output[1][0]);


Not the most beautiful of code, but this should do the job.

Flat View: This topic has 2 replies on 1 page
Topic: Help with multi dimensional array Previous Topic   Next Topic Topic: STUPID JAVA QUESTION OF THE DAY!!!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use