Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Cool Hey! ArrayList problem here

    Hey alright heres what im trying to accomplish.

    I want to be able to add strings to an arraylist and then give them a numerical value.
    Like:

    int coolness;
    static ArrayList<String> people = new ArrayList<String>();
    people.add(sam);


    and then something like
    people.sam.coolness = 999999;


    How is this accomplished? thanks!
    Sam

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    whay don't you just use Vector() or a simple array for this ?
    int [] array=new int[index];
    Vector vector=new Vector();

  3. #3
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Yea alright thats what i was looking for a simple solution thanks..
    So how is it used sorry ive never used a vector and im still learning arrays..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Webuser View Post
    whay don't you just use Vector() or a simple array for this ?[/B]
    How is a Vector better than an ArrayList? Especially when Sun is recommending that you not use Vector, that instead you use the newer classes found in the Collections framework such as ArrayList?

    The solution is not to store an ArrayList of String but an ArrayList of objects. The Object stored should be able to hold a String and an int and whatever other data is needed to be logically held together.

    e.g.,
    Java Code:
    class CoolPeople
    {
      private String name;
      private int coolness;
    
      public CoolPeople(String name, int coolness)
      {
         this.name = name;
         this.coolness = coolness;
      }
    
      // usual getters, setters, equals and hashCode overrides,...
    }
    Java Code:
    List<CoolPeople> coolPeopleList = new ArrayList<CoolPeople>();
    coolPeopleList.add(new CoolPeople("Sam", 9999));
    // ... etc...

    Webuser -- what's going on with your answers?
    Last edited by Fubarable; 02-17-2009 at 06:05 AM.

  5. #5
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    So would i do it like this?

    ArrayList people = new ArrayList();
    people.add(Sam, 9999);

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I would use a generic arraylist and a new class as I demonstrated above.

  7. #7
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Alright the second one was what i was looking for totally awsome thanks..

  8. #8
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    just one thing..what are the commands like contains and add remove.
    how do i check the int of sam?
    something like sam.getValue();

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    the "Second one"? I'm not sure what you meant by that, but you're welcome.

  10. #10
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    and can i do like
    sam += 1;
    or
    CoolPeople.sam(++1);

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Samgetsmoney View Post
    just one thing..what are the commands like contains and add remove.
    You'll need to learn to search and love the API as it will give you all the methods of ArrayList and List that you'll need. It may seem confusing at first, but the more you study it, the more familiar you'll be with it and the more helpful it will become.

    how do i check the int of sam?
    something like sam.getValue();
    You'll have to create a class to hold this information as well as have getter and setter methods as I hinted at in my example above.

  12. #12
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Could you give me a website?
    It would be a nice favor just save me some time?
    Or would wikipedia work?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One of the best websites I know of are the Sun tutorials: The Java™ Tutorials

  14. #14
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    ok it says nothing about adding or how to tell wich has the biggest amount of coolness

  15. #15
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    err... you have to write those codes yourself...
    like fubarable's first post above. #scroll up#
    Java Code:
    public int getCoolness(){
      return this.coolness;
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No site will have these specifics on it, but the site I linked, if studied diligently will teach you basic Java, and with that you can figure out how to code any specifics you desire. In other words it's up to you. Best of luck.

  17. #17
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable
    ...and love the API ...
    It is a really good advice )))

  18. #18
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by Samgetsmoney View Post
    Yea alright thats what i was looking for a simple solution thanks..
    So how is it used sorry ive never used a vector and im still learning arrays..
    oh, it is quite simple to use vector ) java.util.*; lib...
    vector it is useful thing because contains objects and more over you don't need to put a predictable number of it's elements like [n].
    you just write
    Vector vector=new Vector();
    and then you can add and set and remove its elements. Some really don't like using it but it is quite useful )

  19. #19
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by Samgetsmoney View Post
    just one thing..what are the commands like contains and add remove.
    how do i check the int of sam?
    something like sam.getValue();
    Vector sam=new Vector();
    sam.elementAt(,);
    Last edited by Webuser; 02-18-2009 at 06:42 AM.

  20. #20
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    So i can do?
    Vector people = new Vector();
    people.addElement(sam, 200);
    people.getElementAt(sam);
    and that will show 200?


    and then do
    people.addElement(mike, 200);
    people.element(sam, ++100);

    And something like
    people.sort();
    So it orders the person with the greatest amount first.
    so if i did print("The winner is: " + people.elementAt(1));
    it would print "The winner is: sam"
    Last edited by Samgetsmoney; 02-17-2009 at 10:35 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Arraylist
    By gnarly hogie in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 02:59 AM
  2. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  3. ArrayList problem with images
    By Cymro in forum New To Java
    Replies: 2
    Last Post: 02-05-2008, 07:22 PM
  4. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 06:46 AM
  5. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 01:47 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
  •