猿问

如何声明并添加到全局数组列表?

我有一个将整数值输入全局数组的项目。我正在使用 ArrayList 并且似乎无法弄清楚如何将值添加到数组中我不太确定如何将整数添加到数组中,因为它是全局的而 actionPerformed 函数是本地的。另外,声明数组的位置是否正确?


private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       

    int intEnter = Integer.parseInt(this.txtEnter.getText());

}                                      



public static void main(String args[]) {

    ArrayList <Integer> intMarks = new ArrayList();

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {

            new MarksCalculator().setVisible(true);

        }

    });

}


绝地无双
浏览 147回答 4
4回答

拉莫斯之舞

该数组应声明为类的静态字段。private static ArrayList <Integer> intMarks;private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;int intEnter = Integer.parseInt(this.txtEnter.getText());&nbsp; &nbsp;intMarks.add(intEnter);}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;public static void main(String args[]) {&nbsp; &nbsp; intMarks = new ArrayList();&nbsp; &nbsp; java.awt.EventQueue.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new MarksCalculator().setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}

慕哥6287543

在您的代码中,arraylistintMarks不是全局的,它是main方法的局部变量。要使其成为全局变量,您必须使其成为您班级的直接变量。class ClassName{&nbsp; &nbsp; private static ArrayList <Integer> intMarks;&nbsp;&nbsp; &nbsp; private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int intEnter = Integer.parseInt(this.txtEnter.getText());&nbsp; &nbsp; &nbsp; &nbsp; intMarks.add(intEnter);&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; intMarks = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; java.awt.EventQueue.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new MarksCalculator().setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}此外,您必须使静态在方法intMarks中使用它main

翻过高山走不出你

您可以使用单例设计模式。public class Singleton {private ArrayList<Object> arrayList;private static Singleton instance;private Singleton(){    arrayList = new ArrayList<Object>();}public static Singleton getInstance(){    if (instance == null){        instance = new Singleton();    }return instance;}public ArrayList<Object> getArrayList() {   return arrayList;}}您可以在任何课程中增加价值。public class ExampleClass{Singleton.getInstance().getArrayList().add(sampleObject);}

海绵宝宝撒

如果 arraylist 是全局的,那么它可以在同一类的任何函数中的任何地方访问。虽然您可以添加 mArrayListName.add(1); 之类的值&nbsp;即对于int类型的arraylist
随时随地看视频慕课网APP

相关分类

Java
我要回答