0

I've an object and It's JSON schema structured goes like this :

{
"ParentList": [
    {
        "Parrent": "ListItem0",
        "ChildListItems": [
            1,
            2,
            3
        ]
    },
    {
        "Parrent": "ListItem1",
        "ChildListItems": [
            1,
            2,
            3,
            4,
            5
        ]
    }
]

}

I have consumed this data into an object that has an Array list for Parent. Each item of parent has an array list for children.

When user selects a child of any parent.

I swap the selected parent into 0th position and swap the selected child also into 0th position. Say user selected 4th child of second parent.

{
"ParentList": [
    {
        "Parrent": "ListItem1",
        "ChildListItems": [
            4,
            2,
            3,
            1,
            5
        ]
    },
    {
        "Parrent": "ListItem0",
        "ChildListItems": [
            1,
            2,
            3
        ]
    }
]

}

But I want to keep the original copy uninterrupted post swapping.

To deal with mutability of List I'm creating a new ArrayList with refernce value of actual list as of follow

 parentListCopy = new ArrayList<>(actualParentList);

after swapping my actualParentList indexes are not affected.

But index of child array list getting collapsed based on the swap performed to the copyParent's child list.

{
"ParentList": [
    {
        "Parrent": "ListItem0",
        "ChildListItems": [
            1,
            2,
            3
        ]
    },
    {
        "Parrent": "ListItem1",
        "ChildListItems": [
            4,
            2,
            3,
            1,
            5
        ]
    }
]

}

MohanRaj
  • 662
  • 1
  • 6
  • 21
  • You want the swapping to be done in the actualParentList? In your example, the child is getting swapped, but the parent is not. What is the desired behaviour there? Should both be swapped, or none of them be swapped? – iavanish Apr 11 '17 at 08:14
  • 1
    you are not doing a deep copy of the array list items. You need to clone the contents of arraylist one by one. see here: http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents – RajatJ Apr 11 '17 at 08:23
  • Possible duplicate of [How to clone ArrayList and also clone its contents?](http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents) – RajatJ Apr 11 '17 at 08:23

1 Answers1

0

When you do

 parentListCopy = new ArrayList<>(actualParentList);

it doesn't save the reference of actualParentList into parentListCopy. Instead it creates a new list reference and copies the elements of actualParentList to parentListCopy.

If you want to preserve the reference Ids, you need to swap each field of the objects instead of swapping the objects. For example, lets say there is an object of type Parent with two fields a and b. You have an ArrayList of Parent objects and want to swap index 0 with index 3. You need to swap parents.get(0).a with parents.get(3).a and parents.get(0).b with parents.get(3).b.

iavanish
  • 509
  • 3
  • 8