Java获取然后从列表中删除

时间:2015-06-23 10:00:35

标签: java list setter getter instances

在我的代码中,我有一个类的实例列表。我想获得1个实例的属性,即ArrayList。在这个课程中,我实现了getter和setter。所以我打电话给

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

以删除此列表的第1项。这有效吗?或者我必须首先获取列表,将其存储到临时变量,删除我想要的项目,最后用set方法刷新它?

实施例

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

2 个答案:

答案 0 :(得分:1)

两个代码段都会产生相同的结果,因为listofinstances.get(i).getArrayList().remove(0)更新listofinstances.get(i).getArrayList()返回的List实例,而第二个代码段只执行两行。

第二个代码段中的listofinstances.get(i).setArrayList(tmp);行是多余的,因为i的{​​{1}}元素已经拥有对listofinstances引用的同一列表的引用。

答案 1 :(得分:0)

使用

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

足够且有效。

为了证明这一点,我已经为您编写了测试代码。我刚刚删除了第二个对象的ArrayList的第二个String元素,你可以比较初始状态和更新状态;

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;
        }

    }

}

输出如下;

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

希望它有所帮助。