多次打印我的ArrayList内容有问题

时间:2017-04-29 02:51:15

标签: java arraylist

这是我第一次在这里发帖,而且我还是很擅长编程(不久前刚刚开始使用Java),所以你们可以给我一些关于如何解决我的问题的建议将不胜感激!我希望我在第一篇文章中遵循这些指导原则!

在这个项目的宏伟计划中,我试图对我的“团队”进行排序,以确定谁拥有与马拉松相关的最多“圈数,骑手和捐赠”。 Team类由Rider对象组成。

这是我使用Team类中存在的方法迭代我的Team数组的循环。

ArrayList<Team> team = new ArrayList<Team>();

for(int i=0; i<team.size(); i++){
        team.get(i).mostDonations(team);
        team.get(i).mostLaps(team);
        team.get(i).mostRiders(team);
    }

这是我的团队课程。它充满了所需的构造函数,设置器和getter,以及计算总数的方法。但我认为下面应该是相关的代码行。 (如果我要留下任何东西,请告诉我!)

public class Team {
private String teamName;
private String teamAbbreviation;
private ArrayList<Riders> riders = new ArrayList<Riders>();
DecimalFormat decimal = new DecimalFormat("#0.00");

public double totalDonations(){
    double totalDonations = 0;
    for(int i=0; i < riders.size(); i++){
        totalDonations += riders.get(i).getAmount();
    }
    return totalDonations;
}

public int totalLaps(){
    int totalLaps = 0;
    for (int i = 0; i < riders.size(); i++){
        totalLaps += riders.get(i).getLaps();
    }
    return totalLaps;
}

public int totalRiders(){
    int totalRiders = 0;
    for (int i =0; i < riders.size(); i++){
        totalRiders += riders.size();
    }
    return totalRiders;
}

public void mostLaps(ArrayList<Team> Teams){
    int max = 0;
    int indexOfMax = 0;
    for(int i=0; i < Teams.size(); i++){
        if(Teams.get(i).totalDonations() > max){
            max = Teams.get(i).totalLaps();
            indexOfMax = i;
        }
    }
    System.out.println("The team with the most laps is: " + Teams.get(indexOfMax).getName() + "!");
}

public void mostRiders(ArrayList<Team> Teams){
    int max = 0;
    int indexOfMax = 0;
    for(int i=0; i < Teams.size(); i++){
        if(Teams.get(i).totalRiders() > max){
            max = Teams.get(i).totalRiders();
            indexOfMax = i;
        }
    }
    System.out.println("The team with the most riders is: " + Teams.get(indexOfMax).getName() + "!");
}

public void mostDonations(ArrayList<Team> Teams){
    double max = 0.0;
    int indexOfMax = 0;
    for(int i=0; i < Teams.size(); i++){
        if(Teams.get(i).totalDonations() > max){
            max = Teams.get(i).totalDonations();
            indexOfMax = i;
        }
    }
    System.out.println("The team with the most donations is: " + Teams.get(indexOfMax).getName() + "!");
}

这是我的Rider课程。

public class Riders {

private String name;
private String hometown;
private String id;
private int laps;
private double amountRaised;

public Riders(){
    name = "";
    hometown = "";
    id = "";
    laps = 0;
    amountRaised = 0.0;
}

public Riders(String Name, String Hometown, String ID, int Laps){
    name = Name;
    hometown = Hometown;
    id = ID;
    laps = Laps;
    amountRaised = 0.0;
}

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

public void setHometown(String Hometown){
    hometown = Hometown;
}

public void setAbbreviation(String ID){
    id = ID;
}

public void setLaps(int Laps){
    laps = Laps;
}

public void setAmount(double Amount){
    amountRaised = Amount;
}

public String getName(){
    return name;
}

public String getHometown(){
    return hometown;
}

public String getID(){
    return id;
}

public int getLaps(){
    return laps;
}

public double getAmount(){
    return amountRaised;
}
public String toString(){
    String s = "Rider Name: " + name + "\nHometown: " + hometown + "\nID: " + id + "\nLaps: " + laps;
    return s;
}

}

然而,输出如下所示:

  

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

     

捐赠最多的团队是:斑马军队成员!

     

圈数最多的球队是:Xtremely Together!

     

拥有最多车手的车队是:斑马军队成员!

我很困惑为什么它一遍又一遍地打印相同的三行。我希望有人能指出我正确的方向。

2 个答案:

答案 0 :(得分:0)

以下循环遍历SELECT q2."DATE", NVL(q."TOTAL AMOUNT FUNDED PER DAY", 0) AS "TOTAL AMOUNT FUNDED PER DAY" FROM ( SELECT TRUNC (funddate, 'DD') "DATE", NVL(SUM (amount),0) "TOTAL AMOUNT FUNDED PER DAY" FROM purchases WHERE empid= 1234 AND amount < 0 GROUP BY TRUNC (funddate, 'DD') ) q RIGHT JOIN ( SELECT TRUNC (SYSDATE - ROWNUM) "DATE" FROM DUAL CONNECT BY ROWNUM < 100 ) q2 on q2."DATE" = q."DATE" ,并为每个ArrayList<Team>项打印三行。

Team

现在,您也通过上述循环传递for(int i=0; i<team.size(); i++){ team.get(i).mostDonations(team); team.get(i).mostLaps(team); team.get(i).mostRiders(team); }

team

更新:快速方法是删除调用这三种方法的for循环。

for(int i=0; i < Teams.size(); i++){
    if(Teams.get(i).totalDonations() > max){
        max = Teams.get(i).totalDonations(); // always goes for the max team object
        indexOfMax = i;
    }
}

我将验证这对您的代码有多高效。

答案 1 :(得分:0)

您对编程的了解越多,设计就越重要。我仍然认为你的课程设计糟糕。我已经创建了一个小应用来展示更好的设计。

  

团队课程:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication16;

import java.util.*;

/**
 *
 * @author Sedrick
 */
public class Team {

    private String teamName;
    private List<String> playerName;
    private List<Integer> age;
    private int numOfMembers;
    private double membersAverageAge;

    public Team(String teamName, List<String> playerName, List<Integer> age)
    {
        this.teamName = teamName;
        this.playerName = playerName;
        this.age = age;
        this.numOfMembers = playerName.size();
        this.membersAverageAge = caculateAvgAge(this.age);
    }

    private static double caculateAvgAge(List<Integer> age)
    {
        int sum = 0;

        if (age.size() > 0) {
            for (int i = 0; i < age.size(); i++) {
                sum += age.get(i);
            }
        }
        else {
            return Double.NaN;
        }

        return (double) sum / (double) age.size();
    }

    /**
     * @return the teamName
     */
    public String getTeamName()
    {
        return teamName;
    }

    /**
     * @param teamName the teamName to set
     */
    public void setTeamName(String teamName)
    {
        this.teamName = teamName;
    }

    /**
     * @return the playerName
     */
    public List<String> getPlayerName()
    {
        return playerName;
    }

    /**
     * @param playerName the playerName to set
     */
    public void setPlayerName(List<String> playerName)
    {
        this.playerName = playerName;
    }

    /**
     * @return the age
     */
    public List<Integer> getAge()
    {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(List<Integer> age)
    {
        this.age = age;
    }

    /**
     * @return the numOfMembers
     */
    public int getNumOfMembers()
    {
        return numOfMembers;
    }

    /**
     * @param numOfMembers the numOfMembers to set
     */
    public void setNumOfMembers(int numOfMembers)
    {
        this.numOfMembers = numOfMembers;
    }

    /**
     * @return the membersAverageAge
     */
    public double getMembersAverageAge()
    {
        return membersAverageAge;
    }

    /**
     * @param membersAverageAge the membersAverageAge to set
     */
    public void setMembersAverageAge(double membersAverageAge)
    {
        this.membersAverageAge = membersAverageAge;
    }

}
  

主类:

import java.util.*;

/**
 *
 * @author Sedrick
 */
public class JavaApplication16 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        List<String> cowPlayer = new ArrayList();
        List<Integer> cowPlayerAge = new ArrayList();
        cowPlayer.add("Tim");
        cowPlayerAge.add(20);
        cowPlayer.add("Jane");
        cowPlayerAge.add(23);
        cowPlayer.add("Kim");
        cowPlayerAge.add(25);
        cowPlayer.add("Chris");
        cowPlayerAge.add(10);

        Team cow = new Team("Cows", cowPlayer, cowPlayerAge);

        List<String> frogPlayer = new ArrayList();
        List<Integer> frogPlayerAge = new ArrayList();
        frogPlayer.add("Dana");
        frogPlayerAge.add(21);
        frogPlayer.add("Jack");
        frogPlayerAge.add(19);
        frogPlayer.add("Jill");
        frogPlayerAge.add(30);

        Team frog = new Team("Frogs", frogPlayer, frogPlayerAge);

        System.out.println(cow.getTeamName() + " info");
        System.out.println("\tNumber of members: " + cow.getNumOfMembers());
        System.out.println("\tNumber of members: " + cow.getMembersAverageAge());
        System.out.println("\tPlayers Name and Age");
        for (int i = 0; i < cow.getPlayerName().size(); i++) {
            System.out.println("\t\t" + cow.getPlayerName().get(i) + " - " + cow.getAge().get(i));
        }

        System.out.println(frog.getTeamName() + " info");
        System.out.println("\tNumber of members: " + frog.getNumOfMembers());
        System.out.println("\tNumber of members: " + frog.getMembersAverageAge());
        System.out.println("\tPlayers Name and Age");
        for (int i = 0; i < frog.getPlayerName().size(); i++) {
            System.out.println("\t\t" + frog.getPlayerName().get(i) + " - " + frog.getAge().get(i));
        }

        List<Team> teams = new ArrayList();
        teams.add(cow);
        teams.add(frog);

        System.out.println("Team with the most players: " + whichTeamHasTheMostPlayers(teams));
        System.out.println("Team whose players average age is higher: " + whichTeamHasTheHighestAverageAge(teams));

    }

    //If there are more than one team with the same number of players this will not work correctly.
    static private String whichTeamHasTheMostPlayers(List<Team> teams)
    {

        int mostPlayersIndex = 0;//assume the first team has the most players;
        int amountOfPlayers = teams.get(0).getNumOfMembers();
        for (int i = 1; i < teams.size(); i++) {
            if (teams.get(i).getNumOfMembers() > amountOfPlayers) {
                mostPlayersIndex = i;
                amountOfPlayers = teams.get(i).getNumOfMembers();
            }
        }

        return teams.get(mostPlayersIndex).getTeamName();
    }

    //If there are more than one team with the same average this will not work correctly.
    static private String whichTeamHasTheHighestAverageAge(List<Team> teams)
    {
        int highestAvgIndex = 0;//assume the first team has the most players;
        double teamAvg = teams.get(0).getMembersAverageAge();
        for (int i = 1; i < teams.size(); i++) {
            if (teams.get(i).getMembersAverageAge() > highestAvgIndex) {
                highestAvgIndex = i;
                teamAvg = teams.get(i).getMembersAverageAge();
            }
        }

        return teams.get(highestAvgIndex).getTeamName();
    }

}