File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copy from one list another List with custom classes

 
swapna ponnat
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're iterating over the list that you have just created. You should be iterating over the source list.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
swapna ponnat
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi swapna,
remove the inner for loop and you will be fine


 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for giving an idea to resolve this issue.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic