Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Arrays of Objects

  1. #1
    superscout is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Arrays of Objects

    How does one go about using an array of objects? The array has been set up as:

    Java Code:
    ArrayList<Item> SortedList = new ArrayList<Item>();
    With Item being a class name(string) quantity(int) cost(double) and size(double)

    What I want to know is how do I add objects to the array and then how can I sort it based on the data in the objects

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Arrays of Objects

    You should start off by reading all the methods in the ArrayList() class.
    ArrayList (Java Platform SE 6)

    And when you have your objects in your array, you could sort them by implementing your own sorting method and sort each one by accessing a certain member of each of your objects.
    Last edited by jammas615; 01-15-2012 at 02:02 PM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays of Objects

    Quote Originally Posted by jammas615 View Post
    And when you have your objects in your array, you could sort them by implementing your own sorting method and sort each one by accessing a certain member of each of your objects.
    That's not needed, i.e. make the Item class implement the Comparable<T> interface and the Collections.sort() method can do the job.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to use class objects in arrays
    By dironic88 in forum Eclipse
    Replies: 6
    Last Post: 04-06-2011, 01:34 PM
  2. scanner objects with arrays again..
    By nevermiind in forum New To Java
    Replies: 18
    Last Post: 05-24-2010, 11:45 AM
  3. Objects and Arrays
    By bannow in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 07:15 PM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. [SOLVED] Arrays of Objects with Subscripts
    By Sidmyre in forum New To Java
    Replies: 5
    Last Post: 12-12-2008, 02:18 AM

Posting Permissions

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