Reputation: 3
Context
I have two user input fields, and I want to save the input data into a List of String Array (NOT in file or databse).
As a part of my future work, I will have to perform search by specific value and list/display all the inputs that user has entered so far. Moreover, the value of one input field should be associated with other input field. For example- in the first field user writes "Nice", and second field he writes multiple words- "beautiful", "Excellent". That means the last two words should belong to the first one- "Nice". Pretty much like a Synonym App.
However, at this moment, my focus is to store the inputs, that happens when user clicks on Save
button.
Code
Class that implements an Interface:
class SynonymApp : ISynonym
{
private List <string[]> allItems = new List <string[]>();
public void AddSynonyms(IEnumerable<string> synonyms)
{
//adding objects at the end of the list. Am I doing right?
allItems.Add((synonyms.ToArray<string>()));
}
}
Class where button click event happen:
private void button1_Click(object sender, EventArgs e)
{
List<string> userInput = new List<string>();
string wordInput = textBox1.Text;
if (wordInput == "") throw new Exception("Please give input");
char[] emptySpace = new char [] {' ', '\t'};
string[] synonymInput = textBox2.Text.Split(emptySpace);
userInput.Add(wordInput);
userInput.AddRange(synonymInput);
synonymApp.AddSynonyms(userInput);
}
Questions
List
or an array
? What does a List
of Array
do, in general?private List <string[]> allItems = new List <string[]>();
) grow in size according to the needs?Upvotes: 0
Views: 164
Reputation: 521
Is the right choice to save the string input into a List of Array over just a simple List or an array? What does a List of Array do, in general?
In your case, No. Always try keep things simple. I never see the use of List<string[]>
. Arrays and List are two different things and should not be combined. Even if you use it will be hard to maintain, to debug and won't be much optimal.
Will my List of array (i.e.
private List <string[]> allItems = new List <string[]>();
) grow in size according to the needs?
Yes, but I would try to avoid using List<string[]>
unless there is a specific reason.
IMO, you should organize your code into classes and use HashSet
as it is usually faster than List
.
class MySynonyms
{
public string Word { get; set; }
public HashSet<string> Synonyms { get; set; }
}
Hope this helps.
Upvotes: 2
Reputation: 11
You may instead use List<List<string>> which would grow as needed. Another solution may be a Dictionnary<string,List<string>> You should try to choose the right one. Array of string does not grow as easily as list.
Upvotes: 0
Reputation: 35400
You didn't tell the context of your application (you just said Pretty much like a Synonym App), but if you really are storing synonyms, know that a synonym is a binary relation; i.e. if A is synonymous to B, B too is a synonym of A.
In this context, I'd just store all your words (whether main words, or synonyms) in a large list of strings (List<string>
). Alongside this list, I'd store another store the relationships in a List<(int ,int)>
, storing the indexes of main word and synonym word in each Tuple.
If you use a DB to serialize this list (which you said you won't), This would translate to two simple tables; Words (int ID, varchar Word) and Relations (int ID1, int ID2).
Upvotes: 0
Reputation: 2098
If you really want the first word in the input to "own" the other two, I would use Dictionary<string, List<string>>
. That separates out the original word from the synonyms, and makes it searchable.
You are correct that you need multidimensional storage (List of Array, Array of Array, etc.). Or, better, create a SynonymEntry
class:
public class SynonymEntry
{
public string EntryWord { get; set; }
public List<string> Synonyms { get; set; }
}
Then you could create a simple List<SynonymEntry>
to store all the data.
List vs. Array - I would only use an array if I know that there will never be new information added later.
Upvotes: 2