0

I have a database table of topics that changes frequently. Let's say the table has topic_id and topic in it.

In my code, I need to keep count of how many occurrences are used in each topic.

Whats a good dynamic array datatype to store the counts of each topic?

Should I use arrayList?

An example of how to use it would be helpful.

cdub
  • 24,555
  • 57
  • 174
  • 303
  • Do you need to do anything with the count(s) once you have it? Do you need to store the count somewhere, or are you just calculating it on the fly as needed? – Dr. Wily's Apprentice Jan 19 '12 at 21:29

8 Answers8

2

A dictionary is probably a good choice, as pointed out by other answers.

Assumption:

  • your topic_id is an int data type.

Usage example:

Dictionary<int, int> occurrencesOfTopicsByTopicID = new Dictionary<int, int>();

// The following code increments the number of occurrences of a specific topic,
// identified by a variable named "idOfTopic", by one.

int occurrences;

// Try to get the current count of occurrences for this topic.
// If this topic has not occurred previously,
// then there might not be an entry in the dictionary.
if (occurrencesOfTopicsByTopicID.TryGetValue(idOfTopic, out occurrences))
{
    // This topic already exists in the dictionary,
    // so just update the associated occurrence count by one
    occurrencesOfTopicsByTopicID[idOfTopic] = occurrences + 1;
}
else
{
    // This is the first occurrence of this topic,
    // so add a new entry to the dictionary with an occurrence count of one.
    occurrencesOfTopicsByTopicID.Add(idOfTopic, 1);
}
Dr. Wily's Apprentice
  • 10,212
  • 1
  • 25
  • 27
1

You can use a dictionary <int,int>

Pleun
  • 8,856
  • 2
  • 30
  • 50
1

For anything where you have a key-value sort of data and need a collection of it, a Map (or Dictionary) is going to be the correct choice.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
1

I would suggest a dictionary

Dictionary<string, int> topicCounts

or you can strongly type it a little more

Dictionary<Topic, int> topicCounts

then you just access the count like an indexer

Rikon
  • 2,688
  • 3
  • 22
  • 32
1

A good choice would be

Dictionary<int, int>

or if you're updating/reading it in multiple threads, the excellent

ConcurrentDictionary<TKey, TValue>

Actually, if you like lambdas, ConcurrentDictionary has a (naturally thread safe) AddOrUpdate method that comes in handy when doing counts; can't think of a way to do this without multiple calls in the regular Dictionary<>.

var dictionary = new ConcurrentDictionary<int, int>();

dictionary.AddOrUpdate(topic_id,          // For the topic with id topic_id
                       x => 1,            // Set count to 1 if it didn't already exist 
                       (x, y) => y + 1);  // Otherwise set to old value + 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
1

An implementation of IDictionary<TKey, int> where TKey matches the type you'll lookup on (perhaps Topic perhaps int).

The simplest and fastest for most uses is Dictionary<int, int>. However, since this is ASP.NET, and you seem to be using this for some sort of caching, you'll probably need to access this collection from multiple threads. Dictionary is safe for multiple concurrent readers, so if updates are infrequent then protecting it with a ReaderWriterLockSlim is probably the way to go. If you could have multiple threads trying to update simultaneously though then you may get better preformance from ConcurrentDictionary or my own ThreadSafeDictionary.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
0

Yes . ArrayLists are the best.

Use this namespace to include ArrayLists

using System.Collections;

Declare an array list like

ArrayList myArray = new ArrayList();

Add items to arraylist.

myArray.Add("Value");

Remove items from arraylist.

myArray.Remove("Value");
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

List<System.Web.UI.Triplet> can be used to store a list.

Triples has three properties (First, Second, Third) - which can hold TopicID, TopicName, Count.

Or you can create a custom class to hold your Topic information with ID, Name, Count properties.

Leon
  • 3,311
  • 23
  • 20