Additions to the Collections API
Hi,
Just had an idea for some useful additional methods for the Collection interface. Are such things in the pipeline? If not, is there a way to formally request them?
publicinterface Collection<E>extends Iterable<E>{
publicboolean addAll(E... o);
publicboolean addAll(int index, E... o);
}
publicclass ArrayList<E>implements List<E>{
public ArrayList(E... o){
super();
addAll(o);
}
}
And probably others of a like nature, using the new "..." notation for adding as many things as you want to a method call.
Allows you to instantiate a list where you know the members in much the same way you would an array - instead of
String[] letters =new String[]{"A","B","C"};
you would have
List<String> letters =new ArrayList<String>("A","B","C");
I've created my own subclass of ArrayList to let me do the constructor, but it looks to me like a no-lose addition to the API - can't break any existing code.
Yes, but I think these versions are syntactically nicer than
List<String> list = Arrays.asList(new String[] {"A", "B", "C"});
which looks overly busy and non-intuitive to me.
Thanks for the pointer to where to request such things.
An ArrayList is a good, simple and staightforward implementation of a stack (having nothing to do with java.util.Stack), except for some trivial missing methods.
java.util.Stack is based on an quite obsolet class - Vector, which was later quite doubled by a better one: ArrayList - this is hardly a good API design, but was necessary. Simply said: Vector is quite useless, it's just a sort of beta version of ArrayList. And so is Stack for the very same reasons (it's synchronized etc.).
Moreover, I do not speak about providing a new class. Just about implementing all the methods making good sense for an existing one.
Implementing a LIFO/Queue is something completelly different, it can't be done with an ArrayList without substantial overhead. There's already a nice implementation of Queue in java.util.ArrayDeque, but this is no simple extension of ArrayList.
If there is a push, why there is no pop?
I see no error in std::vector - IMHO it's perfect: it implements every usable method you can get without additional overhead, and it does not implement anything superfluos or hard to use.
I can't say much about c++ class libraries in general, and nearly nothing about iostreams, but I know c++ containers quite good and I like them very much.
I do not like java.io at all: I see no reason for the existence of both PrintStream and PrintWriter, I hate "platform's default character encoding" being the default, and I see absolutely no reason for plattform dependent java.io.File (it IS dependent, there're other languages/tools which do not bother me with "/" vs. "\" and other meaningless details).
Then you'd hate iostreams, which are a deadly mixture of input, output, formatting, and a blatant abuse of operator overloading, implemented in an almost completely non-extensible way.
BTW File is platform-independent if you restrict yourself to '/'. It fixes all that nonsense for you.
ejpa at 2007-7-14 20:47:44 >
