This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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
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 ]
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
Joined: Aug 17, 2004
Hi Timmy...I feel like an idiot now...BUT THANK YOU! I will remember it!
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.
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.