ArrayList的问题!!急
** TestStaffs.java
* /
import java.util.ArrayList;
public class TestStaffs {
public static void main(String[] args) {
Build_Display_Persons bs = new Build_Display_Persons();
// set up staffs
ArrayList staffs = bs.build_persons();
bs.display_person(staffs);
// display staffs
new Displaypersons (staffs);
}
}
/*
* Person.java
* /
public class Person {
private String firstname;
private String lastname;
private int age;
public Person()
{ }
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/*
* Build_Display_Persons.java
* /
import java.util.ArrayList;
public class Build_Display_Persons {
String [] fnames = {"Smon", "Weiming", "Yunzhe"};
String [] lnames = {"Sheng", "He", "He"};
int [] ages = {20, 59, 24};
public ArrayList build_persons ()
{
ArrayList staffs = new ArrayList();
int n = fnames.length;
for (int i=0; i < n; i++)
{
Person p = new Person ();
p.setFirstname(fnames[i]);
p.setLastname(lnames[i]);
p.setAge(ages[i]);
staffs.add(p);
}
return staffs;
}
public void display_person (ArrayList alist)
{
for (int i=0; i < alist.size(); i++){
Person p = (Person) alist.get(i);
System.out.print(p.getFirstname()+"\t");
System.out.print(p.getLastname()+"\t");
System.out.println(p.getAge());
}
}
}
如何把?
a. ask user input firstname
b. ask user input lastname
c. ask user input age 加入TestStaffs.java
我如果我想建立个Search.java
只要有搜寻(firstname lastname age)功能 用arraylist方法!!
我在Core Java书上找不到怎么解决这2个问题方法!请高手帮我
搜索更多相关的解决方案:
ArrayList
----------------解决方案--------------------------------------------------------