0

I'm looking to improve my Java coding.

Can someone provide a link or an explanation on whether there's a code of practise to initialise ArrayLists and avoid the following problem:

-I have 6 ArrayList in one class, some are subsets of others. Because some are subsets of others I understand they share the same references through the "addAll()" and "add()" methods.

As a result, by attempting to change the elements in subsets I'm also altering the original sets because again - they share the same reference. My code is so messy that a few "get" calls results in 2 of my ArrayLists being reset.

I have researched this forum and google and I can't seem to find the relevant information I want. I only find simple examples of ArrayLists. I have noticed that there are a few ArrayList reference related questions on this forum so I think the answer to this question will benefit others in the future.

6
  • 6
    You should show your code - it sounds like some refactoring would be beneficial... Commented Nov 28, 2012 at 12:42
  • My coding is too long, but I'll include it if that is what you wish. Commented Nov 28, 2012 at 12:43
  • 4
    @cworner1: To ask useful questions, create a minimal, self-contained example of what you're talking about. Post that, and ask your question about it. Commented Nov 28, 2012 at 12:45
  • maybe Arrays.asList(T... a) Commented Nov 28, 2012 at 12:47
  • @ T.J.Crowder. Okay, but it will take me some time to re-edit. I know I have reference problem and I'm not surprised with the amount of reference sharing that goes on in my class. But I'm struggling to work out where - to be able to make a small example. Let me see if I can edit with minimal code. Commented Nov 28, 2012 at 12:56

2 Answers 2

3

Can someone provide a link or an explanation on whether there's a code of practise to initialise ArrayLists and avoid the following problem:

There is no such a code of practice, or best practice or whatever on initializing ArrayLists.

The problem is basically that you need to understand the difference in Java between using a reference to an existing object, and creating a new object. And you need to then choose the appropriate one ... depending on what you are trying to do.

(Asking for "best practice" on this topic is like asking for "best practice" on whether you should use + or - operators ...)

Rather than Googling for "best practice" on this, I suggest you go back to your Java text book / tutorial / lecture notes and read up on:

  • what Java object references are
  • what object assignment means, and
  • what the new operator does.

And make sure that you really understand them. When you understand them you will be able to understand which to use to do what you are trying to do in your program.

Sign up to request clarification or add additional context in comments.

Comments

1

addAll adds references to the objects in the ArrayList, not copies from objects. Id you want copies, you must iterate through the 1st ArrayList, and for every Object, call the "clone" function that will create a copy of the object, and add it to the new ArrayList.

Example:

public static void main(String[] args) {
    ArrayList<Foo> A = new ArrayList<Foo>();
    A.add(new Foo("foo1", 1));
    A.add(new Foo("foo2", 2));
    ArrayList<Foo> B = new ArrayList<Foo>();
    System.out.println("Before: ");
    System.out.println("A:");
    for(Foo foo:A){
        System.out.println(foo);
        try {
            B.add((Foo)foo.clone());
        } catch (CloneNotSupportedException ex) {
            //Never Gonna Happen
        }
    }
    System.out.println("B:");
    for(Foo foo:B){
        System.out.println(foo);
    }

    A.remove(0);

    System.out.println("After:");
    System.out.println("A:");
    for(Foo foo:A){
        System.out.println(foo);
    }
    System.out.println("B:");
    for(Foo foo:B){
        System.out.println(foo);
    }
}

public static class Foo{
    private String Name;
    private int Id;

    public Foo(String Name, int Id) {
        this.Name = Name;
        this.Id = Id;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return new Foo(Name,Id);
    }

    @Override
    public String toString() {
        return "Name: "+Name+", Id: "+Id;
    }
}

This prints out:

Before: 
A:
Name: foo1, Id: 1
Name: foo2, Id: 2
B:
Name: foo1, Id: 1
Name: foo2, Id: 2
After:
A:
Name: foo2, Id: 2
B:
Name: foo1, Id: 1
Name: foo2, Id: 2

If you deal with ArrayLists of classes you created, make sure they all have "clone" function overridden. For classes containing classes you also created, use the "clone" functions you created for the inner classes in the parent class "clone". And so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.