0

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);
Puteolana
  • 41
  • 1
  • 6

2 Answers2

1

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Might be a good idea to also mention that the first snippet can result in spaghetti code if moderation is not applied. – npinti Jun 23 '15 at 10:05
  • @npinti I'm not sure about that. The first snippet looks cleaner to me. Of course it makes assumptions regarding the values returned by the intermediate chained method calls (it assumes none of them returns null), but that's a different issue. – Eran Jun 23 '15 at 10:07
  • I was thinking more in terms of an FYI sort of thing. I also agree that the first snippet is cleaner, but if one keeps piling up things can get a bit messy. – npinti Jun 23 '15 at 10:24
0

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.

Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106