Download the Tom's Hardware App from the App Store
The reference for current tech news
Yes No
Tom's Hardware > Forum > Applications > Programming > Java: Transfering 2d array into a file

Java: Transfering 2d array into a file

Forum Applications : Programming Java: Transfering 2d array into a file

Word :    Username :           
 

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

Reply to clydefrog
Register or log in to remove.

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!

Reply to Rusting In Peace

Writing to disk is always slower than doing it in memory.

Reply to totalknowledge

Rusting In Peace wrote :

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?

Reply to clydefrog

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.

Reply to totalknowledge

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

Reply to theDanijel

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.


Message edited by Ijack on 02-02-2012 at 11:08:09 PM
Reply to Ijack

clydefrog wrote :

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.

Reply to Rusting In Peace
Register or log in to remove.
Tom's Hardware > Forum > Applications > Programming > Java: Transfering 2d array into a file
Go to:

There are 497 identified and unidentified users. To see the list of identified users, Click here.

  • Ask the community now
  • Publish
Ad
Latest best answer
US
By oldmangamer_73, 92 days ago:

Of course it's possible. I believe you must be US citizen though. I would begin a...

They won a badge
Join us in greeting them