Added compact().

git-svn-id: https://samskivert.googlecode.com/svn/trunk@649 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-03-14 03:48:44 +00:00
parent 18775ed66b
commit 40e0623806
@@ -1,5 +1,5 @@
//
// $Id: IntListUtil.java,v 1.1 2001/11/06 02:13:29 mdb Exp $
// $Id: IntListUtil.java,v 1.2 2002/03/14 03:48:44 mdb Exp $
package com.samskivert.util;
@@ -261,6 +261,32 @@ public class IntListUtil
return val;
}
/**
* Converts a sparse array (with zero-valued entries) into a compact
* array (where all elements contain non-zero values) with ordering
* preserved.
*/
public static int[] compact (int[] list)
{
int llength = list.length, lcount = 0;
// count up the non-zero entries
for (int i = 0; i < llength; i++) {
if (list[i] != 0) {
lcount++;
}
}
int[] nlist = new int[lcount];
int n = 0;
for (int i = 0; i < llength; i++) {
if (list[i] != 0) {
nlist[n++] = list[i];
}
}
return nlist;
}
/**
* Creates a new list that will accomodate the specified index and
* copies the contents of the old list to the first.