0

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?

nhuff717
  • 369
  • 2
  • 3
  • 17
  • 3
    possible duplicate of [Is Java "pass-by-reference"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Sotirios Delimanolis Apr 27 '14 at 20:06
  • So I see that Java is pass-by-value, but I'm unsure how to apply that to my problem. If this was a language like PHP this wouldn't even be an issue... – nhuff717 Apr 27 '14 at 20:15
  • 1
    The variable `strings` in your example is always referencing the same object. When you pass it to `setString()`, the `Test` object gets a reference to the same object that `strings` is referring to. – Sotirios Delimanolis Apr 27 '14 at 20:18

2 Answers2

1

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.

Braj
  • 46,415
  • 5
  • 60
  • 76
  • Same output as before: 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 – nhuff717 Apr 27 '14 at 20:11
  • Okay, good to see this works for strings. Would it work if I had an ArrayList of objects? – nhuff717 Apr 27 '14 at 20:25
  • 1
    you can try it yourself by making a slight change in my code. – Braj Apr 27 '14 at 20:26
  • This worked changing all the String type declarations to Object type declarations. Thank you. – nhuff717 Apr 27 '14 at 20:36
1

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));
}
Benefits
  • 78
  • 6