Skip to main content

ArrayList as an interface method argument?

1 reply [Last post]
jxstanford
Offline
Joined: 2007-06-14

Ok, this may be posted to the wrong discussion forum, but I didn't see one for silly questions, so feel free to direct me to the right place...

I have this interface:

<br />
import java.util.ArrayList;</p>
<p>public interface TemplateActions {</p>
<p>    public void commit(ArrayList partialList);<br />
    public void rollback(ArrayList partialList); </p>
<p>}<br />

When I try to implement the methods in a class like:

<br />
     public void commit(ArrayList partialList) {<br />
          doit();<br />
     }</p>
<p>    public void rollback(ArrayList partialList) {<br />
          doit();<br />
     }<br />

I am told that I haven't implemented these methods properly. To implement properly I can't specify the type of thing in the ArrayList. So this passes:

<br />
    public void commit(ArrayList partialList) {<br />
          doit();<br />
     }</p>
<p>    public void rollback(ArrayList partialList) {<br />
          doit();<br />
     }<br />

Two questions:

1. Can someone explain why I can't type this method as well as I want to?
2. Should I just use Boolean[] instead?

Thanks,
John

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
walterln
Offline
Joined: 2007-04-17

[url=http://forum.java.sun.com/forum.jspa?forumID=316]This[/url] might be a better forum.

But who tells you it is wrong? The first solution compiles OK for me (JDK1.6). If your code must work with Java 1.4 then you indeed cannot use generics (only in 1.5+).

I would require List instead of ArrayList though (interface instead of implementation class):
[code]public void commit(List partialList);[/code]

Also normally a method like this would allows lists of subclasses, but I doubt many people would extends Boolean :)
[code]public void commit(List partialList);[/code]

On whether to use Boolean[] (maybe even boolean[]?) depends on how that method should be used (the parameter name partialList suggests you want a list?).
In 1.6 you could also use the varargs option:
[code]public void commit(Boolean... partialList);[/code]