最后应该是要用System.out.println(ArrayList)这样输出一个结果吧..编写一个程序,使用arraylist存储客户的邮件地址.地址中应该包含姓名\街道\市\省\国家和代码,然后显示array中的内容
[地址
姓名
街道
等等
,
地址
姓名
街道
等等
,
地址
姓名
街道
等等
]求一个比这样更好的方法.谢谢大家的帮助啊class ArrayTest {
ArrayList list; ArrayTest() {
list = new ArrayList();
} void display() {
list.add("\nName : " + "David Clarke" + "\nStreer : "
+ "10 Downing Street" + "\nCity : " + "LondonState" + " : "
+ "London" + "\nCountry : " + "United Kingdom" + "\nPincode : "
+ 110022+"\n");
list.add("\nName : " + "John" + "\nStreer : "
+ "12 Park Avennue" + "\nCity : " + "CaliforniaState" + " : "
+ "California" + "\nCountry : " + "USA" + "\nPincode : "
+ 210033+"\n");
System.out.println(list);
}
}public class MailAddressTest {
public static void main(String[] args) {
ArrayTest addressTest = new ArrayTest();
addressTest.display();
}
}比如这样.但这样做好像有错误~~哎import java.util.*;class Address {
String name; String address; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}public class a {
public static void main(String[] args) {
Address test = new Address();
Collection cK = new ArrayList();
test.setName("name");
test.setAddress("address");
cK.add(test);
test.setName("name2");
test.setAddress("address2");
cK.add(test);
Iterator i = cK.iterator();
while(i.hasNext())
{
System.out.println(i.next().toString());
} }}
[地址
姓名
街道
等等
,
地址
姓名
街道
等等
,
地址
姓名
街道
等等
]求一个比这样更好的方法.谢谢大家的帮助啊class ArrayTest {
ArrayList list; ArrayTest() {
list = new ArrayList();
} void display() {
list.add("\nName : " + "David Clarke" + "\nStreer : "
+ "10 Downing Street" + "\nCity : " + "LondonState" + " : "
+ "London" + "\nCountry : " + "United Kingdom" + "\nPincode : "
+ 110022+"\n");
list.add("\nName : " + "John" + "\nStreer : "
+ "12 Park Avennue" + "\nCity : " + "CaliforniaState" + " : "
+ "California" + "\nCountry : " + "USA" + "\nPincode : "
+ 210033+"\n");
System.out.println(list);
}
}public class MailAddressTest {
public static void main(String[] args) {
ArrayTest addressTest = new ArrayTest();
addressTest.display();
}
}比如这样.但这样做好像有错误~~哎import java.util.*;class Address {
String name; String address; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}public class a {
public static void main(String[] args) {
Address test = new Address();
Collection cK = new ArrayList();
test.setName("name");
test.setAddress("address");
cK.add(test);
test.setName("name2");
test.setAddress("address2");
cK.add(test);
Iterator i = cK.iterator();
while(i.hasNext())
{
System.out.println(i.next().toString());
} }}
list = new ArrayList();
} void display() {
System.out.println(list);
} void setPerson(String name, String street, String city, String state,
String country, int code) {
this.name = name;
this.street = street;
this.city = city;
this.state = state;
this.country = country;
this.code = code;
list.add("\nName : " + name + "\nStreer : " + street + "\nCity : "
+ state + " : " + city + "\nCountry : " + country
+ "\nPincode : " + code + "\n");
}
}public class MailAddressTest {
public static void main(String[] args) {
ArrayTest addressTest = new ArrayTest();
addressTest.setPerson("David Clarke", "10 Downing Street",
"LondonState", "London", "United Kingdom", 110022);
addressTest.setPerson("John Lenon", "12 Park Avenue",
"CaliforniaState", "California", "USA", 210033);
addressTest.setPerson("Stefil Graff", "14 Maple Lane",
"MahomaState", "Sydney", "Australia", 412033);
addressTest.display();
}
}
list = new ArrayList();
}// void display() {
// System.out.println(list);
// } void setPerson(String name, String street, String city, String state,
String country, int code) {
// this.name = name;
// this.street = street;
// this.city = city;
// this.state = state;
// this.country = country;
// this.code = code;
list.add("\nName : " + name + "\nStreer : " + street + "\nCity : "
+ state + " : " + city + "\nCountry : " + country
+ "\nPincode : " + code + "\n");
}
}public class MailAddressTest {
public static void main(String[] args) {
ArrayTest addressTest = new ArrayTest();
addressTest.setPerson("David Clarke", "10 Downing Street",
"LondonState", "London", "United Kingdom", 110022);
addressTest.setPerson("John Lenon", "12 Park Avenue",
"CaliforniaState", "California", "USA", 210033);
addressTest.setPerson("Stefil Graff", "14 Maple Lane",
"MahomaState", "Sydney", "Australia", 412033);
// addressTest.display();
System.out.println(addressTest.list);
}
}
在class Personal的个人资料类里,要包含name, street, city, state, country等属性,并添加getXXX(),setXXX(),方法。在你的class ArrayTest类里setPerson方法里写下面代码:
Personal per = new Personal();
per.setXXX();
.
.
.
然后list.add(per);
把对象存放到Arraylist里