Results 1 to 14 of 14
  1. #1
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default [SOLVED] A better way to do this - ArrayLists

    public java.util.List<com.netcb.sas.adapters.gms.Town> getTowns() {
    ArrayList<Town> towns = new ArrayList<Town>();
    Town town = new Town();
    try{
    java.util.Iterator locations = new DBUtil().getTowns().iterator();
    while(locations.hasNext()){
    Location loc = (Location)locations.next();
    town.setId(loc.getNoLocation());
    town.setName(loc.getDescLocation());
    towns.add(town);
    }
    return towns;
    }
    catch(HibernateException he){
    he.printStackTrace();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    //todo
    return null;
    Town and Location are my own defined classes but not related. Is there a way to achieve the same results without a loop or without
    town.setId(loc.getNoLocation());
    town.setName(loc.getDescLocation());
    Is there a better way to do this?
    Tshegofatso Manakana
    a.k.a Untouchable

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Collections.copy()?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    The classes Town and Location are not related so I don't think Collections.copy() will do the trick.
    Tshegofatso Manakana
    a.k.a Untouchable

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by Tshegofatsom View Post
    Town and Location are my own defined classes but not related. Is there a way to achieve the same results without a loop or without
    Is there a better way to do this?
    All this is doing:
    Java Code:
    town.setId(loc.getNoLocation());
    town.setName(loc.getDescLocation());
    is setting the id and name of the same town, over and over again. So all the items in ArrayList will be exactly the same object, and consequently have exactly the same data in them.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Hi,
    If the objects are related we can do.But,Location is a separate class object and Town is a separate class object.How can u copy directly?

    -Regards
    Ramya
    Ramya:cool:

  6. #6
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    That is exactly my question buddy. How can we copy exactly? Is there a way?
    Tshegofatso Manakana
    a.k.a Untouchable

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by Tshegofatsom View Post
    That is exactly my question buddy. How can we copy exactly? Is there a way?
    I'm a bit confused as to what you expect could happen here?

    Apart from the bug I pointed out earlier, that is.

    You have a list of objects, which you want to turn into a list of other objects on a one-to-one basis, so there's got to be a loop somewhere.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    As Toll said,it is not possible.

    Take a Hashtable and load the values from location and return it to Townclass.Here also u need to iterate.No other go........
    Ramya:cool:

  9. #9
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    The bug isn't really a bug,it does not really matter. I just wanted to know
    1. If the same goal can be achieved using a logic that saves resources
    2. If we still need to loop, can we at least be able to assign a loc to a town? ie
    town = loc;
    If I was using c++, i would overload the operator "=" to accept a type of Location and do the "one-to-one basis" in there. In that way I can assign an object of Location to Town whenever I want to i;e
    town = loc;
    Tshegofatso Manakana
    a.k.a Untouchable

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by Tshegofatsom View Post
    The bug isn't really a bug,it does not really matter. I just wanted to know
    1. If the same goal can be achieved using a logic that saves resources
    2. If we still need to loop, can we at least be able to assign a loc to a town? ie

    If I was using c++, i would overload the operator "=" to accept a type of Location and do the "one-to-one basis" in there. In that way I can assign an object of Location to Town whenever I want to i;e
    Well, the closest to that would be a constructor in Town that accepts a Location, in which you then do the assignments you are currently doing.

    But that bit I pointed out is still a bug...even in test code...:)

  11. #11
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    Well, the closest to that would be a constructor in Town that accepts a Location, in which you then do the assignments you are currently doing.
    Yeah that is good.... I think that will work for me since I cant overload the "=" operator
    in JAVA.

    But that bit I pointed out is still a bug...even in test code...
    Ok if its a concern then all needs to be done is to create a new town in every iteration in the loop.....That should work :)

    But the constructor solution is sure the closest (Unless someone has some thing else) I like that......I give you raps for that... "viva Tolls viva!!"
    Tshegofatso Manakana
    a.k.a Untouchable

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by Tshegofatsom View Post
    Yeah that is good.... I think that will work for me since I cant overload the "=" operator
    in JAVA.


    Ok if its a concern then all needs to be done is to create a new town in every iteration in the loop.....That should work :)

    But the constructor solution is sure the closest (Unless someone has some thing else) I like that......I give you raps for that... "viva Tolls viva!!"
    If you do this via a constructor, you'll be creating a new Town each time anyway, and I can rest happy in the knowledge there's one less bug in the world...:)

  13. #13
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Hi,
    As Toll Suggested,I think you can do like this..

    In Townclass u will have constructor
    public Town(String id, String name) {
    }

    then have something in the iterator loop like

    Java Code:
    public java.util.List<com.netcb.sas.adapters.gms.Town> getTowns() {
    ArrayList<Town> towns = new ArrayList<Town>();
    //Town town = new Town();[COLOR="Red"][/COLOR]
    try{
    java.util.Iterator locations = new DBUtil().getTowns().iterator(); 
    while(locations.hasNext()){
    Location loc = (Location)locations.next();
    [COLOR="red"]//town.setId(loc.getNoLocation());
    //town.setName(loc.getDescLocation());[/COLOR]
    //The above block u can remove and instead u can do like this below
    
    //[COLOR="red"]towns.add(town);[/COLOR]
    //U can add this in a single line.
    towns.add(new Town(loc.getNoLocation(), loc.getDescLocation);
    
    }
    return towns;
    }
    towns.add(new Town(loc.getNoLocation(), loc.getDescLocation);
    Ramya:cool:

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    As Toll Suggested,I think you can do like this..

    In Townclass u will have constructor
    public Town(String id, String name) {
    }
    Well, I was thinking more of
    Java Code:
    public Town (Location loc) { // Do assignment stuff here
    }
    but that depends on whether Town and Location will always be distributed together.

Similar Threads

  1. how to compare the elements of these two arraylists
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 03-25-2009, 10:55 PM
  2. ArrayList of ArrayLists
    By coolnfunky_raj in forum New To Java
    Replies: 10
    Last Post: 07-03-2008, 10:07 AM
  3. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:29 PM
  4. recursively searching through arraylists
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:36 AM
  5. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 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
  •