This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Traversing an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Traversing an ArrayList" Watch "Traversing an ArrayList" New topic
Author

Traversing an ArrayList

Ranadhir Nag
Ranch Hand

Joined: Mar 09, 2006
Posts: 138
I have an arraylist of strings.
Within that,I have to find an entry which contains a particular substring.
ArrayList 'contains' does not allow regular expressions - is there some way to retrieve the appropriate element without actually traversing through each entry?
Also,when iterating through the entries(if at all required) ,is it possible to retrieve the index from the iterator?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

I don't know of any method in the standard Java API that does exactly what you need. It's ofcourse easy to write a utility method which loops through the array and checks every string until it finds a string that matches. There's no way to do this without traversing the array. Note that the 'contains' method of ArrayList also traverses the array behind the curtains.

To get the index, you could use ListIterator instead of a plain Iterator. ListIterator contains methods 'nextIndex()' and 'previousIndex()', see the API documentation for details. To get a ListIterator instead of a plain Iterator from your ArrayList, call the method 'listIterator()' instead of 'iterator()' on the ArrayList.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I'm posting this just for the fun of it. I'm using Jakarta's commons Collection library.



[My Blog]
All roads lead to JavaRanch
Jeff Hemminger
Greenhorn

Joined: Jun 15, 2007
Posts: 7
The arraylist method contains overrides AbstractCollection.contains. In the API, AC contains states:
"Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element."

What this means to you is that the contains method is already doing the iteration work for you, and is using obj1.equals(obj2).

Therefore, I would suggest creating your own FancyString object that extends String and overrides equals to perform the regex that you'd like to perform.


<a href="http://ehemminger.org" target="_blank" rel="nofollow">http://ehemminger.org</a><br /><a href="http://reverttoconsole.com" target="_blank" rel="nofollow">http://reverttoconsole.com</a>
Jeff Hemminger
Greenhorn

Joined: Jun 15, 2007
Posts: 7
oops, probably don't want to try and "extend String" since it's a final class. :-)

So, my solution would look something like this:

public class MyString {

private String myString;
private Pattern myPattern;

public MyString( String inputString, Pattern inputPattern ) {
this.myString = inputString;
this.myPattern = inputPattern;
}

public boolean equals( Object anObject) {
//use this to perform your regex

}

/* setters and getters */
}

Use a class like this instead of a String class in the ArrayList.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Jeff, that's not a very elegant solution, and I don't think it is going to work.

Think about this: What would the implementation of the equals(...) method in class MyString look like? Remember that the object passed to it is another MyString object. Note that the equals() method should be implemented conform to what is described for Object.equals() in the API documentation. How are you going to compare those two MyString objects so that it will work according to what Ranadhir needs?

Also, probably Ranadhir is getting that array of strings from somewhere else in his software, that doesn't know about the MyString class. Now, he could first convert the array of strings to an array of MyString objects, but that would be very cumbersome. He could also use MyString in his software everywhere instead of the standard String, but that is strange and you'll loose all the benefits of the standard String class, including the special optimalisations that the Java compiler performs for strings.

[ June 15, 2007: Message edited by: Jesper Young ]
[ June 15, 2007: Message edited by: Jesper Young ]
Jeff Hemminger
Greenhorn

Joined: Jun 15, 2007
Posts: 7
Jesper,
Thanks for your comments.
For the most part, you are correct.
If I were in Ranadhir's situation, I would first look for a commons collection solution because they often have exactly what I'm looking for, maybe something akin to Satou's solution.
If not, I would probably just iterate through the ArrayList, or secondarily implement your suggestion which was to write a utility class or method to save from writing the loop several times.
It would be ridiculous to use this kind of custom string-like object throughout an application, and I concede that in a software system you're likely to receive an ArrayList of Strings intact and would not want to convert all Strings to this type of object.
And for that, I'm sorry, I should have elaborated my intentions regarding this solution before posting it.
So what I should have said is , if you really do not want to manually iterate over an ArrayList and want to make use of a regex in the contains method, and you have no concern for anything else and are programming in your own little ivory tower, you can do something like below...



Obviously, ostentatious concerns about elegance, the founding fathers desires for use of the equals() method, and String optimalisations do not come in to play here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Traversing an ArrayList
 
Similar Threads
How to remove an element from LinkedList while traversing
changing count traversing items
Iterator for ArrayList and LinkedList
Type casting
ArrayLists and Submit