访问ArrayList中的特定元素

时间:2015-04-02 17:53:20

标签: java class methods arraylist indexoutofboundsexception

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

但是,如何访问和打印未列出的元素?

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

我希望它读取类似“空白和空白”的内容未添加到列表中,因为访客列表已满。

每当我尝试使用“get方法”访问这些ArrrayList元素时,它都会抛出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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

添加extraGuest字段:

private ArrayList<String> extraGuest; 

在addGuest(guest)方法的其他块中添加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