Results 1 to 17 of 17
  1. #1
    Proshitness is offline Member
    Join Date
    Oct 2011
    Posts
    28
    Rep Power
    0

    Default Something Like A List.

    Hi.
    I am designing a level loader and I need something which can hold two elements the name of the object, and its serial so:
    I need something that is similar to a List \ ArrayList, but can get an undefined number of elements i.e: instead of ArrayList<X>, ArrayList<X,Y,Z,K>
    thats just an example, I know that I can't do that with an arraylist or a list, the most that I found was SortedList which can hold 2 elements
    thanks.
    Last edited by JosAH; 06-15-2012 at 04:21 PM. Reason: removed the boldface tags.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default Re: Something Like A List.

    Can you explain what is "undefined"
    An ArrayList can hold an undefined number of elements.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Something Like A List.

    Maybe HashMap is what you are looking for.
    Map<String, List<String>> m = new HashMap<String, List<String>>();


    Ksharp

  4. #4
    jitu.ideas2it is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Something Like A List.

    I Think This Is the Best Idea For Ur Requirement

    Map<String,Object> unlimitedMap=new HashMap<String,Object>();

    If U Want To Put In List U Can Also Like This

    List<Map<String,Object>> listOfMap=new ArrayList<Map<String,Object>>();

    may be This Thing Help full To You

  5. #5
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Something Like A List.

    You're trying to store tuples in a List, right? So something like
    List<int xLocation, int yLocation, double Elevation, double Temperature>
    For a series of x- and y-coordinates?

    Java doesn't do tuples. You have to write a class that acts as a C-struct essentially. So instead you have
    List<NewPointClass>
    where NewPointClass has
    int xLocation, int yLocation, double Elevation, double Temperature
    as fields.

  6. #6
    Proshitness is offline Member
    Join Date
    Oct 2011
    Posts
    28
    Rep Power
    0

    Default Re: Something Like A List.

    Quote Originally Posted by Norm View Post
    Can you explain what is "undefined"
    An ArrayList can hold an undefined number of elements.
    i.e:
    ArrayList holds 1 generic elements(ArrayList<E>), while a SortedMap holds 2 generic elements (SortedMap<K,V>)

    Quote Originally Posted by Diargg View Post
    You're trying to store tuples in a List, right? So something like
    List<int xLocation, int yLocation, double Elevation, double Temperature>
    For a series of x- and y-coordinates?
    Java doesn't do tuples. You have to write a class that acts as a C-struct essentially. So instead you have
    List<NewPointClass>
    where NewPointClass has
    int xLocation, int yLocation, double Elevation, double Temperature
    as fields.
    umm not excatly, but if you can explain what do you mean with the NewPointClass I think that will help me.
    EDIT:
    did you mean something like this:
    Java Code:
    public class Tuple<X, Y> { 
      public final X x; 
      public final Y y; 
      public Tuple(X x, Y y) { 
        this.x = x; 
        this.y = y; 
      } 
    }

  7. #7
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Something Like A List.

    How many generics do you want to hold? You need to define your specs better, or else you're defeating the point of OOP.

    The code you posted won't even compile - you're assigning to a final variable.

    I meant something like this:
    Java Code:
    public class MyData { 
      private int x, y; 
      private double temperature, elevation; 
      public MyData(int x, int y, double e, double t) { 
        this.x=x;
        this.y=y;
        this.elevation=e;
        this.temperature=t;
      } 
    }
    If "undefined" means "variable length", then you'll probably end up making a list of lists. Unless that reflects what you're modeling, it's a bad idea.

  8. #8
    Proshitness is offline Member
    Join Date
    Oct 2011
    Posts
    28
    Rep Power
    0

    Default Re: Something Like A List.

    I need it to hold 6 generics.

  9. #9
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Something Like A List.

    "Undefined" = 6, good to know.

    And you have no idea what they'll end up being? Not even a parent class?

    Will it at least always be <ClassA, ClassB, ..., ClassF>, even if you don;t know what ClassA will be until runtime?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default Re: Something Like A List.

    Sounds like the OP whats to go back to pre-Generics and use casting and instanceof for getting objects out of the collection.

    How will the compiler be able to control the type of what is returned by the get() method? There can only be one type.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Proshitness is offline Member
    Join Date
    Oct 2011
    Posts
    28
    Rep Power
    0

    Default Re: Something Like A List.

    Quote Originally Posted by Diargg View Post
    "Undefined" = 6, good to know.

    And you have no idea what they'll end up being? Not even a parent class?

    Will it at least always be <ClassA, ClassB, ..., ClassF>, even if you don;t know what ClassA will be until runtime?
    I look at it again and I found that I only need 6 generics for all the data to be stored.
    not sure what you mean but "even if you don;t know what ClassA will be until runtime", but yes it will always be the same
    <Name, Serial,X,Y,Type, Can interact with player>

  12. #12
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Something Like A List.

    Quote Originally Posted by Norm View Post
    Sounds like the OP whats to go back to pre-Generics and use casting and instanceof for getting objects out of the collection.
    Mmm, the Dark Times. Perhaps one day Java can even step forth into the Enlightened Era of the Snake et al. and have objects remember their classes when they go into a collection...


    Quote Originally Posted by Proshitness View Post
    I look at it again and I found that I only need 6 generics for all the data to be stored.
    not sure what you mean but "even if you don;t know what ClassA will be until runtime", but yes it will always be the same
    <Name, Serial,X,Y,Type, Can interact with player>
    Sounds ripe for a class-based representation.

    Maybe a parent class that contains Name, serial, x,y, type is defined by the subclass, as is interaction?
    Last edited by Diargg; 06-15-2012 at 03:25 PM.

  13. #13
    Proshitness is offline Member
    Join Date
    Oct 2011
    Posts
    28
    Rep Power
    0

    Default Re: Something Like A List.

    Quote Originally Posted by Diargg View Post
    Mmm, the Dark Times. Perhaps one day Java can even step forth into the Enlightened Era of the Snake et al. and have objects remember their classes when they go into a collection...




    Sounds ripe for a class-based representation.

    Maybe a parent class that contains Name, serial, x,y, type is defined by the subclass, as is interaction?
    Sorry but I don't understand what do you mean by "parent class that contains Name, serial, x,y, type is defined by the subclass"

  14. #14
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Something Like A List.

    What are you modeling? Something in a game, some kind of object, right?

    Bust out the Animal analogy!

    All Animals have a name, a location (x,y), a serial number, I guess. An abstract function in it ( public boolean canInteract() ) requires all subclasses to decide whether they can or not.

    You store a ton of Animals in your list, and then call their respective functions when the time comes to use them.

    Java Code:
     Dogs     can    interact with players, because they're our friends.
    (Type)(boolean)
    So Dog would extend Animal, with public boolean canInteract() returning true;

    This may help

  15. #15
    Proshitness is offline Member
    Join Date
    Oct 2011
    Posts
    28
    Rep Power
    0

    Default Re: Something Like A List.

    ohhh (xD) ok I get it now, so my parent class (for animal) will be something like this:
    Java Code:
    public class Animal{
    public int x;
    public int y;
    public int serial;
    public String name;
    public boolean canInteract;
    public String type;
    
    public Animal(int x, int y, int serial, String type){
    this.x = x;
    this.y = y;
    this.serial = serial;
    this.type = type;
    }
    something like this?

  16. #16
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Something Like A List.

    I don't know what serial or type represent, but that's a start. Work with it and see what you end up with.

  17. #17
    Proshitness is offline Member
    Join Date
    Oct 2011
    Posts
    28
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 09:25 PM
  2. Replies: 8
    Last Post: 11-04-2011, 08:30 PM
  3. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  4. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  5. Replies: 3
    Last Post: 11-21-2009, 05:48 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
  •