Sign in with
Sign up | Sign in
Your question

Java: Transfering 2d array into a file

Last response: in Applications
Share
January 31, 2012 1:57:56 AM

Hi,

I am somewhat new to Java, and i am working on a project at the moment. The program has a 9x9 2D char array. what the it does is take in 2 chars inputted by the user, compares them to where those chars are in the table, and then does a bunch of other stuff once is finds positions of the 2 chars in the grid.

Its working great, except in order to speed things up im considering writing that 2d char array to a binary file and then just looking up the index of the chars i need rather than searching the array.

What would be the method i would use to transfer my char array to a binary file, and then more importantly how would i call it up when i need it? (of course maintaining the integrity of the grid since the locations of the chars are extremely important)

I'm not asking for the entire code, but just some hints, snippets and other things to get me on track. My hours of google searching did not turn up anything useful.

Note: I didnt post my code because its sort of a mess right now as im trying out a lot of methods, and have a bunch of stuff commented out; it would probably end up confusing everyone lol
a b L Programming
January 31, 2012 7:35:34 AM

Writing the array to file will cause slower performance. Keep the data structure in program memory!

Have a look here for some ideas on structures. However with such a small structure and using primitives the performance or memory benefits you are going to get is going to be small!
m
0
l
January 31, 2012 12:29:13 PM

Writing to disk is always slower than doing it in memory.
m
0
l
Related resources
January 31, 2012 3:19:55 PM

Rusting In Peace said:
Writing the array to file will cause slower performance. Keep the data structure in program memory!

Have a look here for some ideas on structures. However with such a small structure and using primitives the performance or memory benefits you are going to get is going to be small!


Oh really?


I assumed writing some sort of index file would be much more efficient.my professor kept mentioning it too lol

So should I just keep my program the way iit it?
m
0
l
January 31, 2012 3:41:43 PM

If the structures are small, the operating system will actually cache the file in memory, thereby effectively undoing all the programming that you did to place it on disk.

The only time you should worry about writing to disk if for persistence sake.

I maybe unclear as to what you mean, but if you are wanting a fast way to access chars you have already found then you can store another array containing pointers to the already indexed chars. If you are wanting faster searching in general, you can use a hash map.
m
0
l
February 2, 2012 7:47:58 PM

Getting something from an array should be easy and not cause performance issues, unless you are using a recursive algorytm or something like that. The two inputed chars represent position in the array(no way to speed this up any more) or elements allready in the array? With the second you need to compare elements in the array until you find them and there is no way around this, and since the array should be in program memory, there is no way to speed it up.
I would look for a way to speed that "bunch of other stuff" instead of getting elements from an array...
m
0
l
a b L Programming
February 2, 2012 8:07:17 PM

I'm not sure if I've understood this correctly but with a half-decent processor it's going to be far quicker to check the 81 elements of the array than to read something from the disk. Disk access speeds are orders of magnitude slower than memory access.

I think you must have misunderstood what your Professor said. What you really need is a hash table; that will require just one memory access to find the element you are interested in.
m
0
l
a b L Programming
February 3, 2012 9:58:19 AM

clydefrog said:
Oh really?


I assumed writing some sort of index file would be much more efficient.my professor kept mentioning it too lol

So should I just keep my program the way iit it?


Yeah just keep things in memory.

If you have two chars, for 81 element positions the char data itself at max is taking up 324B of memory space.

What he perhaps meant (and you should 100% ask for clarification) is that if you have large volumes of data in your application you may want to do some kind of "paging" policy out to a file to free up memory. It's well known that this will have performance penalty and should only be used when appropriate. It's inappropriate for you to implement such a feature here with a statically sized array that is backing fixed sized primitive data.
m
0
l
!