a generalized list structure can be declared in C as follows:

typedef struct element{
        bool sign;
        union{
               char data;
               element* link; 
        };
        element* link;
}

as you see in this approach an element can have either "data" or "element* link"(the last link is included in every element) and union works great in this point.

If we try to move this code to Java,how can we find the corresponding of the union part?
Should we use inheritance as a parent class and two subclasses,one of them has data other has link?Would it be a good approach? Or if are there any better approachs you can suggest,please let me know...

Recommended Answers

All 4 Replies

a generalized list structure can be declared in C as follows:

typedef struct element{
        bool sign;
        union{
               char data;
               element* link; 
        };
        element* link;
}

as you see in this approach an element can have either "data" or "element* link"(the last link is included in every element) and union works great in this point.

If we try to move this code to Java,how can we find the corresponding of the union part?
Should we use inheritance as a parent class and two subclasses,one of them has data other has link?Would it be a good approach? Or if are there any better approachs you can suggest,please let me know...

if I correctly understand what you are trying to do (and since my knowledge of the C++ syntax is quite limited, I don't claim I surely do) you want to have a list with two kinds of elements: basicly the same, but the one kind of Object can have a data, the other kind an element - variable, none can have the two.

there are 2 options just jumping in mind (not that there can't be better) but here we go:

1. you create an Object which has both the variables, but you make sure you can only have one of those variables not null. for instance

Class Test{

  private String shared = null;
  private Data data = null;
  private Element element = null;

  public Test(String shared, Data data){
  this.shared = shared;
  this.data = data;
  }
  public Test(String shared, Element element){
  this.shared = shared;
  this.element = element;
  }
  // or maybe a more general constructor
  public Test(String shared, Object object){
  this.shared = shared;
  if(object.instanceOf(Data)
    this.data = (Data)object;
  else{ if(object.instanceOf(Element)
            this.element = (Element)object
           else
             // you could, but don't have to throw an Exception here
   }
  }

  // make sure you get the getters
  public void setShared (String shared) { this.shared = shared;}
  public void setData (Data data){ 
    if (this.element == null) this.data = data;
  }
  // same for element
}

an other option I see, is to create an Interface, which contains the methods for the shared elements, implement it in two different classes, and adapt each of them with the correct methods for their own specific variable (data or element)

Josh Bloch has an excellent article specifically on substitutes for C constructs here:
http://java.sun.com/developer/Books/shiftintojava/page1.html
It is an actually a chapter except from his book Effective Java Programming, which I would highly recommend for any Java programmer.

if I correctly understand what you are trying to do (and since my knowledge of the C++ syntax is quite limited, I don't claim I surely do) you want to have a list with two kinds of elements: basicly the same, but the one kind of Object can have a data, the other kind an element - variable, none can have the two.

there are 2 options just jumping in mind (not that there can't be better) but here we go:

1. you create an Object which has both the variables, but you make sure you can only have one of those variables not null. for instance

Class Test{

  private String shared = null;
  private Data data = null;
  private Element element = null;

  public Test(String shared, Data data){
  this.shared = shared;
  this.data = data;
  }
  public Test(String shared, Element element){
  this.shared = shared;
  this.element = element;
  }
  // or maybe a more general constructor
  public Test(String shared, Object object){
  this.shared = shared;
  if(object.instanceOf(Data)
    this.data = (Data)object;
  else{ if(object.instanceOf(Element)
            this.element = (Element)object
           else
             // you could, but don't have to throw an Exception here
   }
  }

  // make sure you get the getters
  public void setShared (String shared) { this.shared = shared;}
  public void setData (Data data){ 
    if (this.element == null) this.data = data;
  }
  // same for element
}

an other option I see, is to create an Interface, which contains the methods for the shared elements, implement it in two different classes, and adapt each of them with the correct methods for their own specific variable (data or element)

thanks,this approach seems simpler than class hierarchy equivalent of union 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.