Frage

Ich fange an, indem ich sagte, ich habe meinen Kopf nicht ganz um Oop umwickelt.

Ich brauche eine Routine, die jedes Wort in einer Zeichenfolge iterhaltert, prüft, ob es in meiner verknüpften Liste ist, und fügt es entweder als Knoten hinzu, wenn er nicht oder erhöht die Zählung des vorhandenen Knotens, wenn er in der Liste.

hier habe ich:

generasacodicetagpre.

Bevor ich dann den addnode= new wordnode (stringword) versuchte, zu Beginn jeder Iteration über die Schleife "für jedes Wort in String", aber das würde neu definieren und die Klasse definieren Die Anzahl der Zählung auf 1. Nun kann ich derzeit den Zählern nicht erhöhen, da addnode.count += 1; nicht definiert ist. Ich hatte gehofft, dass ich überprüfen könnte, ob StringWord in der verknüpften Liste war, und wenn ja, inkrementieren stringword.count um eins, aber das wirft einen Fehler aus.

das jetzt ansehen, ich denke an den addnode.count += 1; gehört in der while-Schleife ein paar Zeilen darunter ...

Hier ist meine WordNode-Klasse:

generasacodicetagpre.

War es hilfreich?

Lösung

Versuchen Sie das:

generasacodicetagpre.

und alternative ist die Verwendung von linq:

generasacodicetagpre.

Andere Tipps

It sounds like you are just trying to practice making a linked list so this might not be helpful, but a much simpler solution is to use Key/Value pair like a dictionary.

Dictionary<string, int> Words = new Dictionary<string, int>();
string wordsList = "a list of words for testing a list of words for testing";
foreach (string word in wordsList.Split(' '))
{
   if (Words[word] == null)
      Words[word] = 1;
   else
      Words[word] += 1;
}
System.Console.WriteLine("testing: {0}", Words["testing"]); //result- testing: 2

The result of indexing the Words dictionary by the string will return the number of words.

This looks like a job for a Dictionary or here

Dictionary<string,int> myDict = new Dictionary<string,int>();

foreach(string str in listOfWords)
{
   myDict.add(str,0);
}

foreach(string x in cleanText.split(' '))
{
if(myDict.ContainsKey(x))
   myDict[x]+=1;
}

After running through the foreach myDict will contain counts for each word in the "bag of words."

Edit

if(myDict == null)
   myDict = new Dictionary<string,int>(); //assuming running tally at higher scope.


foreach(string x in cleanText.split(' '))
{
if(myDict.ContainsKey(x))
   myDict[x]+=1;
else
   myDict.add(x,1);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top