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


Groups > comp.lang.java.programmer > #36113 > unrolled thread

Trying to set an ArrayList within an object class

Started bybobr <rraupach@att.net>
First post2016-12-13 09:02 -0800
Last post2016-12-13 17:56 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#36113 — Trying to set an ArrayList within an object class

Frombobr <rraupach@att.net>
Date2016-12-13 09:02 -0800
SubjectTrying to set an ArrayList within an object class
Message-ID<5707f5e9-7009-44e6-a072-5e06f8327816@googlegroups.com>
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);

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

.
.
.


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

public class GroupX {

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

etc...
.
.
.	

Any help would be appreciated. 
Bob R	

[toc] | [next] | [standalone]


#36114

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-12-13 12:47 -0500
Message-ID<o2pc5a$mbj$1@dont-email.me>
In reply to#36113
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)

[toc] | [prev] | [next] | [standalone]


#36115

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2016-12-13 17:56 +0000
Message-ID<o2pco0$ip3$1@dont-email.me>
In reply to#36113
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       |

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web