【问题标题】:Accessing specific elements in an ArrayList访问 ArrayList 中的特定元素
【发布时间】:2015-06-07 18:51:12
【问题描述】:

我正在创建一个程序来将客人添加到聚会中。客人的最大数量设置为 3。我的程序正确输出了 3 位客人。

但是,我如何访问并打印出未列入列表的元素?

正如您在我的 Party2Driver 中看到的,我调用了我的“addGuest”方法 5 次。自从我将 maxGuests 设置为 3 后,程序正确地将前 3 个名称添加到列表中。但是如何输出未添加到列表中的最后两个名称?

我希望它显示“空白和空白”之类的内容未添加到列表中,因为来宾列表已满。

每次我尝试使用“get 方法”访问这些 ArrayList 元素时,都会引发 OutOutBoundException。

感谢任何帮助!

import java.util.ArrayList;

public class Party2Driver
{
 public static void main(String[] args)
    {

      Party2 party = new Party2(3, "David Beckham");
      party.addGuest("Roberto Baggio");
      party.addGuest("Zinedine Zidane");
      party.addGuest("Roberto Baggio");
      party.addGuest("Johan Cruyff");
      party.addGuest("Diego Maradona");
      party.printParty();

    } // end main

}//end Party2Driver

这是我的方法的Party 2 Class:

import java.util.ArrayList;

public class Party2
{
    // instance variables that will hold your data
    // private indicates they belong to this class exclusively
    private int maxGuests;
    private String host;
    private ArrayList<String> guests;

    //Constructor
    public Party2(int maxGuests, String host)
    {
        System.out.println("Maximum number of guests: " + maxGuests + ".  Guest list for " + host + "'s party. \n");
        this.guests = new ArrayList<String>();
        this.maxGuests = maxGuests;
        this.host = host;
    }

    //getter
    // define type of data to be returned
    public String getHost()
    {
        System.out.println("Setting host to: " + host);
        return host;
    }

    //setter
    // setters have type void b/c they return nothing
    public void setHost(String host)
    {
        System.out.println("Setting host to: " + host);
        this.host = host;
    }

    //*************************************
    //Method to add to guest list
    public void addGuest(String guest)
    {

        if (guests.size() < maxGuests)
        {
            System.out.println("Guest: " + guest + "\n");
            this.guests.add(guest);
        }

        else
        {
            System.out.println("     Guest cannot be added. Guest list is full. \n");
        }//end if

    }//end method

    //*************************************
    //Method to print party
    public void printParty()
    {
        System.out.println("****************************************************\n");
        System.out.println("Guest list for " +
        this.host + "'s party is: \n\n" +
        this.guests + ".\n");

    } // end Print Party method

    }//end class party

【问题讨论】:

  • 定义“get方法”.

标签: java class methods arraylist indexoutofboundsexception


【解决方案1】:

创建第二个 ArrayList,名称类似于 rejectedGuests,当客人被拒绝时,将其添加到该列表中。然后放一个getter方法来检索列表。从那里开始,它只是一个简单的 String.format() 调用和一个 println() 语句。

【讨论】:

    【解决方案2】:

    添加 extraGuest 字段:

    private ArrayList<String> extraGuest; 
    

    在 addGuest(guest) 方法的 else 块中添加 extraGuest。

    import java.util.ArrayList;
    
    public class Party2
    {
     // instance variables that will hold your data
     // private indicates they belong to this class exclusively
     private int maxGuests;
     private String host;
     private ArrayList<String> guests;
     private ArrayList<String> extraGuest;
    
     //Constructor
     public Party2(int maxGuests, String host)
     {
        System.out.println("Maximum number of guests: " + maxGuests + ".  Guest list for " + host + "'s party. \n");
        this.guests = new ArrayList<String>();
        this.extraGuest = new ArrayList<String>();
        this.maxGuests = maxGuests;
        this.host = host;
     }
    
     //getter
     // define type of data to be returned
     public String getHost()
     {
        System.out.println("Setting host to: " + host);
        return host;
     }
    
     //setter
     // setters have type void b/c they return nothing
     public void setHost(String host)
     {
        System.out.println("Setting host to: " + host);
        this.host = host;
     }
    
     //*************************************
     //Method to add to guest list
     public void addGuest(String guest)
     {
    
        if (guests.size() < maxGuests)
        {
            System.out.println("Guest: " + guest + "\n");
            this.guests.add(guest);
        }
    
        else
        {
            extraGuest.add(guest);
            System.out.println("     Guest cannot be added. Guest list is full. \n");
        }//end if
    
     }//end method
    
     //*************************************
     //Method to print party
     public void printParty()
     {
          System.out.println("****************************************************\n");
        System.out.println("Guest list for " +
        this.host + "'s party is: \n\n" +
        this.guests + ".\n");
    
        System.out.println(extraGuest+"weren't added to the list because the guest list is full");
    
     } // end Print Party method
    
    }//end class party
    

    【讨论】:

    • 这行得通。现在我这样做了,这让我意识到我并没有以错误的方式考虑 ArrayList。我试图让“客人” ArrayList 做得比它应该做的更多。 “extraGuest”绝对应该有一个单独的列表。当您是初学者并仍在学习时,事后诸葛亮总是有意义的!
    猜你喜欢
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2014-03-10
    相关资源
    最近更新 更多