Results 1 to 9 of 9
  1. #1
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default file input: array of integers

    Hey guys,

    I want to read an array of integers from a txt file, but before i can read the array i have to initialize the array of integers. How can i do this that the array is not to big? I've tried so far with the lenght( ) method of my file object, but my array was too big. Has anyone got a solution?

    Thanks, Hannes

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Either read the file twice, the first time to determine exactly how many integers it contains and the second time to actually read the data once the array has been created. Or use a collection that grows as needed: List<Integer> has an add() method and, unlike an array, it just gets as big as needed.

    I know you said "I want to read an array..." but consider not doing that. Perhaps you can do whatever processing you need without holding all of the integers in memory at the same time.

  3. #3
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    It is for drawing a curve, so i have to hold the array in memory.

    I've an array of 20.000 integers. One integer takes 4 bytes, that's 80.000 bytes totally. And i want 100 arrays: 100*80000=8.000.000 bytes= 8.000kb=8mb only. That's not that much.
    Last edited by hannes; 01-27-2010 at 10:40 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Why do you have to hold it in memory though?

  5. #5
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    you can modify the arrays, change colors and so on...

    Hannes

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    OK.
    Then do one of the suggestions above, either two read throughs, one to find the length and the other to actually read the data...or List<Integer> (or List<Integer>[100]). Note that List<Integer> will require more memory, but that should still not prove a problem.

  7. #7
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    I agree with Tolls.

    If you use the List<Integer>, you can use List.toArray() once the List is filled to get an Integer array. The List can then be discarded and you can continue with the array. This could prove handy when performance or memory are important.

  8. #8
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    Great, thanks guys!

    Hannes

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

Similar Threads

  1. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 08:58 AM
  2. How to write integers into file?
    By dj kourampies in forum New To Java
    Replies: 1
    Last Post: 08-20-2009, 04:52 AM
  3. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 PM
  4. Replies: 7
    Last Post: 04-28-2008, 04:28 AM
  5. Replies: 2
    Last Post: 04-16-2008, 12:35 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
  •