f



Trying to set an ArrayList within an object class

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 SetGro=
up 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() {
=09
	System.out.println("test");
=09
    List<CyclistX> riders  =3D new ArrayList<CyclistX>();
    List<GroupX>   teams   =3D new ArrayList<GroupX>();

    teams.add(new GroupX());
   =20
	riders.add(new CyclistX("Froome", "Sky", 1, sky, "GBR", "x"));
	riders.add(new CyclistX("Costa",  "Sky", 2, sky, "POR", "x"));=09
=09
    /* THE FOLLOWING LINE FAILS, AND I DON'T KNOW WHY!! */
	teams.setGroup(riders);

    for(CyclistX biker : riders) {
    	System.out.println(biker.getName());
    }
   =20
    for(GroupX team : teams) {
    	System.out.println(team.getBreakingAway());
    	System.out.println(team.getGroup());
    }  =20
  }
} =20

..
..
..


import java.util.ArrayList;
import java.util.List;

public class GroupX {

	private boolean movingForward;
	private boolean breakingAway;
	private boolean droppingBack;
	private boolean droppingOff;
=09
    private List<CyclistX> group =3D new ArrayList<CyclistX>();
=09
	public GroupX() {
	}
=09
	public List<CyclistX> getGroup() {
		return group;
	}
	=20
	public void  setGroup(List<CyclistX> group) {
		this.group =3D group;
	}

etc...
..
..
..=09

Any help would be appreciated.=20
Bob R=09


0
bobr
12/13/2016 5:02:50 PM
comp.lang.java.programmer 52714 articles. 0 followers. Post Follow

2 Replies
62 Views

Similar Articles

[PageSpeed] 53

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)
0
Eric
12/13/2016 5:47:39 PM
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       |
0
Martin
12/13/2016 5:56:16 PM
Reply: