Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36114

Re: Trying to set an ArrayList within an object class

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Trying to set an ArrayList within an object class
Date 2016-12-13 12:47 -0500
Organization A noiseless patient Spider
Message-ID <o2pc5a$mbj$1@dont-email.me> (permalink)
References <5707f5e9-7009-44e6-a072-5e06f8327816@googlegroups.com>

Show all headers | View raw


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<CyclistX>) is undefined for the type List<GroupX>
>
>
> Here is my code:
>
>
> public class Test extends PApplet {
> 	/* test */
> public void setup() {
> 	
> 	System.out.println("test");
> 	
>     List<CyclistX> riders  = new ArrayList<CyclistX>();
>     List<GroupX>   teams   = new ArrayList<GroupX>();
>
>     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)

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Trying to set an ArrayList within an object class bobr <rraupach@att.net> - 2016-12-13 09:02 -0800
  Re: Trying to set an ArrayList within an object class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-13 12:47 -0500
  Re: Trying to set an ArrayList within an object class Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-13 17:56 +0000

csiph-web