Java: Transfering 2d array into a file

clydefrog

Distinguished
Dec 5, 2009
7
0
18,510
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
 

Rusting In Peace

Distinguished
Jul 2, 2009
312
0
19,060
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!
 

clydefrog

Distinguished
Dec 5, 2009
7
0
18,510


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?
 

totalknowledge

Distinguished
Jul 8, 2011
56
0
18,590
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.
 

theDanijel

Distinguished
May 4, 2011
74
0
18,590
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...
 

Ijack

Distinguished
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.
 

Rusting In Peace

Distinguished
Jul 2, 2009
312
0
19,060


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.