mardi 25 août 2015

Iterating through arraylist to solve aproblem

In my code below I am trying to find out "products" which has no child associated with it. I have a structure of products like parent and child. Products are in an arraylist:{A,B,C,D,E,F,G}. I represent these products(A,B,C..) as objects of class ProductionOrder:

public class ProductionOrder {

    public int id;
    public int parentId;


    public ProductionOrder(int ids,int parentIds) {
        id=ids;
        parentId=parentIds;
    }
}

I store each product(A,B,C..) with an id and its associated productId(If any). If it is a parent or if the product does not have a child and is standalone, I assign parentID as 0. if it is a child of some parent(A(Id=1)) , I assign A's child with parentID=1. Now I need to sort out all the child and standalone product and store it in a list and return it. Here is what I started, but need to think of a logic to iterate and get the childs:

import java.util.*;

public class ProductTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Hello");
        ArrayList<ProductionOrder> prod=new ArrayList<ProductionOrder>();
        ProductionOrder A=new ProductionOrder(1,0);
        prod.add(A);
        ProductionOrder F=new ProductionOrder(2,1);
        prod.add(F);
        ProductionOrder C=new ProductionOrder(3,1);
        prod.add(C);
        ProductionOrder E=new ProductionOrder(4,3);
        prod.add(E);
        ProductionOrder D=new ProductionOrder(5,0);
        prod.add(D);
        ProductionOrder B=new ProductionOrder(6,5);
        prod.add(B);
        ProductionOrder G=new ProductionOrder(7,0);
        prod.add(G);


        ArrayList<ProductionOrder> result=ProdOrder(prod);

    }


    static ArrayList<ProductionOrder> ProdOrder(ArrayList<ProductionOrder> prodord){

        ArrayList<ProductionOrder> child=new ArrayList<ProductionOrder>();

        // logic to get the child and the standalone product with no child like G


        return child;

    }




}



from Newest questions tagged java - Stack Overflow http://ift.tt/1NSnBtK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire