T
Tom Anderson
Mike said:Lew said:Roedy Green wrote:
Okay, so maybe we can have a hybrid approach that accumulates large
strings in the list, but when fed short strings or individual
characters, puts them into a buffer
I was thinking along those lines last night. I occurred then that my
pieces ArrayList would contain both Strings and char[]s.
I would need an ArrayList<Object> to contain both. I don't think
there as a way te specify an ArrayList of either String or char[].
Object is the only class they have in common.
Since char[] is not a fully fledged class, there is no hope of it
implementing some common interface with String.
Technical nit: arrays are "fully fledged" classes.
You know what Roedy means, though. It isn't possible for char[] to
implement an interface that Object[] does not. Though creating a class
that contains a (possibly mutable) char[] and implements CharSquence is
trival, and creating a new instance of it is almost free. Having done
so, you can use ArrayList<CharSequence>.
It seems to me that what you need is a class that wraps a char[],
handles appends, and implements CharSequence. StringBuilder does all
those things.
Append for a long String would unconditionally add it to the List. For a
short String, it would check the last element in the List. If it is a
reference to a short String, replace it with a StringBuilder containing
it. If, after doing that, the last element is a StringBuilder, append
the short String parameter to it. If not, add the String to the List.
The result would be a mixture of String objects representing long
strings and isolated short strings, with StringBuilder objects each
representing two or more consecutive short string appends.
Nice one!
tom
--
The other big thing is the method by which these new discoveries had
been made. They had not been made in studies. They were not made by
the ransacking of ancient texts. Nobody deduced the existence of Nova
Scotia. These things were discovered by the very simple process of
driving a ship into them. A ship is a form of scientific instrument. --
Allan Chapman