Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-16-2008, 06:02 PM
Member
 
Join Date: Jan 2008
Posts: 16
newtojava7 is on a distinguished road
recursively searching through arraylists
hello,

i have a bunch of strings in an arraylist, which im displaying in a JList. i have a textfield with a search button which allows the user to type in a string into the textfield and search for that string within the arraylist. i need this search to be done recursively, how can I do this? thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-17-2008, 03:36 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
import java.util.*; import java.util.List; public class SearchingTest { static String[] items = { "London", "Manchester", "Cardiff", "Bristol" }; static List<String> list = Arrays.asList(items); public static void main(String[] args) { String s = //items[2]; "Middlesex"; int index = search(s, 0); String result = (index == -1) ? "not found" : "found at index " + index; System.out.println(s + " is " + result); } private static int search(String target, int n) { if(n > list.size()-1) return -1; if(list.get(n).equals(target)) return n; else return search(target, n+1); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Map implemented with ArrayLists Java Tip java.lang 0 04-16-2008 11:29 PM
arraylists problem newtojava7 New To Java 1 03-12-2008 08:38 AM
Computing Fibonacci numbers recursively Java Tip Java Tips 0 01-22-2008 09:20 PM
searching nalinda New To Java 3 12-06-2007 03:56 AM
How do you recursively traverse through file folders ? nimraj New To Java 2 11-27-2007 02:45 PM


All times are GMT +3. The time now is 01:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org