Java Get and then remove from a list

3.6k Views Asked by At

In my code i have a list of instances of a class. And i want to get a attribute of 1 instance which is ArrayList. In this class i have implement getters and setters. So I call

listofinstances.get(i).getArrayList().remove(0); 

in order to remove the 1st item of this list. Is this valid?? Or i have to get the list at first, store it to a temp variable, remove the item i want and finally refresh it with set method??

Example

tmp = listofinstances.get(i).getArrayList();
tmp.remove(0);
listofinstances.get(i).setArrayList(tmp);
2

There are 2 best solutions below

0
On BEST ANSWER

Removing with

listOfInstances.get(1).getArrayList().remove(1);

is enough and valid.

In order to demonstrate this, I've written a test code for you. I've just removed the second object's ArrayList's second String element, you can compare the initial and updated states;

import java.util.ArrayList;
import java.util.Arrays;

public class TestQuestion {

    public static void main(String[] args) {
        // inital objects to be stored
        ArrayList<String> arr1 = new ArrayList<String>(
                Arrays.asList((new String[] { "ins1: test string1",
                        "ins1: test string2", "ins1: test string3" })));
        PlaceHolderObject<String> pho1 = new PlaceHolderObject<String>();
        pho1.setArrayList(arr1);

        ArrayList<String> arr2 = new ArrayList<String>(
                Arrays.asList((new String[] { "ins2: test string1",
                        "ins2: test string2", "ins2: test string3" })));
        PlaceHolderObject<String> pho2 = new PlaceHolderObject<String>();
        pho2.setArrayList(arr2);

        ArrayList<String> arr3 = new ArrayList<String>(
                Arrays.asList((new String[] { "ins3: test string1",
                        "ins3: test string2", "ins3: test string3" })));
        PlaceHolderObject<String> pho3 = new PlaceHolderObject<String>();
        pho3.setArrayList(arr3);

        // gather up all instance in one
        ArrayList<PlaceHolderObject<String>> listOfInstances = new ArrayList<PlaceHolderObject<String>>();

        // assignments
        listOfInstances.add(pho1);
        listOfInstances.add(pho2);
        listOfInstances.add(pho3);

        // print contents of listOfInstances objects
        System.out.println("Contents of the 'listOfInstances' list");
        System.out.println("**************************************");

        for (int i = 0; i < listOfInstances.size(); i++)
            System.out.println(listOfInstances.get(i).getArrayList());

        System.out.println();

        // print references of the contents of listOfInstances objects
        System.out.println("References of the 'listOfInstances' list");
        System.out.println("****************************************");

        for (int i = 0; i < listOfInstances.size(); i++) {
            System.out.println(i+1 + "th Object: " + listOfInstances.get(i) );
            System.out.println("   ArrayList hashcode: " + listOfInstances.get(i).getArrayList().hashCode()  );
        }

        // Remove second item of the second object
        listOfInstances.get(1).getArrayList().remove(1);
        System.out.println();
        System.out.println();

        // print contents of listOfInstances objects
        System.out.println("Contents of the 'listOfInstances' updated list");
        System.out.println("**********************************************");

        for (int i = 0; i < listOfInstances.size(); i++)
            System.out.println(listOfInstances.get(i).getArrayList());

        System.out.println();

        // print references of the contents of updated listOfInstances objects
        System.out.println("References of the 'listOfInstances' updated list");
        System.out.println("************************************************");

        for (int i = 0; i < listOfInstances.size(); i++) {
            System.out.println(i+1 + "th Object: " + listOfInstances.get(i) );
            System.out.println("   ArrayList hashcode: " + listOfInstances.get(i).getArrayList().hashCode()  );
        }

    }

    // A POJO class that only stores an arrayList
    public static class PlaceHolderObject<T> {
        private ArrayList<T> arrayList;

        // no-arg default constructor
        public PlaceHolderObject() {
        }

        // parametric constructor
        public PlaceHolderObject(ArrayList<T> arrayList) {
            this.arrayList = arrayList;
        }

        public ArrayList<T> getArrayList() {
            return arrayList;
        }

        public void setArrayList(ArrayList<T> arrayList) {
            this.arrayList = arrayList;
        }

    }

}

And the output is as follows;

Contents of the 'listOfInstances' list
**************************************
[ins1: test string1, ins1: test string2, ins1: test string3]
[ins2: test string1, ins2: test string2, ins2: test string3]
[ins3: test string1, ins3: test string2, ins3: test string3]

References of the 'listOfInstances' list
****************************************
1th Object: TestQuestion$PlaceHolderObject@5058431c
   ArrayList hashcode: 1200611515
2th Object: TestQuestion$PlaceHolderObject@529e0c79
   ArrayList hashcode: -744028452
3th Object: TestQuestion$PlaceHolderObject@645064f
   ArrayList hashcode: 1606298877


Contents of the 'listOfInstances' updated list
**********************************************
[ins1: test string1, ins1: test string2, ins1: test string3]
[ins2: test string1, ins2: test string3]
[ins3: test string1, ins3: test string2, ins3: test string3]

References of the 'listOfInstances' updated list
************************************************
1th Object: TestQuestion$PlaceHolderObject@5058431c
   ArrayList hashcode: 1200611515
2th Object: TestQuestion$PlaceHolderObject@529e0c79
   ArrayList hashcode: 828096323
3th Object: TestQuestion$PlaceHolderObject@645064f
   ArrayList hashcode: 1606298877

Hope that it helps.

3
On

Both code snippets will produce the same result, since listofinstances.get(i).getArrayList().remove(0) updates the List instance returned by listofinstances.get(i).getArrayList(), and the second snippet does the same, only in two lines.

The listofinstances.get(i).setArrayList(tmp); line in the second snippet is redundant, since the ith element of listofinstances already holds a reference to the same list referred by tmp.