How do I assign names to variables in an arraylist? - java

I am doing a project for my AP Computer Science class. The current project asks us to create a rip off version of excel that prints to the console. I am wondering if theres a way for me to create an arraylist of empty values that I can assign cell names to (A1, A2, F6, etc.) and then use those names to call values from their place in the arraylist. For example, I'd give the cell D4 a value of 6, store that 6 in an arraylist, then call it back using "D6".
A link to the directions of the project: https://issaquahwednet-my.sharepoint.com/:w:/g/personal/stutlerk_issaquah_wednet_edu/EQOW8BFzHIhIsdvXGP-qKDsBbN7BFa-kUCiMeeq9BZbbwg?e=Nc1Jcs
Maybe I'm not making sense, for which I am very sorry.
//what i would like to be able to do:
arraylist "cells" = (A1 through L20)
user input = "A1 = hi"
set index "A1" of "cells" to "hi"
repeat that stuff until user input = quit
is this possible? or would I just have to create two arraylists, one for cell numbers and one for values? I guess I could do that and then if a user wants to see what a cell says then they could Say "A1" and I could search my cell arraylist for a value of A1 then compare that index with the other arraylist.
I'm completely lost one this and, again, sorry if I'm not making any sense.

To do what you want, you'd need a map rather than a list. Lists always have integer numbers as the indices and there's no way to change that. (You could technically do this with two arrayLists, but it would be a pain to maintain it so that "A1" and "hi" stayed in the same index on two different lists if you ever had to delete/move anything.)
The HashMap documentation is here: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
And you'd do something like this:
Map<String, String> cells = new HashMap<String,String>();
cells.put("A1", "hi");
That would create a key-value pair with key "A1" and value "hi".

You can use structure map where for every key you have associated a value.
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

Related

Any way to make a top 5 in java displaying the variable names?

I've been trying to make and display in JFrame a top five out of 13 options. It goes something like this:
I have a .txt with a bunch of words, let's say apple, banana, orange, etc. And a counter for each word.
Then I open the file and scan it with a Scanner and a String, if it finds the word "apple" then it adds 1 to the apple counter. After it scans the whole .txt I put the counters in an array and sort them with Arrays.sort(array);
My problem is that the array only holds the numeric value for each word, so I tried comparing the last position in the array (since it's the highest one) with every counter for each word, and if it matches with any counter I set that one as "Top 1", then I do the same for "Top 2" and the second-to-last in the array and so on.
But this obviously only works if there are no repeated values. If "Apple" and "Orange" both appear 3 times then both will match and then, the program will set the first one matched in the TextField1 and 2.
Is there a way to go around this? Maybe a way to display the variable name in the TextField instead of the value? or maybe a different data structure to keep both the name and the value of the variable?
Thanks in advance!
or maybe a different data structure to keep both the name and the value of the variable?
Ding ding! You want to use a map for this. A map stores a key and a value as a pair. To give you a visual representation, a map looks like this:
Cats: 5
Dogs: 3
Horses: 2
Using a map you can access both the key (name of the word you found) and its paired value (number of times you found it in your file)
How to use maps
You might want to consider priority queue in java. But first you need a class which have 2 attributes (the word and its quantity). This class should implement Comparable and compareTo method should be overridden. Here's an example: https://howtodoinjava.com/java/collections/java-priorityqueue/

Data Structure choices based on requirements

I'm completely new to programming and to java in particular and I am trying to determine which data structure to use for a specific situation. Since I'm not familiar with Data Structures in general, I have no idea what structure does what and what the limitations are with each.
So I have a CSV file with a bunch of items on it, lets say Characters and matching Numbers. So my list looks like this:
A,1,B,2,B,3,C,4,D,5,E,6,E,7,E,8,E,9,F,10......etc.
I need to be able to read this in, and then:
1)display just the letters or just the numbers sorted alphabetically or numerically
2)search to see if an element is contained in either list.
3)search to see if an element pair (for example A - 1 or B-10) is contained in the matching list.
Think of it as an excel spreadsheet with two columns. I need to be able to sort by either column while maintaining the relationship and I need to be able to do an IF column A = some variable AND the corresponding column B contains some other variable, then do such and such.
I need to also be able to insert a pair into the original list at any location. So insert A into list 1 and insert 10 into list 2 but make sure they retain the relationship A-10.
I hope this makes sense and thank you for any help! I am working on purchasing a Data Structures in Java book to work through and trying to sign up for the class at our local college but its only offered every spring...
You could use two sorted Maps such as TreeMap.
One would map Characters to numbers (Map<Character,Number> or something similar). The other would perform the reverse mapping (Map<Number, Character>)
Let's look at your requirements:
1)display just the letters or just the numbers sorted alphabetically
or numerically
Just iterate over one of the maps. The iteration will be ordered.
2)search to see if an element is contained in either list.
Just check the corresponding map. Looking for a number? Check the Map whose keys are numbers.
3)search to see if an element pair (for example A - 1 or B-10) is
contained in the matching list.
Just get() the value for A from the Character map, and check whether that value is 10. If so, then A-10 exists. If there's no value, or the value is not 10, then A-10 doesn't exist.
When adding or removing elements you'd need to take care to modify both maps to keep them in sync.

Java: User defined String, that points to an Array

Basically I have a method that takes a String name and will delete that entry if found.
I have a LinkedList composed of 26 LinkedList instances, named (alphabetically) as follows a, b, c, ..., z
Lets say when the user enters a word like "Charlie" so I want to delete it.
Charlie starts with 'c' so how can I make my code point to the LinkedList c without having to traverse all the LinkedList items.
So far I get the name Charlie, get char at 0 index, convert it to lowercase and call it String Letter. Is there a way to do [Letter].remove(name);
I asked a similar question but I don't think I wrote it well.
Thanks.
Maybe you should take a look closer into the java API and try not to reinvent the wheel. Basically you can use a HashMap to wrap you LinkedList instances instead of using a LinkedList because it will let you select your items based on a key (the alphabet characters in your case)
Map<Character,LinkedList> myMap = new HashMap<Character,LinkedList>();
After having filled your Map (simply calling myMap.put('c', new LinkedList()) and so on for each char), you can get the desired LinkedList, without having to iterate through the whole map:
myMap.get('c');
You are looking for a trie data structure.
If you still want to do it using your approach, Instead of having a linked list of lists, try having an array of arrays.Might come in handy

2 Dimentional array with Hash-map

Is there a way to put a 2d array into a Hash map setup?
Example of the array would be two strings {"John", "red"},
{"George", "blue}
And I would want red to correspond to john etc.
I know I can use a nested loop to go through every item in the 2d array but how would then set it up to add them to the hash-map
hashMap.put("John", "red");
Assuming every array has 2 items in the form of {Name, Color}, you can just iterate over it
for(String[] combo : some2DArray){
someHashMap.Put(combo[0], combo[1]); // Or swap them, depending on what you
// want to be the key and the value
}
If you want to avoid the possibility of removing data because you happen to have two people with the same name there are a few approaches you can take:
Keep the old data
Keep the new data
Assign the new data to a new key
Combine the data in the same key
Keep the old data
Perform a check before using HashMap#put and see if the key already exists.
Only add the data if it doesn't exist yet.
Keep the new data
Use the current code, it will overwrite the old value.
Assign the new data to a new key
Create a new key based on your own rules and insert that.
Combine the data in the same key
Define your HashMap as HashMap<String, List<String>> and add the values to the list.
How about implementing a Pair class, so you can use HashMap<Pair<String,String>> ?
EDIT: Might be that I misunderstood your question, is that what yoe were asking?

HashMap<String, Set<String>> cant find its key even though it exists

I have a HashMap of this type
Map<String, Set<String>> list_names = new HashMap<String,Set<String>>();
that I have constructed and added its elements from a txt file that has a list's name and a set of names in it.
98298XSD98 N9823923 N123781 N723872 ....
13214FS923 N9818324 N982389
... ...
I made another HashMap, called names_list that pretty much replaces the order of the list_names HashMap such that I can get all the lists that a given name is in.
now the HashMap I have is pretty big, and there are over 400k items and 60k lists.
somewhere in my code im trying to get the Set of different lists many many times and then getting the intersection of these two lists for computational purposes,
a_list = this.names_lists.get(a);
b_list = this.names_lists.get(b);
// printing lists
//intersection stuff
but whats weird is the HashMap didn't recognizance one of its keys(or maybe many of its keys) and treated it as null after one retrieval or sometimes 0 retrievals.
a:0122211029:[R3DDZP35ERRSA] b:1159829805:[R3ALX1GRMY5IMX, R1204YEYK4MBCA]
a:0122211029:[] b:1593072570:[R222JSDL42MS64]
here, im just printing the name and names_list.get(key).toString();
and yes i'm printing these before doing any intersection stuff.
any idea why is it doing that?
When you calculate the intersection of two sets, you actually modify one of the sets. You have to create a temporary set to hold the intersection, e.g.:
a_list = this.names_lists.get(a);
b_list = this.names_lists.get(b);
Set<String> intersection = new HashSet<>(a_list).retainAll(b_list);

Categories