I have two classes, let's call them Foo and Bar.
public class Foo {
//...
}
public class Bar {
//...
}
I want to create an ArrayList that can handle Foo, Bar, and String types. Normally, I would just apply an interface to my classes, i.e.
public class Foo implements MyInterface {
//...
}
public class Bar implements MyInterface {
//...
}
And then I could just use ArrayList<MyInterface>. However, I'm struggling with how to implement this interface for a String. Am I stuck in using ArrayList<Object> or is there a another more preferred way that I should go about accomplishing this task?
Edit: Adding some clarity to the actual problem that I have:
I want to create a sentence that has objects embedded inside of it.
I.e. "The [Foo] ran up the tree and fought with the [Bar] while a gang of [Foo]'s watched."
So the ArrayList could be constructed and decontsructed by its contents which would be indexed in order like:
- "The "
- [Foo] Object
- " ran up the tree and fought with the "
- [Bar] object
- etc.
An alternative approach I have thought about was tracking object positions by index and length in a string. So I'd have a String for the whole sentence, and another property that tracked the position/length of any MyInterface objects.