Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36115
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Trying to set an ArrayList within an object class |
| Date | 2016-12-13 17:56 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <o2pco0$ip3$1@dont-email.me> (permalink) |
| References | <5707f5e9-7009-44e6-a072-5e06f8327816@googlegroups.com> |
On Tue, 13 Dec 2016 09:02:50 -0800, 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);
>
setGroup is a method belonging to GroupX, not to ArrayList<GroupX>, so
you need to first get a reference to the GroupX instance in the teams
list and operate on that, something like this would work:
GroupX gx = new GroupX();
teams.add(gx);
....
gx.setGroup(riders);
or, less elegantly or readably, substitute the following for your failing
line:
teams.get((teams.size() -1).setGroup(riders);
**** Not tested since the code you supplied is incomplete and so
**** not compilable (PApplet and CyclistX declarations are missing).
**** Please supply an SSCCE in future: http://sscce.org/
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll 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