Results 1 to 14 of 14
- 05-19-2009, 11:59 AM #1
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
[SOLVED] A better way to do this - ArrayLists
Town and Location are my own defined classes but not related. Is there a way to achieve the same results without a loop or withoutpublic 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;Is there a better way to do this?town.setId(loc.getNoLocation());
town.setName(loc.getDescLocation());Tshegofatso Manakana
a.k.a Untouchable ™
- 05-19-2009, 12:19 PM #2
Collections.copy()?
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-19-2009, 01:14 PM #3
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
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 ™
- 05-19-2009, 02:03 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,441
- Rep Power
- 16
All this is doing:
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.Java Code:town.setId(loc.getNoLocation()); town.setName(loc.getDescLocation());
- 05-19-2009, 02:06 PM #5
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
RamyaRamya:cool:
- 05-20-2009, 09:33 AM #6
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
That is exactly my question buddy. How can we copy exactly? Is there a way?
Tshegofatso Manakana
a.k.a Untouchable ™
- 05-20-2009, 10:59 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,441
- Rep Power
- 16
- 05-20-2009, 11:07 AM #8
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:
- 05-20-2009, 11:10 AM #9
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
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? ieIf 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;etown = loc;town = loc;Tshegofatso Manakana
a.k.a Untouchable ™
- 05-20-2009, 11:15 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,441
- Rep Power
- 16
- 05-20-2009, 12:53 PM #11
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Yeah that is good.... I think that will work for me since I cant overload the "=" operatorWell, 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.
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 that bit I pointed out is still a bug...even in test code...
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 ™
- 05-20-2009, 01:15 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,441
- Rep Power
- 16
- 05-20-2009, 01:26 PM #13
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
towns.add(new Town(loc.getNoLocation(), loc.getDescLocation);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; }Ramya:cool:
- 05-20-2009, 02:05 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,441
- Rep Power
- 16
Similar Threads
-
how to compare the elements of these two arraylists
By raj reddy in forum Web FrameworksReplies: 1Last Post: 03-25-2009, 10:55 PM -
ArrayList of ArrayLists
By coolnfunky_raj in forum New To JavaReplies: 10Last Post: 07-03-2008, 10:07 AM -
A Map implemented with ArrayLists
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:29 PM -
recursively searching through arraylists
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-17-2008, 02:36 AM -
arraylists problem
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-12-2008, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks