试图编写一个方法来创建一个数组列表

时间:2016-06-06 23:48:10

标签: java user-interface arraylist methods listbox

我正在使用Netbeans / GUI制作一个梦幻篮球游戏程序。我正在尝试创建创建arraylists的方法,这样我每次需要它时都可以为我的一个按钮事件调用数组的方法,而不是在每个单独的事件下重新编码数组。数组列表方法从我在设计窗口中创建的列表框中加载团队名称的数据。代码如下:

public class JFMarchMadness extends javax.swing.JFrame {

    public JFMarchMadness() 
    {
        initComponents();
    }

    //creates arraylist and loads up data from arraylist of type string (lstConfTeams)
    void createCTArrayList ()
    {
        ArrayList<BasketballTeam> alCTeams=new ArrayList <>();

        String CTteamName;
        String CTstatus="";
        int CTwins=0;
        int CTlosses=0;
        double CTwinLossPct=0;

        for (int iCount = 0; iCount <lstConfTeams.getModel().getSize(); iCount++)
        {
            CTteamName=lstConfTeams.getModel().getElementAt(iCount);
            alCTeams.add(new BasketballTeam(CTstatus,CTteamName,CTwins,CTlosses,CTwinLossPct));
        }

    }

    private void btnPlayMouseClicked(java.awt.event.MouseEvent evt) {                                     
    // call createCTArrayList method and begin for loop


        createCTArrayList ();

        int iCTCount;

        for (iCTCount=0; iCTCount<alCTeams.size; iCTCount++){
        //
        }
    }

现在,当我尝试在for循环语句中调用它时,它会找不到arraylist:alCTeams。我是否错误地编写了方法,或者我调用的方法不正确?

3 个答案:

答案 0 :(得分:0)

如果您希望alCTeams对其他方法可见,那么它应该是类成员而不是局部变量。

答案 1 :(得分:0)

class的声明应位于method而不是public class JFMarchMadness extends javax.swing.JFrame { ArrayList<BasketballTeam> alCTeams=new ArrayList <>(); public JFMarchMadness() { initComponents(); } //creates arraylist and loads up data from arraylist of type string (lstConfTeams) void createCTArrayList () { String CTteamName; String CTstatus=""; int CTwins=0; int CTlosses=0; double CTwinLossPct=0; for (int iCount = 0; iCount <lstConfTeams.getModel().getSize(); iCount++) { CTteamName=lstConfTeams.getModel().getElementAt(iCount); alCTeams.add(new BasketballTeam(CTstatus,CTteamName,CTwins,CTlosses,CTwinLossPct)); } } private void btnPlayMouseClicked(java.awt.event.MouseEvent evt) { // call createCTArrayList method and begin for loop createCTArrayList (); int iCTCount; for (iCTCount=0; iCTCount<alCTeams.size; iCTCount++){ // } } 下。

像这样:

|

答案 2 :(得分:0)

您尚未在btnPlayMouseClicked方法中声明alCTeams。您可以通过从createCTArrayList

返回对象来传递实例ogf
public class JFMarchMadness extends javax.swing.JFrame {

    public JFMarchMadness() 
    {
        initComponents();
    }

    //creates arraylist and loads up data from arraylist of type string (lstConfTeams)
    ArrayList<BasketballTeam> createCTArrayList ()
    {
        ArrayList<BasketballTeam> alCTeams=new ArrayList <>();

        String CTteamName;
        String CTstatus="";
        int CTwins=0;
        int CTlosses=0;
        double CTwinLossPct=0;

        for (int iCount = 0; iCount <lstConfTeams.getModel().getSize(); iCount++)
        {
            CTteamName=lstConfTeams.getModel().getElementAt(iCount);
            alCTeams.add(new BasketballTeam(CTstatus,CTteamName,CTwins,CTlosses,CTwinLossPct));
        }
        return alCTeams;
    }

    private void btnPlayMouseClicked(java.awt.event.MouseEvent evt) {                                     
    // call createCTArrayList method and begin for loop


        ArrayList<BasketballTeam> alCTeams = createCTArrayList ();

        int iCTCount;

        for (iCTCount=0; iCTCount<alCTeams.size; iCTCount++){
        //
        }
    }