1

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:

  1. "The "
  2. [Foo] Object
  3. " ran up the tree and fought with the "
  4. [Bar] object
  5. 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.

5
  • 2
    Why would you want a list like that? Commented Apr 16, 2014 at 6:01
  • ArrayList<String> cannot be used to hold Object, it only can hold String object. That is a limitation by design in java. Commented Apr 16, 2014 at 6:06
  • why bother using generics then? Commented Apr 16, 2014 at 6:07
  • @BheshGurung I basically 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." An alternative approach I thought was tracking object positions by index and length in a string. Commented Apr 16, 2014 at 6:07
  • On the basis of that clarification, the answer from طاهر is probably the right approach. Commented Apr 16, 2014 at 6:19

4 Answers 4

3

a simple solution is to create a wrapper for String that implements the MyInterface. like:

public class MyString implements MyInterface {
  private String string;
....
}

and then add an instance of MyString to the list.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice one. I would suggest to let the class MyString also implement the interface CharSequence, as this might come in handy.
3

If you are going to have a collection that could hold instances of String, instances of Foo, and instances of Bar, then - yes - there is no other possibility to have a Collection<Object>, as these three types have nothing in common.

The reason is, that you indeed have a very heterogenous collection. Declaring it as Collection<Object> expresses what you have and what you want.

Note, that you thought into the right direction: The type parameter for your collection should be the closest type that they have together. So, if you have your classes Foo and Bar implement the interface MyInterface, and you are planning to only hold instances of these two types, then you could have a Collection<MyInterface>.

You cannot have the String type implement your interface.

Comments

1

If you want to store Strings in the same ArrayList with your custom objects it means your classes works with string too no? Implement CharSequence in your classes.

And methods of CharSequence which you don't need throw MethodNotImplementedException.

1 Comment

I think technically this may accomplish what I want, but it just wouldn't make sense to a reader because the type CharSquence isn't really what the ArrayList object is supposed to represent. I've added details to the original question about my specific problem.
-1

ok,I think you have not understand the purpose of Generic Type of Java clearly.Generic type is to define element type explicitly,it means,only when your collection only contains some type element,Generic type can help you to aviod ClassCastException.And now,absolutely there three types in your collection.so it's not suitable to use Generic Type.you can use No Generic type simply,like

@SuppressWarnings("rawtypes")
ArrayList arrayList=new 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.