0

I have to translate a C++ source code into Java. Unfortunately, I've never been taught C++. Most of it is fairly easy, but I could use a bit of help.

void DepthFirstSearch(HeadNode *V[MaxCities], bool *Visited, int Start)
{
    //display each cited as it is visited
    cout << endl << V[Start]->City;
    //mark city as visited
    Visited[Start] = true;

    //continue depth first search
    CityNode *C;
    int NewStart;

    C = V[Start]->FirstCity;
    while(C != NULL){
        NewStart = C->Vertex;
        if(!Visited[NewStart])
            DepthFirstSearch(V,Visited,NewStart);
        C = C->NextCity;
    }//end while
}//end DepthFirstSearch

The line:

cout << end1 << V[Start]->City;

is particularly confusing. Any help?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52

2 Answers2

2

HeadNode *V[MaxCities] is an array of pointers pointing to HeadNode objects. In Java its just like an array.

To get values or methods from to objects pointer you use the -> operator. In Java its some kind of . oprtator from objects.

cout is an outputstream which writes to stdout, in Java this would be System.out.print()

<< Operator is used to write into this stream.

endl like the new line characters \r\n

al-eax
  • 712
  • 5
  • 13
1

the line cout << end1 << V[Start]->City; is particularly confusing. Any help?

would translate to:

System.out.print("\r\n" + v[Start].City);

There are guides available online by searching Google for "cpp to java - [function]" ([function] being replaced by "cout", in this case.)

Wayne
  • 351
  • 1
  • 13