Results 1 to 9 of 9
Thread: file input: array of integers
- 01-27-2010, 09:46 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
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
- 01-27-2010, 09:56 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 01-27-2010, 10:35 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
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.
- 01-27-2010, 12:52 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Why do you have to hold it in memory though?
- 01-27-2010, 02:39 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
you can modify the arrays, change colors and so on...
Hannes
- 01-27-2010, 03:08 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-27-2010, 03:16 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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.
- 01-27-2010, 03:44 PM #8
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
Great, thanks guys!
Hannes
- 01-27-2010, 03:44 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Error if array contains duplicate integers
By lithium002 in forum New To JavaReplies: 4Last Post: 12-05-2009, 08:58 AM -
How to write integers into file?
By dj kourampies in forum New To JavaReplies: 1Last Post: 08-20-2009, 04:52 AM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM -
Help with Code! Display Array of Strings and Integers - Sorted
By luvjoynt in forum New To JavaReplies: 7Last Post: 04-28-2008, 04:28 AM -
return Set .toArray(); method as an array of integers
By maxim in forum New To JavaReplies: 2Last Post: 04-16-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks