aspose file tools*
The moose likes Beginning Java and the fly likes functions contains for array ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "functions contains for array ?" Watch "functions contains for array ?" New topic
Author

functions contains for array ?

Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 107
Is there existing function or easy way to check if the object is included in array, like contains of arraylist? I know for loop can do it, but tired to insert the for loop in so many places. Hope to get some quick method or existing one?

or if there is quick way to convert array to arraylist without using the for loop.

Thanks,
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your for loop is proabably about as good as it gets. If you are troubled by coding the same thing more than once, congratulations on good instincts! You could make a utility class with a static method to find any object in any array. You'd need additional methods for any primitives you want to seek.

I thought Arrays might have something, but it only has binary search on sorted arrays. But is also has a sort method so you could sort your array! You might time the actual overhead of creating a list from your array so you can use the nicer List APIs. I'd probably do that until somebody proves it's too slow, whicih I wouldn't expect unless you have zillions of items or call this a million times a second. Of course you could maybe change your design to use Lists in the first place
[ May 09, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 144
Hi Steve,
I am not sure there is a way to convert an array into an ArrayList, but insted of using a lot of for loops all over your code, you can simply create your own "contains" method and use it instead of cloning the for loops. Or you can convert your arrays into arrayList.
You can do something like:


Is this helpful?

Giovanni
[ May 09, 2005: Message edited by: Giovanni De Stefano ]

SCJP 1.4
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Hi Stan and Giovanni,

Arrays does have a method. Check out Arrays.asList(Object[] a)



From the javadoc:


asList
public static List asList(Object[]´┐Ża)
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray.
The returned list is serializable and implements RandomAccess.
Parameters:
a - the array by which the list will be backed.

Returns:
a list view of the specified array.

See Also:
Collection.toArray()


By the way, I totally agree with Stan James. When you type something for the third time, it's time to refactor!
[ May 09, 2005: Message edited by: Timmy Marks ]
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 144
Hi Timmy...I feel like an idiot now...BUT THANK YOU!
I will remember it!

Giovanni :-)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

It is actually an interesting implementation, the list that is create is backed by the array. That means operations that can change the size of the array, such as add() or remove() will throw an unsupported exception. But everything else works, including the iterator.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Another option is to consider using Jakarta Commons Lang library. Their org.apache.commons.lang.ArrayUtils class has static contains() methods for each of the primatives as well as Object. You simply pass in the array and the item you're looking for and it returns true or false.

Of course, the implementation is pretty much the same thing that you might do in your code with a for loop or the same as a util library you might create, it's just already done and contained within Commons Lang. I've found a number of their helper utilities to be very convenient.

Hope that's helpful.
Josh


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: functions contains for array ?