1

I am porting some C++ code over to Java that uses some heavy pointer arithmetic. My problem is the following:

There is a structure built as such that I have implemented as a class:

In the original C/C++ code I have the following assignments,

struct mst_edge
{
    int u, v;
    double w;
}

mst_edge * tree_edges = new mst_edge[size];

int node_l = tree_edges->u;
int node_r = tree_edges->v;

How is it possible to convert this code to java? The second block declares mst_edge as a C/C++ dynamic array. But in the third block of code it uses the tree_edges as a direct pointer.

What would the Java equivalent of this be, considering memory and references are handled directly by Java?

3
  • Wouldn't that just be the reference to the first value of the array? Commented Mar 20, 2013 at 23:06
  • 1
    The <code> tags can only be used for code that doesn't contain newlines. See How do I format my code blocks? for more info. Commented Mar 20, 2013 at 23:06
  • The second block defines tree_edges as a pointer to mst_edge and initializes it with an array allocated on the free store. It is emphatically not a C dynamic array; C doesn't have new. Commented Mar 21, 2013 at 0:03

3 Answers 3

1

The Java equivalent is

mst_edge[] tree_edges = new mst_edge[size];

int node_l = tree_edges[0].u;
int node_r = tree_edges[0].v;
Sign up to request clarification or add additional context in comments.

Comments

1

A struct in C/C++ is like a class in Java with all its member variables public:

class MstEdge
{
    public int u;
    public int v;
    public double w;
}

You wouldn't normally do that in Java; the member variables would be private, with public accessor methods, but that's closest to a struct.

A pointer to an array in C/C++ is just an array in Java.

MstEdge[] treeEdges = new MstEdge[size];

But take care, in Java, declaring an array means now you have size null elements. Initialize them like this:

treeEdges[0] = new MstEdge();  // and so on for other array positions

Instead of treating a pointer as a direct reference to the first element as in C/C++, in Java you must explicitly refer to the first element.

int nodeL = treeEdges[0].u;
int nodeR = treeEdges[0].v;

Comments

0

In OO Java you would use getters to retrieve the private member variables. size is whatever value you have defined.

class MstEdge {
    private int u;
    private int v;
    private double w;

    public int getU() {
        return u;
    }

    public int getV() {
        return v;
    }
   .
   .
   .
}


MstEdge [] treeEdges = new MstEdge[size];

int nodeL = treeEdges[i].getU();
int nodeR = treeEdges[i].getV();

1 Comment

Ok. All of your answers are the same as what I came up with....so the error I am encountering (in the overall runtime) must be related to something else...hmmm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.