我如何在2个内部匿名类中共享相同的ArrayList副本?

时间:2015-11-07 04:45:40

标签: java

请问,我如何在2个内部类中共享相同的ArrayList副本? picture of the GUI,我放了两个按钮:第一个到#34;添加患者"我做到了,但我必须添加什么功能"检索"在第一个按钮中注册了相同值的按钮?

package my.firstProject;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

public class PatientRecordUI extends javax.swing.JFrame {

    /**
     * Creates new form PatientRecordUI
     */
    public PatientRecordUI() {
        initComponents();
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        // TODO add your handling code here:
        String first, second, last;
        int bMonth, bDay, bYear, aMonth, aDay, aYear, fileNo;
        first = jTextField1.getText();
        second = jTextField2.getText();
        last = jTextField3.getText();

        bMonth = Integer.parseInt(jTextField4.getText());
        bDay = Integer.parseInt(jTextField5.getText());
        bYear = Integer.parseInt(jTextField6.getText());

        aMonth = Integer.parseInt(jTextField7.getText());
        aDay = Integer.parseInt(jTextField8.getText());
        aYear = Integer.parseInt(jTextField9.getText());
        fileNo = Integer.parseInt(jTextField10.getText());

        Date birth = new Date(bMonth, bDay, bYear);
        Date admissionDate = new Date(aMonth, aDay, aYear);
        Patient patient = new Patient(first, second, last, fileNo, birth, admissionDate);

        ArrayList<Patient> obj = new ArrayList<Patient>();
        obj.add(patient);
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PatientRecordUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private JButton jButton1;
    private JButton jButton2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel7;
    private JLabel jLabel8;
    private JPanel jPanel1;
    private JTextField jTextField1;
    private JTextField jTextField10;
    private JTextField jTextField11;
    private JTextField jTextField2;
    private JTextField jTextField3;
    private JTextField jTextField4;
    private JTextField jTextField5;
    private JTextField jTextField6;
    private JTextField jTextField7;
    private JTextField jTextField8;
    private JTextField jTextField9;
    // End of variables declaration
}

这里的患者班:

import java.util.Calendar;

public class Patient {
    private String firstName;
    private String secondName;
    private String lastName;
    private int fileNumber;
    private Date birthDate;
    private Date admissionDate;

    // constructor to initialize name, birth date and admission date

    public Patient(String first, String second, String last, int fileNo, Date dateOfBirth, Date dateOfAdmission) {
        firstName = first;
        secondName = second;
        lastName = last;
        fileNumber = fileNo;
        birthDate = dateOfBirth;
        admissionDate = dateOfAdmission;
    } // end Patient constructor

    public int getAage() {
        return Calendar.getInstance().get(Calendar.YEAR) - birthDate.getYear();

    }

    // convert Patient to String format
    public String toString() {
        return String.format("%s, %s %s   File No.:%d\n Admission: %s  age: %d\n\n", lastName, firstName, secondName,
                fileNumber, admissionDate, getAage());
    } // end method toString
} // end class Patient

这里是日期类

    public class Date
    {
    private int month; // 1-12
    private int day;   // 1-31 based on month
    public  int year;  // any year

  // constructor: call checkMonth to confirm proper value for month; 
   // call checkDay to confirm proper value for day

   }
   public Date( int theMonth, int theDay, int theYear )
  {
  month = checkMonth( theMonth ); // validate month
  setYear(theYear); // could validate year
  day = checkDay( theDay ); // validate day

    } // end Date constructor

  // utility method to confirm proper month value
   private int checkMonth( int testMonth )
   {
     if ( testMonth > 0 && testMonth <= 12 ) // validate month
       return testMonth;
      else // month is invalid 
      { 
     System.out.printf( 
        "Invalid month (%d) set to 1.", testMonth );
     return 1; // maintain object in consistent state
      } // end else
    } // end method checkMonth

    // utility method to confirm proper day value based on month and year
     private int checkDay( int testDay )
    {
    int daysPerMonth[] = 
     { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    // check if day in range for month
    if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
     return testDay;

    // check for leap year
    if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
       ( year % 4 == 0 && year % 100 != 0 ) ) )
     return testDay;

    System.out.printf( "Invalid day (%d) set to 1.", testDay );
    return 1;  // maintain object in consistent state
   } // end method checkDay

 private void setYear(int testYear)
 {
 year = testYear < 0 ? 0 : testYear;
 }

 public int getYear()
 {
    return year;
 }
 // return a String of the form month/day/year
  public String toString()
  { 
  return String.format( "%d/%d/%d", month, day, year ); 
  } // end method toString

  } // end class Date

2 个答案:

答案 0 :(得分:0)

当您在方法中声明变量时,它将是该方法的本地变量。

在这种情况下,只要您点击Add Patient按钮,它就会创建一个新的arraylist。所以它会重置早期的值。

不是在方法级别声明obj,而是应该在类级别声明它,以便它不会被重置,所有其他方法都可以访问它(你想要的)

答案 1 :(得分:0)

因此,首先,您的PatientRecordUI方法正在创建一个新的患者阵列,然后在每次呼叫时为其添加新患者。这根本不会有用。您需要创建一个且只有一个阵列,并使用它来存储您的患者。

转到public class PatientRecordUI extends javax.swing.JFrame { private ArrayList<Patient> patientsArray = new ArrayList<Patient>(); 课程并添加如下新字段:

jButton1ActionPerformed()

现在,在您的 ArrayList<Patient> obj = new ArrayList<Patient>(); obj.add(patient); 方法中,删除此部分:

    patientsArray.add(patient);

并将其替换为

jButton2ActionPerformed()

现在你的程序应该正确存储患者。

您要做的下一件事是创建一种检索患者数据的方法。我想这就是你的fileNumber方法应该发生的事情。

要检索患者数据,首先需要一种基于 int numberRequested = Integer.parseInt(jTextField11.getText()); 属性定位的方法。我的猜测是用户使用第11个文本字段输入文件编号,因此编号为

numberRequested

由于您将患者存储在数组中,您应该迭代每位患者并寻找文件编号等于Patient的患者。一旦找到患者,您只需获取他们的信息并在界面中显示。

检索患者数据的一种方法是在Starting hostapd. Configuration file: /etc/hostapd.conf ioctl[SIOCS80211, op=21, val=0, arg_len=42]: Invalid argument Could not connect to kernel driver. ioctl[SIOCS80211, op=21, val=0, arg_len=42]: Invalid argument ioctl[SIOCS80211, op=17, val=0, arg_len=0]: Invalid argument ioctl[SIOCS80211, op=20, val=0, arg_len=7]: Invalid argument ioctl[SIOCS80211, op=20, val=0, arg_len=7]: Invalid argument ioctl[SIOCS80211, op=20, val=0, arg_len=7]: Invalid argument ioctl[SIOCS80211, op=20, val=0, arg_len=7]: Invalid argument ioctl[SIOCS80211, op=1, arg_len=32]: Invalid argument Could not read SSID from system em0: Unable to setup interface. ioctl[SIOCS80211, op=21, val=0, arg_len=42]: Invalid argument Could not connect to kernel driver. ioctl[SIOCS80211, op=21, val=0, arg_len=42]: Invalid argument ioctl[SIOCS80211, op=7, val=4, arg_len=0]: Invalid argument /etc/rc.d/hostapd: WARNING: failed to start hostapd 类中添加一些getter。否则,您可以将字段设为公开。由你决定。

我不会详细介绍如何执行这些最新步骤 - 这应该足以让您走上正轨。