Results 1 to 5 of 5
  1. #1
    ShadowCopy is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Talking Searchable List Structure?

    Hello again,

    I've hit a bit of a snag again. This time I'm just not sure what kind of a structure I need to accomplish what I want to do, and I'm hoping you can help me. I don't mind researching how to use it, but I'm not really sure what I'm looking for.

    Basically I want something like a searchable list. It will act like a database within my program (without actually having to use a database, as right now I'm just learning and I'm desperately trying not to overcomplicate things lol).

    Example: If I created a circle class, and created 4 circle objects. Each circle object has a color attribute, and all four circles are different colors.

    I want to have a dynamic list of circle objects that will allow me to find a specific circle object by it's color. So if I wanted to see if the list contained a blue circle, I would search the list for a circle that had it's color attribute set to blue. In keeping with encapsulation, the color attribute would be private, and I would need to use public accessor methods to check if any of the circles are blue, and return the index of the circle that matches my query, or null/0/whatever if none matched the query.

    Is there such a structure? I've only really played with ArrayList up to this point, but if there is another that would be better suited to what I need I would love to know about it :)

    If my explanation is unclear, or if you simply need to know specifically what I need to use this for, I'll be happy to explain further.

    As always, I thank you very much for your time, understanding, and patience!

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You can still use the ArrayList for storing the circle objects. Then you would write a small static method that takes an ArrayList and a Color and just loop through the circle objects comparing their color with the supplied color.
    So what happens if there are many circles with the supplied color?

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    The method you use really depends on your exact requirements. If you have only 4 circles and each has a different colour, it will be simple and quick to iterate the list. If you had hundreds of circles, some blue, some red, etc., you'd want a different strategy, e.g. sorting. If every circle had a unique colour, you could use a sorted collection, or a map with colour as the key and circle as the value.

  4. #4
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default Sort?

    Of course, the answer depends on how and why you are searching. If you search the list once, a sort is O-n*ln(n) and still requires a linear search in O-n.

    For multiple searches, a sorted list would allow a binary search, or O-ln(n)

  5. #5
    ShadowCopy is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    So what happens if there are many circles with the supplied color?
    You kind of hit the nail on the head here. Here's basically what I'm trying to do:

    I'm writing a simple text adventure game. Each room is an object that holds an ArrayList of Objects (objects like a book, or a cup, or whathaveyou). These objects will have an identifier and a list of modifiers associated with them. For example, if there were a book object, the identifier might be "book", but there might be modifiers like "old", "dusty", "hardcover" etc.

    Therefore, if there happened to be two "books" in the room, and the player typed "examine book", then the game would search the room for all "book" identifiers. If it came across more than one, it would ask the player to specify which book by providing two modifiers to choose from.

    Eg

    An old dusty book lies here.
    A book on Java programming sits on the table.

    The player types "examine book"

    Output: Which book? The old book or the Java book?

    The player types "examine Java book"

    Output: <the Java book's long description>

    I think I could still use the static method r03 mentioned, but I would have to search the entire list of objects and figure out how many objects have that particular identifier (if the player doesn't include a modifier; If he/she does provide one, then I can search for the modifiers instead of the identifiers, which hopefully should be unique) and if there's more than one, alert the user to the ambiguity and ask them to clarify (by using a modifier).

    I think I got an idea now. Thanks so much for your responses. However if you see a flaw in my plan or think you have a better idea, by all means please share :D

    Thanks so much again,

    Your humble noob,
    Shadow
    Last edited by ShadowCopy; 08-30-2009 at 12:11 AM. Reason: Used wrong word - made things even more confusing lol

Similar Threads

  1. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  2. How to access ArrayList in List of List?
    By alvations in forum New To Java
    Replies: 5
    Last Post: 10-08-2008, 12:23 PM
  3. Doubly-linked list with data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:30 PM
  4. Use if then else structure, help
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:00 AM
  5. Help with Structure
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •