aspose file tools*
The moose likes Beginning Java and the fly likes Copy from one list another List with custom classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copy from one list another List with custom classes" Watch "Copy from one list another List with custom classes" New topic
Author

Copy from one list another List with custom classes

swapna ponnat
Greenhorn

Joined: Jan 17, 2007
Posts: 6
trying to copy one list items to another list items after copying it's giving empty list.
I am using custom classes for those lists but attributes are same data type.

[Added code tags - see UseCodeTags for details]

Orders and Customers classes had same attributes CustomerName,OrderId,CustomerId,flag, getters and setters.

Thanks in advance.



Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3568
    
  15
You're iterating over the list that you have just created. You should be iterating over the source list.


Joanne
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

After formatting the code, I'm surprised it outputs anything, as the braces don't seem to match up.

But ignoring that - nowhere do you add any objects to any lists. You also use an order variable that isn't declared anywhere - are you sure that's the correct code you've posted?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Orders and Customers classes had same attributes CustomerName,OrderId,CustomerId,flag, getters and setters.

This doesn't sound good. Why would you then need two classes?


The quieter you are, the more you are able to hear.
swapna ponnat
Greenhorn

Joined: Jan 17, 2007
Posts: 6
Sorry I missed one line. Here is correct code . And Orders class having 100 fields but Customers class having only 4 fields :CustomerName,OrderId,CustomerId,flag
public void copyList(List<Orders> orders)
{
List<Customers> cOrders = new ArrayList<Customers>();
for(Orders order : orders)
{
for (Customers cOrder: cOrders)
{
cOrder.setCustomerName(order.getCustomerName());
cOrder.setOrderId(order.getOrderId());
cOrder.setCustomerId(order.getCustomerId());
cOrder.setFlag(order.isFlag());
}
}
System.out.println(cOrders); //This is giving output as []


}
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3568
    
  15
You're still not creating any Customers instances. Instead of the inner for loop you should be creating a Customers instance and adding it to the cOrders list.
Manu Somasekhar
Ranch Hand

Joined: May 22, 2012
Posts: 44
Hi swapna,
remove the inner for loop and you will be fine


Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3568
    
  15
Manu Somasekhar wrote:Hi swapna,
remove the inner for loop and you will be fine

No he won't.
What's the cOrder variable referencing ?
Where does he add anything to the cOrders list ?
swapna ponnat
Greenhorn

Joined: Jan 17, 2007
Posts: 6
Thanks everyone for giving an idea to resolve this issue.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy from one list another List with custom classes