简体   繁体   中英

How pass a serializable object inside a ArrayList

I have a class call MyClass that implements Serializable, Cloneable with his strings, int, setters, getters methods. Now, I have this two methods, in another class, that works fine separately.

1)

public void myStatus(final MyClass a, final boolean b) {
.
.
.
}

2)

public void secondStatust(final ArrayList<MyClass> as, final boolean b) {
.
.
.
}

That I want to do is call the method 2 inside the method 1, something like this:

public void myStatus(final MyClass a, final boolean b) {

        secondStatust(a, b);
}

But I can't do it, because the second method is waiting a ArrayLis....

Some suggestions??

Thanks in advance

You can do this

public void myStatus(final MyClass a, final boolean b) {
    ArrayList<MyClass> arraylist = new ArrayList<MyClass>();
    arraylist.add(a);
    secondStatust(arraylist, b);
}

Put the 'a' object in the arraylist object and pass it to secondStatust

You may also use double brace initialization , but be aware of the downsides :

 public void myStatus(final String a, final boolean b) {
    secondStatust(new ArrayList<String>() {{ add(a); }}, b);
 }

Downsides include:

  • Bad readability
  • Creation of a new (anonymous) type
  • Risk for memory leaks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM