Java 如何从具有数组列表的类调用方法到另一个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20462267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:07:17  来源:igfitidea点击:

How do I call methods from a class with an arraylist to another class?

javamethodsarraylistbluej

提问by user3081319

I'm in my first semester of Java and I need help in calling methods from the VotingMachine class below to the Candidate Class. The Voting Machine class is compiling properly. Thank you all for any help you can provide.... Mercedes

我正在学习 Java 的第一学期,我需要帮助将方法从下面的 VotingMachine 类调用到 Candidate 类。投票机类正在正确编译。感谢大家提供的任何帮助......梅赛德斯

import java.util.ArrayList;

/**
 * These are the fields for the Voting Machine Class.
 */
public class VotingMachine
{
    private ArrayList<String> candidateList;

    /**
     * The following constructor will establish the Candidate List
     */
    public VotingMachine()
    {
        candidateList = new ArrayList<String>();
    }

    /**
     * This constructor will store the Candidates for the Candidate List   
     */
    public void setCandidateList()
    {
        candidateList.add("Darnell Woffard");
        candidateList.add("Barack Obama");
        candidateList.add("Hillary Clinton");
    }    

    /**
     * This method will display the entire Candidate List.
     */
    public void printCandidateInfo()
    {
        for (int index=0; index < candidateList.size(); index++)
        {
            System.out.println(candidateList.get(index));
        }
    }

    /**
     * Method to the number of Candidates in the CandidateList Arraylist.
     */
    public int getNumberofFiles()
    {
        return candidateList.size();       
    }

   /**
    * Method to select one candidate by first providing an index number.
    */
   public void listFile(int index)
   {
       if(index >= 0 && index < candidateList.size()){
           String filename = candidateList.get(index);
           System.out.println(filename);
       }
   }

    /**
     * This method will enable a user to remove a candidate.
     */
    public void removeFile(int index)
    {
        if(index >= 0 && index < candidateList.size()){
            candidateList.remove(index);
        }
    }

    /**
     * This method will add a file to the Candidate List.
     * 
     */
    public void addCandidate(String filename)
    {
       candidateList.add(filename);
    }


//----------
//The Candidate Class:

public class Candidate{

    private String name;
    private char party;
    private String candidateList;
// Add fields
    /**
     * Fields
     * name - Candidate's name, stored in a String
     * party - Candidate's political party, stored in a char
     * as 'r' for Republican, 'd' for Democrat, and 'i' for Independent
     */

    /**
     * Constructor
     * 
     * @param anyName - caller inputs Candidate name
     * @param anyParty - caller inputs Candidate's party affiliation
     * stored as a char
     * chars are assigned with single quotes.
     */
    public Candidate(String anyName, char anyParty)
    {
        name = anyName;
        party = anyParty;   
    }

    /**
     * The method will enable method calls from the Voting Machine Class.
     */
    public void main(String candidateList)
    {
        VotingMachine votingMachine = new VotingMachine();
    }

            /**
             * This method will define the candidates party affiliation.
             * public char setParty()
             */


//Complete the three methods and their comments.    
    /**
     * Method to retrieve the Candidate's name for the caller.
     * public String getName(String anyName)
     * 
     */



    /**
     * Method to retrieve the Candidate's party for the caller.
     * 
     * @return
     */



    /**
     * Method to change the Candidate's party
     * 
     * @param 
     */

回答by Narmeen25

Actually what i got from this is you are trying to make a voting machine. VotingMachine is the main class here having info of different candidates. so we will make object of candidate in votingMachine class. Note: when we are supposed to make a java project, figure out what is it main class and subclass that means which depends on which. in the above example There is association in the classes. First of all declare an ArrayList for storing objects of candidate class. as shown below.

实际上,我从中得到的是您正在尝试制作投票机。VotingMachine 是这里的主要类,其中包含不同候选人的信息。所以我们将在votingMachine 类中制作候选对象。注意:当我们应该做一个java项目时,弄清楚它是什么主类和子类,这意味着哪个取决于哪个。在上面的例子中,类中有关联。首先声明一个ArrayList,用于存储候选类的对象。如下所示。

private ArrayList<candidate> candidateList;

/**
 * The following constructor will establish the Candidate List
 */
public VotingMachine()
{
    candidateList = new ArrayList<String>();
}

now for adding new candidate in the ArrayList I have modified your method setCandidate()as

现在为了在 ArrayList 中添加新的候选者,我已将您的方法修改 setCandidate()

public void addNewCandidate(String name, char partySymbol)
{
    candidate candid = new candidate(name, partySymbol);// this will call the candidate constructor
    candidateList.add(candid);//add that object in ArrayList


}    

As ArrayList stores references of objects, the built-in function int get(int index)will return the reference of the object. to print the info of that object or you can say values, we should define a function as getName()and getParty(). instead of this System.out.println(candidateList.get(index));you should call System.out.println(candidateList.get(index).getName());and System.out.println(candidateList.get(index).getParty());in the following method

由于 ArrayList 存储对象的引用,因此内置函数int get(int index)将返回对象的引用。要打印该对象的信息或者您可以说值,我们应该将函数定义为getName()and getParty()。取而代之的是System.out.println(candidateList.get(index));,你应该调用System.out.println(candidateList.get(index).getName());,并 System.out.println(candidateList.get(index).getParty());在下面的方法

public void printCandidateInfo()
{
    for (int index=0; index < candidateList.size(); index++)
    {
        System.out.println(candidateList.get(index));
    }
}

so define functions in candidate class as

因此将候选类中的函数定义为

public String getName()
{
    return name;

}

/**
 * Method to retrieve the Candidate's party for the caller.
 * 
 * @return
 */
public char getParty()
{
    return party;

}

the following method will print the reference not the info of candidate, so modify it as described above

以下方法将打印参考而不是候选人的信息,因此如上所述修改它

public void listFile(int index)
{
      if(index >= 0 && index < candidateList.size()){
       String filename = candidateList.get(index);
       System.out.println(filename);
   }

}

}

as i have modified it,

因为我已经修改过了,

import java.util.ArrayList;

/**
* These are the fields for the Voting Machine Class.
*/

public class VotingMachine
{
private ArrayList<Candidate> candidateList;

/**
 * The following constructor will establish the Candidate List
 */
public VotingMachine()
{
    candidateList = new ArrayList<>();
}

/**
 * This method will store the Candidates for the Candidate List   
 */
public void addNewCandidate(String name, char partySymbol)
{
    Candidate candid = new Candidate(name, partySymbol);// this will call the candidate constructor
    candidateList.add(candid);//add that object in ArrayList
}    

/**
 * This method will display the entire Candidate List.
 */
public void printCandidateInfo()
{
    for (int index=0; index < candidateList.size(); index++)
    {
        System.out.print(candidateList.get(index).getName());
        System.out.println("  " + candidateList.get(index).getParty());
    }
}

/**
 * Method to the number of Candidates in the CandidateList Arraylist.
 */
public int getNumberofFiles()
{
    return candidateList.size();       
}

/**
* Method to select one candidate by first providing an index number.
*/
public void listFile(int index)
{
   System.out.print(candidateList.get(index).getName());
   System.out.println("  " + candidateList.get(index).getParty());
}

/**
 * This method will enable a user to remove a candidate.
 */
public void removeFile(int index)
{
    if(index >= 0 && index < candidateList.size()){
        candidateList.remove(index);
    }
}

}

}

in candidate class i have just added the above mentioned getName()and getParty()methods..

在候选班级中,我刚刚添加了上述getName()getParty()方法..

regards

问候