From 68509796a995e83748030427ef99d685a62ec8a9 Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 21 Sep 2001 01:34:17 +0000 Subject: [PATCH] Added indexOf() and indexOfEqual(). git-svn-id: https://samskivert.googlecode.com/svn/trunk@329 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ListUtil.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/ListUtil.java b/projects/samskivert/src/java/com/samskivert/util/ListUtil.java index 62f93fcc..24155a74 100644 --- a/projects/samskivert/src/java/com/samskivert/util/ListUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/ListUtil.java @@ -1,5 +1,5 @@ // -// $Id: ListUtil.java,v 1.3 2001/08/15 18:02:26 mdb Exp $ +// $Id: ListUtil.java,v 1.4 2001/09/21 01:34:17 mdb Exp $ package com.samskivert.util; @@ -195,6 +195,49 @@ public class ListUtil return false; } + /** + * Looks for an object that is referentially equal to the supplied + * element (list[idx] == element) and returns its index + * in the array. Passing a null element to this function + * will cleverly tell you whether or not there are any null elements + * in the array which is probably not very useful. + * + * @return the index of the first matching element if one was found, + * -1 otherwise. + */ + public static int indexOf (Object[] list, Object element) + { + int llength = list.length; // no optimizing bastards + for (int i = 0; i < llength; i++) { + if (list[i] == element) { + return i; + } + } + return -1; + } + + /** + * Looks for an element that is functionally equal to the supplied + * element (list[idx].equals(element)). Passing a null + * element to this function will call + * equals(null) on all objects in the list which may + * cause them to choke, so don't do that unless you mean it. + * + * @return the index of the matching element if one was found, -1 + * otherwise. + */ + public static int indexOfEqual (Object[] list, Object element) + { + int llength = list.length; // no optimizing bastards + for (int i = 0; i < llength; i++) { + Object elem = list[i]; + if (elem != null && elem.equals(element)) { + return i; + } + } + return -1; + } + /** * Clears out the first element that is referentially equal to the * supplied element (list[idx] == element). Passing a