How to create a mixed data buffer in Java?
I need to create a buffer which contains byte and string data. How can I do开发者_开发百科 this in Java?
you can use plain array of objects Object[] or any implementation of collections (List, Set) etc.
But why? If you want to store different types of data in the same collection, check your design. If you still wish to do this create wrapper interface and as many as you want implementations. Each implementation will store its type if data. Then create collection that stores these wrappers:
interface StorageCell<T> {
}
class StringCell implements StorageCell<String>{}
class ByteCell implements StorageCell<Byte>{}
and finally the buffer is:
Collection<? extends StorageCell> buffer = new LinkedList<StorageCell>();
精彩评论