Results 1 to 4 of 4
  1. #1
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Array of Objects

    Hi guys! I seem to be having a problem with the concept of Array of Objects. I already made a class which makes the setters and getters for the array:

    I want to make another class which reads from a file and puts together built-in arrays (e.g FIRSTSTOP,LASTSTOP etc) in to one big array of objects using the code above. So far, Ive got the following:


    it would return: "Leicester".
    I realise I made a post similar to this already, but I dont think I was asking the right questions. I really would appreciate a help on this. Thanks.
    Last edited by coopc; 04-29-2011 at 02:43 AM.

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Make your array static and if you want to access it from a different class, refer to is as BusjourneyList.JourneyArray[].<what ever you need to get from it>

    Actually You might not need to make it static. But thats how you refer to something in a different class.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

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

    Default

    If you want to get the results of retrieveBusInfo(), why not just return the BusJourney array from it, like this:
    Java Code:
    public BusJourney[] retrieveBusInfo() {
       ... // fill the array
       return journeyArray;
    }
    On the other hand, if you don't want to regenerate the array every time you want to use it, just provide a getBusInfo() method to return it. Then you can regenerate it when required by calling retrieveBusInfo() and get hold of it subsequently by calling getBusInfo().

    Incidentally, it's not generally considered Best Practice to make your class data public unless there's a very good reason, but given public access you can get hold of the BusJourney array directly: BusJourney[] busJourneys = data.journeyArray;, although I wouldn't recommend it.


    Also, you should use the Java naming conventions so people can follow your code more easily (class names start with an uppercase letter, method and variable names start with a lowercase letter).
    Last edited by dlorde; 04-27-2011 at 11:51 PM.

  4. #4
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Array of objects
    By Saletra in forum New To Java
    Replies: 2
    Last Post: 07-31-2010, 12:16 PM
  2. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 05:57 PM
  3. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM
  4. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:24 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
  •