1

The title may be confusing, but I will try to explain it with the following example.

I have defined two different types of classes, CDMarker and PDLMarker. In a processing method as below, I create a arraylist for one of the types and use a foreach loop to analyze each one of them.

ArrayList<CDMarker> cdMarkers = new ArrayList<CDMarker>();

......

for (CDMarker marker : cdMarkers) {
   .....
}

Which class to use depends on user input. For example with condition1, CDMarker is the type to use. Condition2, PDLMarker.

Instead of having code like:

if condition1
{
    ArrayList<CDMarker> cdMarkers = new ArrayList<CDMarker>();

    ......

    for (CDMarker marker : cdMarkers) {
       .....
    }
}

if condition2
{
    ArrayList<PDLMarker> pdlMarkers = new ArrayList<PDLMarker>();

    ......

    for (PDLMarker marker : pdlMarkers) {
       .....
    }
}

can I have a fake class (Marker) which can represent either type? Something like:

if condition1
    Marker = CDMarker;
if condition2
    Marker = PDLMarker;

ArrayList<Marker> markers = new ArrayList<Marker>();
for (Marker marker : markers) {
       .....
}

Edit: Looks like interface is the way to go. The problem is that the creation of the List of CDMarker/PDLMarker is done in a method:

if condition1
    List<Marker> markers = writeCDMarker(sheet, dir);
if condition2
    List<Marker> markers = writePDLMarker(sheet, dir);

public static List<PDLMarker> writePDLMarker(HSSFSheet sheet, File dir) {
    List<PDLMarker> pdlMarkers = new ArrayList<PDLMarker>();
    ....
    return pdlMarkers;
}

public static List<CDMarker> writeCDMarker(HSSFSheet sheet, File dir) {
    List<CDMarker> cdMarkers = new ArrayList<CDMarker>();
    ....
    return cdMarkers;
}

Now I got an error at line List<Marker> markers = writePDLMarker(sheet, dir); saying Multiple markers at this line - Type mismatch: cannot convert from List to List - Type mismatch: cannot convert from ArrayList to List

How do I fix this?

1
  • You can if you have them both share the same super class. Your generics for the ArrayList should then contain that superclass and bam your done. Commented Feb 17, 2015 at 19:48

3 Answers 3

2

Create the following classes and interface:

public interface Marker {
}

public class CDMarker implements Marker {
    ...
}

public class PDLMarker implements Marker {
    ...
}

Use them in the following way:

//Create list
List<Marker> markers = new ArrayList<Marker>();

//Add objects
if(condition1)
    markers.add(new CDMarker());
else if(condition2)
    markers.add(new PDLMarker());

//Access elements
for(Marker m : markers) {
    if(m instanceof CDMarker)
        CDMarker cdMarker = (CDMarker)m;
    else if(m instanceof PDLMarker)
        PDLMarker pdlMarker = (PDLMarker)m;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, create a marker class and have CDMarker and PDLMarker inherit from that class. You can then loop through all your Marker objects in the way you listed.

EDIT: Here's a simple example I found. http://examples.javacodegeeks.com/java-basics/java-inheritance-example/

3 Comments

It is not necessary for Marker to be a class, an Interface should suffice. That is if the method declarations are the same in both classes (CDMaker and PDLMaker).
If I have CDMarker inherit from class Marker, is Marker = CDMarker correct statement?
@Nas: it would be with the keyword new: Marker a = new CDMarker();
0

You could have both CDMarker and PDLMarker extends the same super class Marker. Then declare the ArrayList as a superclass ArrayList, then loop through. In the loop, you could have different conditions to invoke different methods on the objects.

Marker marker1;                 //declare as superclass
if condition1
    marker1 = new CDMarker();   //assign to subclass object as needed
if condition2
    marker1 = new PDLMarker();  //assign to subclass object as needed

ArrayList<Marker> markers = new ArrayList<Marker>();
markers.add(marker1);           //add to subclass Arraylist
for (Marker marker : markers) { //loop through subclass Arraylist
       .....
}

Comments

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.