java - 编辑后继续打印整个数组列表

标签 java arraylist

不知道为什么,第一次打印数据后,只一一打印出带有投票的名字,但修改后,由于某种原因,它打印出了整个数组列表。

测试人员类别:

import java.util.ArrayList;
public class ElectionTesterV4 {
    public static void main(String[] args) {
        int sum = 0, counter = 0;
        ArrayList<Candidate4> c = new ArrayList<Candidate4>();

        Candidate4 John = new Candidate4("John Smith", 5000);
        c.add(John);
        Candidate4 Lucy = new Candidate4("Lucy Robertson", 8000);
        c.add(Lucy);
        Candidate4 Marie = new Candidate4("Marie Grace", 7000);
        c.add(Marie);
        Candidate4 Raymond = new Candidate4("Raymond Zhang", 10000);
        c.add(Raymond);
        Candidate4 JohnD = new Candidate4("John Doe", 2500);
        c.add(JohnD);
        Candidate4 JuanG = new Candidate4("Juan Garcia", 6000);

        System.out.println("Raw Election Data: ");
        System.out.println("--------------------------------");
        for(Candidate4 ca : c) {
            System.out.println(ca.toString());
            sum += ca.getVotes();
            counter++;
        }
        System.out.println();
        System.out.println("Election Results");
        System.out.println("--------------------------------");
        System.out.println("Candidate               Votes Received                      % of Total Votes");
        for(Candidate4 ca : c) {
            System.out.printf("%15s                %5d                         %2.2f\n", 
        ca.getName(), ca.getVotes(), ((double)ca.getVotes() / sum) * 100);
        }
        System.out.println();
        System.out.println("Total number of votes cast in election is: "+sum);
        System.out.println();
        John.replaceName(JuanG);
        System.out.println();

        sum = 0;

        System.out.println("Raw Election Data: ");
        System.out.println("--------------------------------");
        for(Candidate4 ca : c) {
            System.out.println(c.toString());
            sum += ca.getVotes();
            counter++;
        }
        System.out.println();
        System.out.println("Election Results");
        System.out.println("--------------------------------");
        System.out.println("Candidate               Votes Received                      % of Total Votes");
        for(Candidate4 ca : c) {
            System.out.printf("%15s                %5d                         %2.2f\n", 
        ca.getName(), ca.getVotes(), ((double)ca.getVotes() / sum) * 100);
        }
        System.out.println();
        System.out.println("Total number of votes cast in election is: "+sum);
        System.out.println();
        John.replaceVotes(6000);
        System.out.println();
        sum = 0;

        System.out.println("Raw Election Data: ");
        System.out.println("--------------------------------");
        for(Candidate4 ca : c) {
            System.out.println(c.toString());
            sum += ca.getVotes();
            counter++;
        }
        System.out.println();
        System.out.println("Election Results");
        System.out.println("--------------------------------");
        System.out.println("Candidate               Votes Received                      % of Total Votes");
        for(Candidate4 ca : c) {
            System.out.printf("%15s                %5d                         %2.2f\n", 
        ca.getName(), ca.getVotes(), ((double)ca.getVotes() / sum) * 100);
        }
        System.out.println();
        System.out.println("Total number of votes cast in election is: "+sum);
    }
}

方法类:

public class Candidate4 {
    // instance variables
    private int numVotes;
    private String name;

    // Constructor for objects of class Candidate
    public Candidate4(String name, int numVotes) {
        // initialize instance variables
        this.name = name;
        this.numVotes = numVotes;
    }

    public String getName() {
        return name;
    }

    public int getVotes() {
        return numVotes;
    }

    public void setVotes(int n) {
        numVotes = n;
    }

    public void setName(String n) {
        name = n;
    }

    public String toString() {
        return name + " received " + numVotes + " votes.";
    }

    public void replaceName(String n) { //replaces name
        System.out.println("Changing " + name + "'s name to " + n);
        name = n;
    }

    public void replaceVotes(int v) { //replaces votes)
        System.out.println("Changing " + name + "'s votes to " + v);
        numVotes = v;
    }

    public void replaceName(Candidate4 c) { //replaces name
        System.out.println("Changing " + name + "'s name to " + c.getName());
        name = c.getName();
    }

    public void replaceVotes(Candidate4 c) { //switches votes
        System.out.println("Replacing " + name + "'s votes with " + c.getName());
        numVotes = c.getVotes();
    }
}

我已经使用与此类似的代码逐个对象地打印出数组列表,所以我不知道为什么这次它在循环中的每次迭代中继续打印出整个数组列表。

最佳答案

错误地输入了:

System.out.println(c.toString());

而不是

System.out.println(ca.toString());

关于java - 编辑后继续打印整个数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015243/

相关文章:

java - SAAJ请求xml,信封前缀自行更改

java - 关于 WSDL 的 Rest 与 SOAP

java - 将 Countrylist 模型、数据和 Arraylist 从 Java 重写为 kotlin

java - 将新元素放入 arrayList 中,而不是放在其头部

java - 如何将字符串(HashMap的ArrayList)转换为ArrayList

java - 同时对两个数组列表进行排序

java - 如何在JunitPerf中使用@BeforeClass和@AfterClass?

java - 使用正则表达式解析此日期时间字符串

java - TreeSet 如何作为成员变量在 Enum Singleton 中处理

java - Android on Back Pressed for multiple child activity 不工作