Improved List initialisation

Richard Warburton richard.warburton at gmail.com
Tue Sep 11 21:40:27 UTC 2007


I hope this is the correct mailing list to discuss such an idea,
please correct me if I'm wrong, but core-libs sounded like it included
collections api.

Within Java we have a sugared syntax for initialising arrays, so for
example we can write this:

int[] intArray = {1,2,3};

However, list initialisation requires a very handed approach, for example:

List<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(3);

Certain other languages have some sugar for this kind of thing, the
general jist of:

List l = [1,2,3];

In java we could get a similarly concise effect, with no hacking in
syntactic sugar to solve these problems, I am thinking of a syntax
along the lines of:

List<Integer> intList = new ArrayList<Integer>(1,2,3);

This could be trivially implemented as such:

public ArrayList(T ... args) {
	for (T t : args) {
		this.add(t);
	}
}

The same approach could be used for Vector and the Set collections classes

I am also not aware of the process of proposing library changes, eg
would I need to do something in conjunction with the JCP?  Perhaps
someone more knowledgable could advise me.  Awaiting commentary with
interest.

  Richard Warburton



More information about the core-libs-dev mailing list