From 262f10c8c2937694169b4896a04a08b2b85af2cb Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 27 Sep 2001 23:13:24 +0000 Subject: [PATCH] Added toIntArray() which converts the contents of a collection to an int array. git-svn-id: https://samskivert.googlecode.com/svn/trunk@332 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/CollectionUtil.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/CollectionUtil.java b/projects/samskivert/src/java/com/samskivert/util/CollectionUtil.java index b136e203..f7bc4f72 100644 --- a/projects/samskivert/src/java/com/samskivert/util/CollectionUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/CollectionUtil.java @@ -1,5 +1,5 @@ // -// $Id: CollectionUtil.java,v 1.2 2001/08/11 22:43:29 mdb Exp $ +// $Id: CollectionUtil.java,v 1.3 2001/09/27 23:13:24 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -47,4 +47,27 @@ public class CollectionUtil col.add(iter.next()); } } + + /** + * If a collection contains only Integer objects, it can + * be passed to this function and converted into an int array. + * + * @param col the collection to be converted. + * + * @return an int array containing the contents of the collection (in + * the order returned by the collection's iterator). The size of the + * array will be equal to the size of the collection. + * + * @exception ClassCastException thrown if the collection contains + * elements that are not instances of Integer. + */ + public static int[] toIntArray (Collection col) + { + Iterator iter = col.iterator(); + int[] array = new int[col.size()]; + for (int i = 0; iter.hasNext(); i++) { + array[i] = ((Integer)iter.next()).intValue(); + } + return array; + } }