Java Filling an array with infinite variable names - java

I have a project which requires me to have an array that stores two things for each entry:
the location of another entry in the array
A Boolean value
To do this I've considered making a new object which has a location and boolean method which would store each of the two things above. And then I would just need to open a 2D matrix of this type object.
Here lies my problem, the size of the 2D matrix must match that of a level file which could be as big as 300*300. I need a way in which I could create and name an object for each cell in the 2D matrix. The name doesn't matter because I will just refer to it as Object[n][n].location. Is there a way to do this? To make a large amount of objects with different names? Without having to do each one by hand.
I'm also open to other methods of solving this problem, this is just the solution I've thought up.

Related

Storing object information in java

I am making a small game in Java which involves guessing what the specified attribute belongs to (For example, what country does the city of Bogota belong to?).
My initial idea was to create a custom object which had the name as the first attribute and then the other attributes of the object (like shape, size, color) as the rest of the attributes. Then you would have a parent array containing these objects and to find the specific attribute of the object you would be guessing, it would pick a random number and select that number object from the array.
I am at the point where I am lost in how I should make this array. If I have 100 objects, it doesn't seem efficient to me to say, create an ArrayList of custom objects, then define each object individually with their attributes, then do list.add(object) 100 times.
Surely it does not make sense to create and store every object each time my main loop is run? Would there be a more effective way of putting each object into an array? Would there be a way to store each object into a file and my program retrieves the object and its information from a different file when I need to call it? I'm pretty new to this so maybe it doesn't make much sense. Any help would be greatly appreciated.

How to load a specific object from a SER-file

I'm saving my game world using serialization. The world is basically a big ArrayList containing each area map object. The number of area maps is fixed, but they vary greatly in size (between 5-30 mB each). So I don't want to have the entire world loaded in memory during runtime, only the specific area maps that the player's characters are located in.
Is it possible to load specific area maps without loading the entire world (or in other words: loading a specific object without deserializing the whole SER-file)? Likewise I need to save specific area maps (when all characters have left the area). Note that the size (in data) of the area map could have changed significantly, since the player could potentially move hundreds of objects from one area to another.
The point of this question is that I want to avoid storing each map in it's own SER-file. That would be A LOT of files, which will become even more with each save the player has. So my goal is to pack it up nicely in a single save-file.
What you could do is create a numbering system for each of your area maps. Afterwards, store an array containing these area maps in a serializable. Unload the serializable to obtain an array, and store it in some form of variable. You could then use your numbering system to select the map which you would want to use.
So to answer your question, I do not think it is possible, because the way a serializable is deserialized is it goes through all the serialized fields one by one. You cannot select something from the middle of a SER-file to deserialize.
Happy Programming :)

Cost of storing and retrieving data

I am dealing with the case of pattern recognition and for input, I need to read coordinates of points, up to 10000 points from a text file. I need to perform certain calculations on the points read. So, my question is whether I should always read them from the text file when I need to do some calculation or I should store them in some data structure, eg, a 2D array and hence access the values. What difference would there be in terms of storage and time?
Edit:
The language I am using is Java.
The data structure is user defined that has
a constructor that instantiates the object with x and y coordinates.
a method to draw the point on standard output.
a method to draw a line between two given points.
a method to compare the position of two points based on coordinates.
a method to compute the slope between two points.
an inner class extending Comparable interface that can be used to compare two points.
The comparison is based on slope made by each point w.r.t a reference point.
PS - I am sorry if the question is a silly one but just wanted to be clear about things than shying back. Thanks in advance!
Well storing them in the memory will be the recommended option here, assuming that its a static text file(meaning that the points don't change when you are running the recognition ) as it will really speed up the whole process and 10000 points is not too large to be cached in memory.
Not to mention this point also depends on the fact which language and data structure you are using.

Programming a 2D grid in Java

What is the best data structure to use when programming a 2-dimensional grid of tiles in Java? Tiles on the grid should be easily referenced by their location, so that neighbors and paths can be efficiently computed. Should it be a 2D array? An ArrayList? Something else?
If you're not worrying about speed or memory too much, you can simply use a 2D array - this should work well enough.
If speed and/or memory are issues for you then this depends on memory usage and the access pattern.
A single dimensional array is the way to go if you need high performance. You compute the proper index as y * wdt + x. There are 2 potential problems with this: cache misses and memory usage.
If you know that your access pattern is such that you fetch neighbours of an element most of the time, then mapping a 2D space into a 1D array as described above may cause cache misses - you want the neighbours to be close in memory, and neighbours from 2 different rows are not. You may have to map your 2d tiles in a different order to your 1d array. See Hilbert curves for example.
For better memory usage, if you know that most of your tiles are always the same (e.g. always grass), you might want to implement a sparse array or a quad tree. Both can be implemented quite efficiently, with cache awareness in mind (the sparse array link is good example for this). Another benefit is that these can be dynamically extended. However, you will always have to pay extra levels of indirection in the end for this to work.
NOTE: Be careful with using generic classes such as HashMaps with the key type being some primitive type or a special location class if you're worried about performance - you will either have to allocate an object each time you index the hash map or pay the price of boxing/unboxing. In addition to this, hash maps will not allow you efficient spatial queries (e.g. give me all objects existing in the radius R of a given object - quad trees are better for this).
If you have a fixed dimension for your grid, use a 2D array. If you need the size to be dynamic, use an ArrayList of ArrayLists.
A 2D array seems like a good bet if you plan on inserting stuff into specific locations. As long as its a fixed Size.
The data structure to use really depends on the type of operations you will perform:
In case the number of meaningful positions (nonzero/nondefault) in the grid is rather low (<< n x m) it might be more space efficient to use a hashmap, that maps (x,y) positions to specific tiles. Also you can iterate over meaningful positions alot more efficiently. In addition you could store references to neighboring tiles to each tile to speed up path/neighborhood traversal.
If your grid is densely filled with "information" you should consider using a 2d array or ArrayList (in case you will at some point have generic types involved as "tile-type", you have to use ArrayLists, since Java does not allow native arrays of generic type).
If you simply need to iterate over the grid and random addressing of cells, then MyCellType[][] should be fine. This is most efficient in terms of space and (one would expect) time for these use-cases.

How to store an array of instances

I'm coding in Java, but most languages would do just fine.
Right now, I have an implementation like this:
I have an array that stores objects from a class. The array's length is 10,000.
This is for a little project I working on. Essentially, over time, any place in the array can be unused or have an object in it. Objects can be created or destroyed at any moment.
What I was trying to figure out was the best way to store and recall them to minimize time for two steps:
Creating an object. Cycling through the array until you find an open slot can be slow when lots of instances are near the front.
Drawing an object. I have to cycle through the array constantly, and, based on the existence of an instance, retrieve and display information regarding it.
My current system uses a list of the Objects, as well as a list of Booleans. When creating an object, I just cycle through till the first empty place in the array, then fill it, and to draw, I just go over the whole thing.
Granted, it isn't slow enough to make the project impossible, but I'd still like to know the most efficient method.
The best way is not to do this. Use one of the Java collections. If you aren't going to be adding or removing things very often, maybe use a ArrayList. If you are, then use a LinkedList. And if you want to maintain a mapping to particular indices, you might consider e.g. a TreeMap. These are all iterable.
What about using some sort of named-map construct instead of an array such as a HashMap?
This will link your objects to a unique key and then you can just use .remove(key) to remove the object once its done.
No real iterations involved, just direct access.
You could maintain a second datastructure, a simple list of 'unused' locations in your array. When you remove an object, push either a reference to that array location or the index of that array location into your list. When you need to add an object, take the first item off your list. When the list is empty, then you get to scan your array as you do now, but when the list isn't empty, finding an unused element can be very quick. (You could keep a list of all unused places, which would remove the full-array search, but keeping a list of 10,000 unused items also seems excessive.)
You could have a list where you could place the indexes of the empty spaces in the array of objects.
Instead of using an array, you could use a HashSet or even HashMap without worrying about open slots or the max capacity your array can hold. You also don't need to worry about adding duplicate objects in HashSet or HashMap. With HashMap, it allows you to get your object by whatever key you assigned to the object... makes your code looks cleaner without all the loopings.
You could use Object[] and just keep around an index to the first open slot. Although, that's pretty much what an ArrayList does.
Also, be aware that iteration through an array of 10k items, is incredibly fast. I wouldn't be concerned about that time unless you have proven it to be an issue.

Categories

Resources