简体   繁体   中英

accessing elements of a linked list that is made up of a private custom class

in a project i am trying to get two or more agents to communicate between each other to collect things in the environment. to do this i am using a mailbox containing messages that they would respond to depending on the messages sent ebtween each other. below is where i create the linked list

mailbox = new LinkedList[numberOfAgents()];
    for ( int i=0; i< numberOfAgents(); i++ ){ 
        mailbox[i] = new LinkedList<Message>();
        mailbox[i].add(new Message(i, knownBloodBanks, knownDonors));
}

and then the private message and intention classes

private class Message 
{ 
    // instance variables 
    private int senderId;  
    private BoardComponentList donors; 
    private BoardComponentList bloodBanks; 
    private BoardComponent requestAssistanceAt; 
    private Intention iIntendToAssistAt; 

    public Message( int senderId, BoardComponentList d, BoardComponentList b ) 
    { 
        this.senderId = senderId;  
        donors = d; 
        bloodBanks = b; 
    } // end constructor 

    public void setIntentions( Intention intention ) 
    { 
        iIntendToAssistAt = intention; 
    } 

    public void setRequest( BoardComponent bC ) 
    { 
        requestAssistanceAt = bC; 
    } 

    public BoardComponentList getDonors() 
    { 
        return donors; 
    } 

    public BoardComponentList getBloodBanks() 
    { 
        return bloodBanks; 
    } 

    public Intention getIntentions() 
    { 
        return iIntendToAssistAt; 
    } 

    public BoardComponent getRequest() 
    { 
        return requestAssistanceAt; 
    } 

    public int getSenderId() 
    { 
        return senderId; 
    } 


} // end Message class 

private class Intention
{
    // instance variables 
    private BoardComponent target; 
    private double distanceTo; 

    public Intention( BoardComponent bC, double distance ) 
    { 
        target = bC; 
        distanceTo = distance; 
    } // end constructor 


    public BoardComponent getTarget() 
    { 
        return target; 
    } 

    public double getDistancetoTarget() 
    { 
        return distanceTo;
    } 

}

i cant for the life of me figure out how to access the methods inside the private class so that i can set goals and see the messages between agents. any help or pointers in the right direction would be greatly appreciated, i hope i've included enough info if not please let me know anything else needed

i didnt explain myself clearly as i so often find problems with but yes both the private classes and the first code snippet are found inside a public class

  1. There can be only one public class per file.
  2. The name of the file name(program name) should match with the name of the pubic class.
  3. There can be multiple private classes inside your public class as inner classes.

Sample code below ( modified your classes a bit because they we giving compilation error):

package com.pkg1;

import java.util.LinkedList;

public class Sample{


    public Sample(){
        LinkedList[]    mailbox = new LinkedList[10];
        for ( int i=0; i< 10; i++ ){ 
            mailbox[i] = new LinkedList<Message>();
            mailbox[i].add(new Message(i));
    }
    }
    private class Message 
    { 
        // instance variables 
        private int senderId;  

        private Intention iIntendToAssistAt; 

        public Message( int senderId ) 
        { 
            this.senderId = senderId;  

        } // end constructor 

        public void setIntentions( Intention intention ) 
        { 
            iIntendToAssistAt = intention; 
        } 



        public Intention getIntentions() 
        { 
            return iIntendToAssistAt; 
        } 


        public int getSenderId() 
        { 
            return senderId; 
        } 


    } // end Message class 

    private class Intention
    {
        // instance variables 

        private double distanceTo; 

        public Intention( double distance ) 
        { 
            distanceTo = distance; 
        } // end constructor 


        public double getDistancetoTarget() 
        { 
            return distanceTo;
        } 

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM