0

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!

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by avirag
0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.