Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Trying to set an ArrayList within an object class Date: Tue, 13 Dec 2016 12:47:39 -0500 Organization: A noiseless patient Spider Lines: 51 Message-ID: References: <5707f5e9-7009-44e6-a072-5e06f8327816@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 13 Dec 2016 17:46:18 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="a700588d24308dff6cdbdc832c4ad3b2"; logging-data="22899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18HZUGfdNhnNgFJAk0DZh3b" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 In-Reply-To: <5707f5e9-7009-44e6-a072-5e06f8327816@googlegroups.com> Cancel-Lock: sha1:i5uRCusXwmkrERLMOm2ruo+DBho= Xref: csiph.com comp.lang.java.programmer:36114 On 12/13/2016 12:02 PM, bobr wrote: > Hi, I'm trying to teach myself java, and am stuck on a concept that I wish to learn. > > I'm trying to have a new object class (GroupX) that has several attributes, including an ArrayList that may contain one or more other objects (of type CyclistX). Within my test code, when attempting to invoke GroupX's SetGroup method, I receive the following message: > > The method setGroup(List) is undefined for the type List > > > Here is my code: > > > public class Test extends PApplet { > /* test */ > public void setup() { > > System.out.println("test"); > > List riders = new ArrayList(); > List teams = new ArrayList(); > > teams.add(new GroupX()); > > riders.add(new CyclistX("Froome", "Sky", 1, sky, "GBR", "x")); > riders.add(new CyclistX("Costa", "Sky", 2, sky, "POR", "x")); > > /* THE FOLLOWING LINE FAILS, AND I DON'T KNOW WHY!! */ > teams.setGroup(riders); What is `teams'? It's a reference to something. To what does `teams' refer? To a List that holds references to GroupX things. (The List happens to be an ArrayList, which is one of several kinds of List, but that's not important here.) Do things of type List have a setGroup() method? No! And that's why Java complains: It's been told to call the setGroup() method of a List, but a List has no such method. A GroupX thing has a setGroup() method, but a List -- and therefore `teams' -- does not. Your List *contains* references to GroupX'es, but is not itself a GroupX. The List does not have GroupX behavior; the individual GroupX'es contained in the list have GroupX behavior. Your bookshelf contains books. Can you read your bookshelf? -- esosman@comcast-dot-net.invalid "Nobody ever went broke underestimating the intelligence of the American public." -- HLM (paraphrased)