Setting List<E> files in location for later use - java

I'm working on some kind of a game. There are default values that don't change and are stored in a list. My question is, is there a way to make a list once and add it to the app files so if I need the list all I need to do is List<E> new = mylist? or something along those lines? I could work with arrays if needed but preferably lists.

Related

Is there a way to create an ArrayList under certain circumstances?

I'm sorry if the question is phrased weirdly, but I wasn't quite sure how to fit it into the title space. I'm making a mini messaging program in which users create and log into accounts and send and receive messages. Is there a way to create an ArrayList of the user's messages when they create an account? All of the usernames are in another ArrayList, so maybe it can create one for every addition? I have the passwords and usernames in two different lists linked by position, so that could work too if it's even possible.
PS - I also need to be able to pull out and match the ArrayList to usernames, but that will come later.
I can clarify in the comments and show my code if you need it.
Thanks!
It sounds like you are looking for a data structure to store a list of messages per user. You can use a Map<User, List<Message> for this. When loading/adding a User, you can create an empty ArrayList<Message> and put it into the map for later use.
// Create map.
Map<User, List<Message>> userMessageMap = new HashMap<>();
// Insert new list for new user.
userMessageMap.put(user, new ArrayList<>());
// Insert message for existing user.
userMessageMap.get(user).add(message);
// Get all messages for an existing user.
List<Message> messages = userMessageMap.get(user);
The Answer by Pieter12345 is correct and smart.
Here is a table graphic I made to assist you in choosing an implementation of Map from among those bundled with Java. All of these support the operations seen in that other Answer.
Third-parties produce may produce Map implementations as well, such as Eclipse Collections or Google Guava.

Working with multiple ID's

I am creating a project in which I prompt the user with a table. And he has to fill in the values. And when he clicks on submit, at the back end I have to check all those values are correct or not.
Here is a sample pick of the table and code
As you can see i have given the id's as "fcfs_p1_ct" for p1 and "fcfs_p2_ct" for p2. Now at the back end when I have to check the values entered by the user. I have to create objects of the respective Edit Texts.
Now one way is to create n number of if else conditions and create the objects for each edited text manually. Can this process be done through loops?
For example for p1
String str = findViewById(R.id.fcfs_p1_ct).getText().toString;
similarly for p2
String str = findViewById(R.id.fcfs_p2_ct).getText().toString;
and so on for n number of processes.
As the only changing variable in the statements is the process number. Please suggest me a way to do the same for a loop
This kind of situations in android are not handled by loops, once you realize that your layout (XML) requires lots of View id assignments, it's recommended to use an Adapter. There are many ready-to-go Adapters available in android SDK, such as ArrayAdapter, BaseAdapter, CursorAdapter etc.
Which Adapter to choose depends entirely on your choice of View/layout you wish to inflate and the data type and source you're willing to use.
In your situation, I'd recommend starting with a simple ListView, and using a custom Adapter (class that extends BaseAdapter). There are many tutorials out there on how to implement this, which are by the way quite easy to follow.
You can simply keep these ids in an ArrayList of Integer like;
ArrayList<Integer> ids = new ArrayList<>();
ids.add(R.id.editText1);
ids.add(R.id.editText2);
When the user presses submit, you can go through that array and do what you want. If you would like to change the corresponding value at the end, you can keep a Map instead of ArrayList.
But my personal suggestion is that using a ListView or RecyclerView. You can repeat views with an Adapter and keep your data as instances of a class so that you can avoid these kind of codes.

I can't seem to find the right Collection for my case. Thinking about list of hashmap of list, which appears very abstract

I'm currently trying to write this programm challenge: https://www.reddit.com/r/dailyprogrammer/comments/2hcwzn/09242014_challenge_181_intermediate_average_speed/
Basically, I've gotte to the point where the cars pass the 4 cameras, and I want to save the time every car has passed a specific camera individually.
So far my thought process was to do a list of a hashmap of lists, but I feel like I'm thinking too abstract, mostly because I am really not feeling too safe with which collection to use at which time (thats why I'm doing this exercise mostly).
Basically, my list would consist of various hashmaps of lists. The "lowest" lists would contain all 4 time dates for a specific car ( data for camera one, data for camera two, and so on). Then I would attach this list to a hashmap, which would have the specific plate of the car as key. The "highest" collection list would then include all the hashmaps, so basically the list would store all car plates which store all data for each camera.
I feel like I made it a little bit hard to follow, so I painted this mindmap for you: https://i.imgur.com/SXGLTvX.png
I hope someone can point me in the right direction.
For the speed limit, you just need a number... Maybe use a double?
For the cameras, you need an array or a List of distance (ints). Either adt is fine
To track if a car is speeding, you can use a Map<String,Date> to record their last position. Then, as you process each set of camera data, you can use this Map to get the last data, do your speeding calculations, and then put the new data into the Map so that when you're processing the next camera data, it will be up to date.
That's how I might design it... Make sense?

Adding list in formatted way?

I'm bukkit python/jython coder, but I'm struggling adding my list to config. Since I can't get it to work with Bukkit API, I got idea to do it on more Pythonic way. I know how to write files using this format (for example):
fo = open("%s/TwistedJobs/config.yml"%pluginlocation2, "wb")
fo.write("Farmers:")
fo.close
I have config YAML file, in which I want to store player names with their job, here's example:
Farmers:
- Player
- Player1
Miners:
- Player2
- Player3
Traders:
- Player4
- Player5
Now I want to add players upon an event to list. For example, Farmers list. There is no problem with fetching player names and triggering event, that is not problem. Plan is to get that arraylist and add another player name there (I've done that already). Now I want to add that new arraylist in config, but how? They must be in format given above (config example)...
This is how I fetch arraylist and add new players to same list (it's working perfectly, but I don't know how to put that new list into config, since I can't get BukkitAPI way to work):
list = pyplugin.getCfg().getStringList("Farmers")
list1 = list.add(event.getPlayer().getName())
Thanks in advance, Amar!
No idea about the Bukkit API, but a list in python usually must me appended:
list.append(event.getPlayer().getName())
Also putting some traceback or errors might help anybody trying to answer your questions.

the undo and redo implementation in Java

I want to ask a question about List in Java.
It is easy to implement delete,add and search an element in a list. But how to implement the undo and redo of a list in Java?
can anyone help me on this?
You might be looking to implement a Command Design Pattern for this. A decent simplified example for List can be found here http://www.algosome.com/articles/implementing-undo-redo-java.html
I guess you want to undo and redo the operations like delete and add on a list.
Just use another list with indexing capabilities for Undo and Redo, e.g. an ArrayList:
Each time an add(element) or delete(element) to the original list really changes that list, you put the element at the end of your undo-list.
Then when you want to undo the operations, you just move through the undo-list: If the element in the undo-list is not in the original list, add it, if it is present in the original list, remove it.
If you want to use your undo-list for redo, too, then don't remove the elements you just "undid" from the undo-list, but rather move through the undo-list via an index. Then you can move through the undo-list in both direction and hence undo and redo your operations.
You mean like delete item from list and undo it? You can easily create new class for a list and define properties like: last performed action + store the origin value (and possibly index) of the effected item. The same for redo (at least for one step in redo and undo). If you don't care about order of the items (or you can order them easily), then define list of last performed actions and list of origin values. So for example:
lastAction[0]="delete";
lastElement[0] = 1; // means you deleted 1 from the list
That's the first and dummy idea how to that. Perhaps there are some issues to consider...
You need binding to do that.
To me this is the most efficient way to do that. This question at SO can give you some hints regarding from where to start.

Categories