Storing object information in java - 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.

Related

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 :)

How to store list of workers objects in java?

What is the most convenient way to store list of workers in Java considering following conditions:
Each worker has attributes of Name, LastName, Wage, BirthDate.
2.I want to store them in file by appending when needed. For example 1 object I added to file today, 1 tomorrow, 3 in one month and so on.
I want to read them also when needed, retrieve, sort, delete, edit and perform other operations.
I tried to store them in file with ObjectOutputStream, but the problem is I need to store them at once, so appending later is not possible.
So my question is which type of storing to implement?
I suppose workers is a struct...
Have you thought on using an array of workers? or a list?
So here's the thing I think you're missing: ObjectOutputStream is a method for carrying out serialization (the converting of an object to a writable value). So, to use OOS, you have to implement that in your worker class to allow it to be serialized on its own. But, since you want to store more than one of these, you can use one of the many java storage mechanisms (all of which implement serializable).
For this particular situation, I'd recommend using an ArrayList. Put all of the worker objects in an ArrayList and then send that list to the ObjectOutputStream. Then, when you pull the list back into your code from the file, you can continue to add more workers to it as you please and then you can serialize the updated list without a problem.
Once you have the list pulled into your code, you can use myList.get(index), myList.remove(index) and all of the other methods that you can find here: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
As an aside, I think an ArrayList would be better than a normal array because when you declare and instantiate a normal array you have to specify the length of the array. When you use the ArrayList, you can have as many objects as you want in the list.

How do I assign an identifier to a Java object

I have the following situation. In my Java back end I collect some data from a service, I create data objects out of it and pass that to my Angular front end. Then a user might choose do something with one of the objects and sends its changed state to the back end. The back end collects the same data again from the service and now I have to identify which object the user choose. I want to do this by creating a common identifier. And here comes my question: how can I best generate the identifier?
First I did the following:
Get data from service
Assign an increasing integer in for-loop
However, I ran into the following problem. The second time the service is called, the same data might return but in a different order. So my first thought was to order the collected data first and then assign the integer. But ordering this data is quite a pain so I thought of something else:
Take the two fields of the data object that makes it unique
Make a string out of those fields and concatenate
Do a .hashCode() on the result
This creates an int that can't be traced back to the original data. That's exactly what I want, but now for the funny part. The hashCodes can collide. My fear is that in very, very rare cases the hashCode will be the same, even though the original string was different!
Is my fear grounded? And how can I compose a better id?

Java Filling an array with infinite variable names

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.

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