cworner1
cworner1

Reputation: 451

Coding practise for initialising ArrayLists

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.

Upvotes: 0

Views: 424

Answers (2)

Stephen C
Stephen C

Reputation: 719346

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.

Upvotes: 3

Hazem Elraffiee
Hazem Elraffiee

Reputation: 453

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.

Upvotes: 1

Related Questions