ClustrMaps

The roadrunner is back as never before! See also: My homepage or my very obsolete site
If by any means entries in my blog are considered to be harmful or damaging, please let me know (add comment) or just mail me. In this (unhopely) case I will remove or change the contents.

Friday, February 02, 2007

Today I saw this for the first time (from AbstractList):
public boolean addAll(int index, Collection<? extends E > c) {
boolean modified = false;
Iterator<? extends E > e = c.iterator();
while (e.hasNext()) {
add(index++, e.next());
modified = true;
}
return modified;
}

This is Java 1.5 , also known as 5.0.
This is about parameterized types and variable arguments.
See: http://www.velocityreviews.com/forums/t152251-method-dynamic-parameter-lists.html

public static Vector<integer> getParameter(int ... params) {

Vector<integer> v = new Vector<integer>();

for (int cnt = 0; cnt < params.length; cnt++) {
v.add(new Integer(params[cnt]));
}
return v;

}

See also: http://today.java.net/pub/a/today/2004/04/19/varargs.html
Class [] argTypes =
{
String.class,
int.class
};

/* Note: Java 1.5's autoboxing language feature lets
you replace "new Integer (20)" with "20". The
compiler converts "20" to "new Integer (20)".
This code fragment assumes an earlier version
of Java -- which is why "new Integer (20)" is
specified instead of "20".
*/
Object [] methodData =
{
"A",
new Integer (20)
};
Class c = Class.forName ("SomeClass");
Method m = c.getMethod ("someMethod", argTypes);
m.invoke (c.newInstance (), methodData);

In contrast, variable arguments let you express the code fragment above more compactly, as follows:

Class c = Class.forName ("SomeClass");
Method m = c.getMethod ("someMethod", String.class, int.class);
m.invoke (c.newInstance (), "A", 20);

You save keystrokes and the code is much easier to read.

No comments: