Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Objects and Inner Objects

Status
Not open for further replies.

gorgered

Programmer
Dec 13, 2005
24
US
I have a data some thing similar to this. Which has section start(1) and section end(2) and
each section might have a sub-sections as well and will also start and end with (1)(2)
Code:
Name		Value		Section
---------------------------------------
1		1		0
2		2		0
3		3		1	// section start
4		4		0
5		5		0
6		6		1	// sub section start
7		7		0
8		8		2	// end of sub section   6
9		9		2	// end of section start 3
10		10		0
In trying to build dynamic Collections objects out of the data. When ever I encounter start of section(1) I have to create a new List and add the the corresponding items to the new list and if I encounter sub-section it has to create a new List and add the subItems. The data would be in seq order.

If I take the above data then the size of the list would be 4 and the list.get(2) will have another list and list(2.2) will have another list
some thing like this.
Code:
new List[
object[0] = 1
object[1] = 2
object[2] = new List[
	object[2.0] = 4
	object[2.1] = 5
	object[2.2] = new List[
			object[2.2.0] = 7
		]
	]
object[3] = 10
]
Help is greatly appreciated.

Gorge
 
I'd use ArrayList class.

The problem you post is a little generic, can you post what's your actual and concrete question?

Cheers,
Dian
 
Hi,

I am trying to write a java method or a class given a List of objects which has groups, subgroups returs the new List with the structure that was mentioned.

I have changed the data a bit.

Code:
Name        Value        Section
---------------------------------------
1        1        0
2        2        0
3        3        1    // section start
4        4        0
5        5        0
6        6        3    // sub section start
7        7        0
8        8        4    // sub section end  6
9        9        2    // section end 3
10        10        0


Code:
public class TestBean {

    private String name;
    private String value;
    private int displayType;

    public TestBean(String name, String value, int displayType) {
        this.name = name;
        this.value = value;
        this.displayType = displayType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public int getDisplayType() {
        return displayType;
    }

    public void setDisplayType(int displayType) {
        this.displayType = displayType;
    }
}

Code:
public class TestData {

    private ArrayList list= null;

    public TestData() {
        list = getList();
    }



    public ArrayList getList() {
        list = new ArrayList();
        list.add(new TestBean("1","1", 0));
        list.add(new TestBean("2","2", 0));
        list.add(new TestBean("3","3", 1));
        list.add(new TestBean("4","4", 0));
        list.add(new TestBean("5","5", 0));
        list.add(new TestBean("6","6", 3));
        list.add(new TestBean("7","7", 0));
        list.add(new TestBean("8","8", 4));
        list.add(new TestBean("9","9", 2));
        list.add(new TestBean("10","10", 0));

        return list;  
    }

}

Code:
public class Test {

    public static void main(String[] args) {
        Test.test();
    }

    static ArrayList group = null;
    static ArrayList subGroup = null;

    public static void test() {
        // get list
        TestData data = new TestData();
        ArrayList list = data.getList();

        // iterate list
        Iterator iterator = null;
        boolean isOuter = false;
        boolean isInner = false;
        ArrayList objectList = new ArrayList();
        iterator = list.iterator();
        while (iterator.hasNext()) {
            // get the bean from the list
            TestBean bean = (TestBean) iterator.next();
            if (bean.getDisplayType() == 1 || bean.getDisplayType() == 2) {
                isOuter = true;
            }
            if (isOuter) {
                // create a new group
                //System.out.println(bean.getName());
                if (group == null) {
                    group = new ArrayList();
                }
                if (bean.getDisplayType() == 3) {
                    isInner = true;
                }
                if (isInner) {
                    // create new sub group
                    if (subGroup == null) {
                        subGroup = new ArrayList();
                    }
                    if (bean.getDisplayType() == 0)
                        subGroup.add(bean);
                }
                if (bean.getDisplayType() == 4) {
                    group.add(subGroup);
                    isInner = false;
                }


                if (bean.getDisplayType() == 2) {
                    isOuter = false;
                    objectList.add(group);
                }
                if (bean.getDisplayType() == 0) {
                    group.add(bean);
                }

            } else {
                // add the object to list
                objectList.add(bean);
            }
        }
        iterator = null;

        System.out.println(objectList.size());
        iterator = objectList.iterator();
        while (iterator.hasNext()) {
            Object object = iterator.next();
            if (object instanceof ArrayList) {
                ArrayList arrayList = (ArrayList) object;
                Iterator subIterator = arrayList.iterator();
                while (subIterator.hasNext()) {
                    Object subObject = subIterator.next();
                    if (subObject instanceof ArrayList) {
                        ArrayList arrayList1 = (ArrayList) subObject;
                        Iterator subIterator1 = arrayList1.iterator();
                        while (subIterator.hasNext()) {
                            TestBean bean = (TestBean) subIterator1.next();
                            System.out.println(bean.getValue() + "SS" + bean.getDisplayType());
                        }
                    } else {
                        TestBean bean = (TestBean) subObject;
                        System.out.println(bean.getValue() + "GG" + bean.getDisplayType());
                    }

                }

            } else {
                TestBean bean = (TestBean) object;
                System.out.println(bean.getValue() + "##" + bean.getDisplayType());
            }
        }
        iterator = null;
    }
}

The output of Test is
Code:
4
1##0
2##0
4GG0
5GG0
7GG0
10##0

I am expecting it to be. I want help is writing up the logic, if there is any better soultion would also hlep.
Code:
4
1##0
2##0
4GG0
5GG0
7SS0
10##0

Thanks
Gorge
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top