访问 ArrayList 中的特定元素
Accessing specific elements in an ArrayList
我正在创建一个程序来将客人添加到聚会中。来宾的最大数量设置为 3。我的程序正确输出了 3 位来宾。
但是,如何访问并打印出没有出现在列表中的元素?
如您在我的 Party2Driver 中所见,我调用了 "addGuest" 方法 5 次。由于我将 maxGuests 设置为 3,程序正确地将前 3 个名字添加到列表中。但是如何输出未添加到列表中的最后两个名字?
我希望它显示类似 "blank and blank" 的内容,因为来宾列表已满,所以未添加到列表中。
每次我尝试使用 "get method" 访问那些 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
这是派对 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
创建第二个名为 rejectedGuests
的 ArrayList,每当客人被拒绝时,将他们添加到该列表中。然后放一个 getter 方法来检索列表。从那里它只是一个简单的 String.format() 调用和一个 println() 语句。
添加 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
我正在创建一个程序来将客人添加到聚会中。来宾的最大数量设置为 3。我的程序正确输出了 3 位来宾。
但是,如何访问并打印出没有出现在列表中的元素?
如您在我的 Party2Driver 中所见,我调用了 "addGuest" 方法 5 次。由于我将 maxGuests 设置为 3,程序正确地将前 3 个名字添加到列表中。但是如何输出未添加到列表中的最后两个名字?
我希望它显示类似 "blank and blank" 的内容,因为来宾列表已满,所以未添加到列表中。
每次我尝试使用 "get method" 访问那些 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
这是派对 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
创建第二个名为 rejectedGuests
的 ArrayList,每当客人被拒绝时,将他们添加到该列表中。然后放一个 getter 方法来检索列表。从那里它只是一个简单的 String.format() 调用和一个 println() 语句。
添加 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