Reputation: 369
Suppose a class has an ArrayList attribute:
private ArrayList<Object> objects;
Is it possible to have a setter / mutator method for this attribute, like so:
public void setObjects(ArrayList<Object> objects) {
this.objects = objects;
}
Or do you have to do something more complicated because of the way Java does things?
I tried to to test this myself with the following two classes:
Test.java
import java.util.*;
public class Test {
private ArrayList<String> strings;
public Test() {
strings = new ArrayList<String>();
}
public ArrayList<String> getStrings() {
return this.strings;
}
public void setStrings(ArrayList<String> strings) {
this.strings = strings;
}
}
testTest.java
import java.util.*;
public class testTest {
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<String>();
strings.add("One");
strings.add("Two");
strings.add("Three");
Test test = new Test();
test.setStrings(strings);
System.out.println("First get() of index 0: "+test.getStrings().get(0));
System.out.println("Modifying test's strings object...");
test.getStrings().set(0, "1");
System.out.println("First get() of index 0: "+test.getStrings().get(0));
System.out.println("Change it back... using strings variable");
test.setStrings(strings);
System.out.println("First get() of index 0: "+test.getStrings().get(0));
}
}
Output:
First get() of index 0: One
Modifying test's strings object...
First get() of index 0: 1
Change it back... using strings variable
First get() of index 0: 1
The last print statement should show that first index is "One" .
So, any ideas how I can achieve my original intent?
Upvotes: 0
Views: 17665
Reputation: 76
You change the strings array in the line of:
test.getStrings().set(0, "1");
so you cannot expect that the line
test.setStrings(strings);
will restore the original one.
You can do something like:
public static void main(String[] args) {
ArrayList<String> originalStrings = new ArrayList<String>();
originalStrings.add("One");
originalStrings.add("Two");
originalStrings.add("Three");
ArrayList<String> strings = (ArrayList<String>) originalStrings.clone();
Test11 test = new Test11();
test.setStrings(strings);
System.out.println("First get() of index 0: "+test.getStrings().get(0));
System.out.println("Modifying test's strings object...");
test.getStrings().set(0, "1");
System.out.println("First get() of index 0: "+test.getStrings().get(0));
System.out.println("Change it back... using strings variable");
test.setStrings(originalStrings);
System.out.println("First get() of index 0: "+test.getStrings().get(0));
}
Upvotes: 1
Reputation: 46841
Try
public void setStrings(ArrayList<String> strings) {
// clear it if you want a fresh list
this.strings.clear();
this.strings.addAll(strings);
}
output:
First get() of index 0: One
Modifying test's strings object...
First get() of index 0: 1
Change it back... using strings variable
First get() of index 0: One
If you don't want to refer the same ArrayList
then use addAll()
method.
Upvotes: 1