查找ArrayList

时间:2017-08-23 02:12:31

标签: java arraylist average

我目前正在为假篮球运动员制作模拟器,但我目前仍然坚持找到ArrayList中所有数字的平均值

我知道如何添加所有值但是当我尝试使用计数器变量来划分列表或使用list.getSize()时,它们都不起作用。

希望这对你有意义,我很乐意更详细地解释这个问题。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Application {
    private JFrame frame;
    private JTextField pst;
    private JTextField pg;
    private JTextField sg;
    private JTextField pf;
    private JTextField C;
    private JTextField sf;
    private JTextField txtPlayerName;
    private JTextField ED;
    private JTextField CJ;
    private JTextField SB;
    private JTextField SW;
    private JTextField JO;
    private JTextField num;
    private JTextField edNum;
    private JTextField cjNum;
    private JTextField sbNum;
    private JTextField swNum;
    private JTextField joNum;
    private JTextField ppg;
    private JTextField ppgED;
    private JTextField ppgCJ;
    private JTextField ppgSB;
    private JTextField ppgJO;
    private JTextField ppgSW;
    private JButton btnPlay;
    private List<Integer> ppgList;
    private int sum;
    private int counter;

    // Launch the application.
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Application window = new Application();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Application() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        initComponenets();
        createEvents();
    }

    ////////////////////////////////////////////////////////////////////////
    //Contains all of the code for creating events
    private void createEvents() {

    }
    //Contains all of the code for creating and initializing components
    private void initComponenets() {

        ////////////////////////////////////////////////////////////Frame
        frame = new JFrame();
        frame.setTitle("Title");
        frame.setBounds(100, 100, 801, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        ////////////////////////////////////////////////////////////Title
        JTextPane title = new JTextPane();
        title.setBackground(Color.LIGHT_GRAY);
        title.setEditable(false);
        title.setFont(new Font("Tahoma", Font.PLAIN, 20));
        title.setForeground(Color.DARK_GRAY);
        title.setBounds(237, 11, 296, 50);
        title.setText("   Basketball Simulation Game");
        frame.getContentPane().add(title);

    /////////////////////////////////////////////////////////// Position TextBox
        pst = new JTextField();
        pst.setHorizontalAlignment(SwingConstants.CENTER);
        pst.setEditable(false);
        pst.setFont(new Font("Tahoma", Font.BOLD, 14));
        pst.setText("PST");
        pst.setBounds(10, 64, 50, 35);
        frame.getContentPane().add(pst);
        pst.setColumns(10);

        pg = new JTextField();
        pg.setHorizontalAlignment(SwingConstants.CENTER);
        pg.setText("PG");
        pg.setFont(new Font("Tahoma", Font.BOLD, 14));
        pg.setEditable(false);
        pg.setColumns(10);
        pg.setBounds(10, 99, 50, 50);
        frame.getContentPane().add(pg);

        sg = new JTextField();
        sg.setHorizontalAlignment(SwingConstants.CENTER);
        sg.setText("SG");
        sg.setFont(new Font("Tahoma", Font.BOLD, 14));
        sg.setEditable(false);
        sg.setColumns(10);
        sg.setBounds(10, 148, 50, 50);
        frame.getContentPane().add(sg);

        sf = new JTextField();
        sf.setText("SF");
        sf.setHorizontalAlignment(SwingConstants.CENTER);
        sf.setFont(new Font("Tahoma", Font.BOLD, 14));
        sf.setEditable(false);
        sf.setColumns(10);
        sf.setBounds(10, 196, 50, 50);
        frame.getContentPane().add(sf);

        pf = new JTextField();
        pf.setText("PF");
        pf.setHorizontalAlignment(SwingConstants.CENTER);
        pf.setFont(new Font("Tahoma", Font.BOLD, 14));
        pf.setEditable(false);
        pf.setColumns(10);
        pf.setBounds(10, 242, 50, 50);
        frame.getContentPane().add(pf);

        C = new JTextField();
        C.setText("C");
        C.setHorizontalAlignment(SwingConstants.CENTER);
        C.setFont(new Font("Tahoma", Font.BOLD, 14));
        C.setEditable(false);
        C.setColumns(10);
        C.setBounds(10, 291, 50, 50);
        frame.getContentPane().add(C);


        /////////////////////////////////////////////// PlayerName
        txtPlayerName = new JTextField();
        txtPlayerName.setText("Player Name");
        txtPlayerName.setHorizontalAlignment(SwingConstants.CENTER);
        txtPlayerName.setFont(new Font("Tahoma", Font.BOLD, 14));
        txtPlayerName.setEditable(false);
        txtPlayerName.setColumns(10);
        txtPlayerName.setBounds(59, 64, 95, 35);
        frame.getContentPane().add(txtPlayerName);

        ED = new JTextField();
        ED.setText("ED");
        ED.setHorizontalAlignment(SwingConstants.CENTER);
        ED.setFont(new Font("Tahoma", Font.BOLD, 14));
        ED.setEditable(false);
        ED.setColumns(10);
        ED.setBounds(59, 99, 95, 50);
        frame.getContentPane().add(ED);

        CJ = new JTextField();
        CJ.setText("CJ");
        CJ.setHorizontalAlignment(SwingConstants.CENTER);
        CJ.setFont(new Font("Tahoma", Font.BOLD, 14));
        CJ.setEditable(false);
        CJ.setColumns(10);
        CJ.setBounds(59, 148, 95, 50);
        frame.getContentPane().add(CJ);

        SB = new JTextField();
        SB.setText("SB");
        SB.setHorizontalAlignment(SwingConstants.CENTER);
        SB.setFont(new Font("Tahoma", Font.BOLD, 14));
        SB.setEditable(false);
        SB.setColumns(10);
        SB.setBounds(59, 196, 95, 50);
        frame.getContentPane().add(SB);

        SW = new JTextField();
        SW.setText("Swanigan");
        SW.setHorizontalAlignment(SwingConstants.CENTER);
        SW.setFont(new Font("Tahoma", Font.BOLD, 14));
        SW.setEditable(false);
        SW.setColumns(10);
        SW.setBounds(59, 245, 95, 47);
        frame.getContentPane().add(SW);

        JO = new JTextField();
        JO.setText("Jordan");
        JO.setHorizontalAlignment(SwingConstants.CENTER);
        JO.setFont(new Font("Tahoma", Font.BOLD, 14));
        JO.setEditable(false);
        JO.setColumns(10);
        JO.setBounds(59, 291, 95, 50);
        frame.getContentPane().add(JO);

        /////////////////////////////////////////////Numbers
        num = new JTextField();
        num.setText("#");
        num.setHorizontalAlignment(SwingConstants.CENTER);
        num.setFont(new Font("Tahoma", Font.BOLD, 14));
        num.setEditable(false);
        num.setColumns(10);
        num.setBounds(153, 64, 50, 35);
        frame.getContentPane().add(num);

        edNum = new JTextField();
        edNum.setText("0");
        edNum.setHorizontalAlignment(SwingConstants.CENTER);
        edNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        edNum.setEditable(false);
        edNum.setColumns(10);
        edNum.setBounds(153, 99, 50, 50);
        frame.getContentPane().add(edNum);

        cjNum = new JTextField();
        cjNum.setText("3");
        cjNum.setHorizontalAlignment(SwingConstants.CENTER);
        cjNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        cjNum.setEditable(false);
        cjNum.setColumns(10);
        cjNum.setBounds(153, 148, 50, 50);
        frame.getContentPane().add(cjNum);

        sbNum = new JTextField();
        sbNum.setText("24");
        sbNum.setHorizontalAlignment(SwingConstants.CENTER);
        sbNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        sbNum.setEditable(false);
        sbNum.setColumns(10);
        sbNum.setBounds(153, 196, 50, 50);
        frame.getContentPane().add(sbNum);

        swNum = new JTextField();
        swNum.setText("8");
        swNum.setHorizontalAlignment(SwingConstants.CENTER);
        swNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        swNum.setEditable(false);
        swNum.setColumns(10);
        swNum.setBounds(153, 245, 50, 47);
        frame.getContentPane().add(swNum);

        joNum = new JTextField();
        joNum.setText("23");
        joNum.setHorizontalAlignment(SwingConstants.CENTER);
        joNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        joNum.setEditable(false);
        joNum.setColumns(10);
        joNum.setBounds(153, 291, 50, 50);
        frame.getContentPane().add(joNum);

        //////////////////////////////////Points per game
        ppg = new JTextField();
        ppg.setText("PPG");
        ppg.setHorizontalAlignment(SwingConstants.CENTER);
        ppg.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppg.setEditable(false);
        ppg.setColumns(10);
        ppg.setBounds(201, 64, 50, 35);
        frame.getContentPane().add(ppg);

        ppgED = new JTextField();
        ppgED.setText("0.0");
        ppgED.setHorizontalAlignment(SwingConstants.CENTER);
        ppgED.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgED.setEditable(false);
        ppgED.setColumns(10);
        ppgED.setBounds(201, 99, 50, 50);
        frame.getContentPane().add(ppgED);

        ppgCJ = new JTextField();
        ppgCJ.setText("0.0");
        ppgCJ.setHorizontalAlignment(SwingConstants.CENTER);
        ppgCJ.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgCJ.setEditable(false);
        ppgCJ.setColumns(10);
        ppgCJ.setBounds(201, 148, 50, 50);
        frame.getContentPane().add(ppgCJ);

        ppgSB = new JTextField();
        ppgSB.setText("0.0");
        ppgSB.setHorizontalAlignment(SwingConstants.CENTER);
        ppgSB.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgSB.setEditable(false);
        ppgSB.setColumns(10);
        ppgSB.setBounds(201, 196, 50, 50);
        frame.getContentPane().add(ppgSB);

        ppgJO = new JTextField();
        ppgJO.setText("0.0");
        ppgJO.setHorizontalAlignment(SwingConstants.CENTER);
        ppgJO.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgJO.setEditable(false);
        ppgJO.setColumns(10);
        ppgJO.setBounds(201, 291, 50, 50);
        frame.getContentPane().add(ppgJO);

        ppgSW = new JTextField();
        ppgSW.setText("0.0");
        ppgSW.setHorizontalAlignment(SwingConstants.CENTER);
        ppgSW.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgSW.setEditable(false);
        ppgSW.setColumns(10);
        ppgSW.setBounds(201, 245, 50, 47);
        frame.getContentPane().add(ppgSW);

        ////////////////////////////////////////Play JButton
        btnPlay = new JButton("PLAY");
        btnPlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getPPG(33, ppgED);
            }
        });
        btnPlay.setFont(new Font("Tahoma", Font.PLAIN, 24));
        btnPlay.setBounds(304, 344, 160, 56);
        frame.getContentPane().add(btnPlay);




    }

    public void getPPG(int num, JTextField box){

        Random rand = new Random();
        String text;
        ppgList = new ArrayList<Integer>();

        for(int xy = 0; xy < 1; xy++){

            num = num + rand.nextInt(10);
            ppgList.add(num);

        }

        for(int i = 0; i < ppgList.size(); i++)
        {
            counter++;
            sum = sum + ppgList.get(i) / counter;
        }
        Integer.toString(sum);
        text = String.valueOf(sum);
        box.setText(text);
    }
}

1 个答案:

答案 0 :(得分:2)

您当前的方法是添加一个项目除以当前计数器,这是不正确的。看一下下面的例子:

values:      1, 2, 3, 4, 5
average:     3

your result:   1/1 + 2/2 + 3/3 + 4/4 + 5/5
             = 1 + 1 + 1 + 1 + 1
             = 5

正确的方法首先计算所有值的总和,然后除以项目数量。

correct:  (1 + 2 + 3 + 4 + 5) / 5
         = 15 / 5
         = 3

或者您可以应用除法和加法的分配属性并获得等效表达式:

1/5 + 2/5 + 3/5 + 4/5 + 5/5

但是,您应该在int保持尽可能长的时间以避免精度问题并利用JVM的大整数优化。

您可以自己计算平均值,也可以只使用Java库中的方法,如IntStream#average

// Setup the list
Random rnd = new Random();
int amount = 1_000;
int valueUpperBound = 10;

ArrayList<Integer> values = new ArrayList<>();
for(int i = 0; i < amount; i++){
    values.add(rnd.nextInt(valueUpperBound));
}

// Compute the average
double avg = values.stream()
    .mapToInt(Integer::intValue)
    .average()
    .getAsDouble();

代码使用您的列表作为流的来源,然后使用Integer方法将每个值int映射到Integer#intValue,以获得IntStreamdocumentation)。此类提供IntStream#average等方法,返回OptionalDoubleOptionalDouble#getAsDouble方法返回结果(有关详细信息,请参阅文档)。

正确的手动平均计算看起来像(从这里:SO/a/10791597/2411243):

private double calculateAverage(List<Integer> values) {
    Integer sum = 0;
    if(!values.isEmpty()) {
        for (Integer value : values) {
            sum += value;
        }
        return sum.doubleValue() / values.size();
    }
    return sum;
}