Results 1 to 5 of 5
  1. #1
    TinoJF is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default Load file to data structure

    I'm rewriting an application from vb.net to Java. It loads a file to a data structure like this:
    Java Code:
    Structure strucData
          Dim intArray() as Integer
          Dim dateArray() as Date
    End Structure
    '//and in another place:
    Dim myData() as strucData
    First I transcribed the Structure statement as a class, then I opted for a bi-dimensional array like this:
    Java Code:
    int intArray[][];
    Date dateArray[][];
    
    private void loadData() {
        File theFile = new File(filePath);
        try {
            DataInputStream dis = new DataInputStream(new BufferedInputStream(new       FileInputStream(fitxer)));
            // Of course the value of nouSi is grater of necessary
            // I use and resize the first dimension in other place of the program
            intArray = new int[1][nouSi];
            dateArray = new Date[1][nouSi];
            int cursor = 0;
            try {
                while (true) {
                    dateArray[0][cursor] = new Date(dis.readLong());
                    intArray[0][cursor] = dis.readInt();
                    cursor++;
            } catch (EOFException e) { }
        } catch (IOException e) { }
    }
    in both cases it works, but in vb.net the execution for the same file takes about 2 seconds but in Java takes few minutes. I do something wrong or there are other better choice that I don't know?
    Also I seen that it consumes much physical memory and nothing of swap memory, but I'm also novice in Linux and perhaps it's ok.

    Thanks!

  2. #2
    biniG is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default

    hum . . . .

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Java Code:
    while (true) {
                    dateArray[0][cursor] = new Date(dis.readLong());
                    intArray[0][cursor] = dis.readInt();
                    cursor++;
            }
    Well, I'm not sure how it worked ... with the above you have an infinite loop, which might explain the "few minutes" execution and "consumes much physical memory" that you're seeing. You need some sort of condition to get out of that loop.
    Also, you are using a 2d array to save only 1D information. Are these arrays used later on so that more elements are assigned to them?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    TinoJF is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    When the file ends, throws an EOFException that is handled by
    Java Code:
    catch (EOFException e) { }
    and this is how get out of the loop.

    Yes, these arrays are used later, for example I load a file in intArray[0] another file in intArray[1], etc.

  5. #5
    TinoJF is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    perhaps this is more comprehensible.
    Java Code:
    int intArray[][];
    Date dateArray[][];
    
    private void loadData() {
        File theFile = new File(filePath);
        try {
            DataInputStream dis = new DataInputStream(new BufferedInputStream(new       FileInputStream(theFile)));
            // The value of nouSi is grater of necessary
            intArray = new int[1][nouSi];
            dateArray = new Date[1][nouSi];
            for (int i = 0; i<nouSi; i++) {
                 dateArray[0][i] = new Date(dis.readLong());
                 intArray[0][i] = dis.readInt();
            }
        }
        catch (IOException e) { }
        dis.close();
    }

Similar Threads

  1. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM
  2. data file structure
    By Nicholas Jordan in forum Advanced Java
    Replies: 2
    Last Post: 01-07-2009, 04:16 AM
  3. Stack data structure in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:34 PM
  4. Queue data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:35 PM
  5. data structure code
    By vgvt in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •