如何从数组列表中获取值?

时间:2013-05-02 21:33:23

标签: java arrays list

该计划的简报:该计划记录了拥有航空公司会员卡的人以及他们每周收集的积分数。 (第1,2,3,4周)信息存储在数组中,然后在需要时可以通过按“listButton”输出。

我知道如何从数组中获取值并简单地输出它,但不确定如何使用循环执行此操作。查看“totalPointsButton”下的问题区域

public class AirlineCardsView extends FrameView {

    class airline {
        String lastName, firstName;
        int week1, week2, week3, week4;

        airline (int _week1, int _week2, int _week3, int _week4, String _lastName, String _firstName) {
            week1 = _week1;
            week2 = _week2;
            week3 = _week3;
            week4 = _week4;
            lastName = _lastName;
            firstName = _firstName;
        }
    }

    /** Define the ArrayList */
    ArrayList <airline> members = new ArrayList <airline>();

    public AirlineCardsView(SingleFrameApplication app) {
        //GUI stuff
    }// </editor-fold>

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

        String lastName, firstName;
        int week1, week2, week3, week4;

        week1 = Integer.parseInt(weekOneField.getText());
        week2 = Integer.parseInt(weekTwoField.getText());
        week3 = Integer.parseInt(weekThreeField.getText());
        week4 = Integer.parseInt(weekFourField.getText());
        lastName = lastNameField.getText();
        firstName = firstNameField.getText();

        airline c = new airline(week1, week2, week3, week4, firstName, lastName);
        members.add(c);
    }                                         

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

        String temp = "";

        for (int x=0; x<=members.size()-1; x++) {
            temp = temp + members.get(x).firstName + " "
                    + members.get(x).lastName + ": "
                    + members.get(x).week1 + " "
                    + members.get(x).week2 + " "
                    + members.get(x).week3 + " "
                    + members.get(x).week4 + "\n";
        }
        memberListTArea.setText(temp);

    }             

这里我不确定如何使用相应成员的数组中存储的相同值初始化值week1,week2,week3,week4(对于int totalPoints)。

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

        int week1, week2, week3, week4; 
        String lastName, firstName;

       String points = "";

        for (int j = 0; j < members.size()-1; j++) {
 //this line checks the inputted name to see if it matches any stored in array.
                if (members.get(j).lastName.equals(lastNameField.getText())) {
                    int totalPoints = week1 + week2 + week3 + week4; //then adds total points
                    }
                }

        }

2 个答案:

答案 0 :(得分:1)

我希望我能正确理解这个问题......

  • 首先,这一行存在问题:

    for (int j = 0; j < members.size()-1; j++) {
    

您应该删除-1或使用<=。否则你将无法到达最后一个元素。

  • if (members.get(j).lastName.equals(lastNameField.getText()))可能会抛出NPE。 (members.get(j).lastName可以为null)此方法中定义的变量(第1-4周,以及姓氏,名字)也没有多大意义。

  • 你在if块中声明了int totalPoints,这意味着它不会在if块之外被看到。如果所有逻辑都在if块中,那就没什么大不了的了。

  • 你可以尝试:

    Airline al = null;
    int totalPoints;
    for (int j = 0; j < members.size(); j++) {
        al = members.get(j);
        if (al.lastName.equals(lastNameField.getText())) {
            totalPoints = al.week1 + al.week2 + al.week3 + al.week4;
        }
    }
    

更好:

    int totalPoints;
    for (Airline al:members) {
        if (al.lastName.equals(lastNameField.getText())) {
            totalPoints = al.week1 + al.week2 + al.week3 + al.week4;
        }
    }
  • 为了让代码看起来更好,您可以考虑在Airline课程中添加getter / setter。还添加了一个方法,int getTotalPoints()返回4周的总和。那么你不必在课外总结它。

答案 1 :(得分:0)

您应该将get和set方法添加到您的航空公司班级。因此,当您在arraylist中存储航空公司的对象时,您可以使用“members.get(j).week1,members.get(j).week2等”获取其值。

例如,像这样创建您的航空公司类。

public class airline {
    String lastName; 
    String firstName;
    int week1;
    int week2;
    int week3;
    int week4;

    public airline (int week1, int week2, int week3, int week4, String lastName, String firstName) {
        week1 = this.week1;
        week2 = this.week2;
        week3 = this.week3;
        week4 = this.week4;
        lastName = this.lastName;
        firstName = this.firstName;
    }

    public int getWeek1(){
        return week1;
    }

    public void setWeek1(int newWeek1){
        week1 = newWeek1;
    }

    public int getWeek2(){
        return week2;
    }

    public void setWeek2(int newWeek2){
        week2 = newWeek2;
    }

    public int getWeek3(){
        return week3;
    }

    public void setWeek3(int newWeek3){
        week3 = newWeek3;
    }

    public int getWeek4(){
        return week4;
    }

    public void setWeek4(int newWeek4){
        week4 = newWeek4;
    }

    public String getFirstName(){
        return firstName;
    }

    public void setFirstName(String newFirstName){
        firstName = newFirstName;
    }

    public String getLastName(){
        return lastName;
    }

    public void setLastName(String newLastName){
        lastName = newLastName;
    }

    public String toString(){
        return getWeek1() + " " + getWeek2() + " " + getWeek3() + " " + getWeek4() + getFirstName() + " " + getLastName();
    }
}