Hello, I'm new to hashtables in C#, and I'm trying to refer to a bit of Java code, but having a problem.

Here's the Java...
note: adjacencyMap is a Java hashmap.

int count = 0;
for (int i=0; i< adjacencyMap.CAPACITY; i++)
{
if (adjacencyMap.keys[i] != null)
{
LinkedList edges = (LinkedList) adjacencyMap.get(adjacencyMap.keys[i]);
count+=edges.size();
}
}
return count;

Ok, and here's how I've got it translated to C#...

int count = 0;
            for (int i=0; i < adjacencyMap.Count; i++)
            {
                if (adjacencyMap[i] != null)
                {
                    List<T> edges = (List<T>) adjacencyMap[adjacencyMap[i]];
                    count += edges.Count;
                }
            }
            return count;

I'm getting problems where I translated the Java lines that contain "keys".

Thanks!

Hi!

first of all do you need the key? because you can also iterate over all values:

int count = 0;
foreach ( List<T> edges in adjacencyMap.Values)
{
      count += edges.Count;
}
return count;

or if you need also the key:

int count = 0;

foreach (object key in adjacencyMap.Keys)
{
    List<T> edges  = (List<T>) adjacencyMap[key];
    count += edges.Count;
}
return count;

Daniel

well the simple thing you can do is, search the java to C# translator,
and can easily convert your code in C#.....:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.