我想把一个类作为元素放进ArrayList,不知道该怎么做,请高手们指教,谢谢!
类是
class user{
String name;
String street;
String city;
String country;
String pincode;
user(String name,String street,String city,String country,String pincode){
this.name = name;
this.street = street;
this.city = city;
this.country = country;
this.pincode = pincode;
}
}
下面的addobj这个我不知道该怎么写,报错了
class al{
ArrayList<Object> a;
al(){
a = new ArrayList<Object>();
}
void addobj(Object (new user())){ a.add(s);
}
}
类是
class user{
String name;
String street;
String city;
String country;
String pincode;
user(String name,String street,String city,String country,String pincode){
this.name = name;
this.street = street;
this.city = city;
this.country = country;
this.pincode = pincode;
}
}
下面的addobj这个我不知道该怎么写,报错了
class al{
ArrayList<Object> a;
al(){
a = new ArrayList<Object>();
}
void addobj(Object (new user())){ a.add(s);
}
}
a.add(obj);
}调用时:
al al=new al();//类名最好大写
User user=new User(........);
al.a.add(user);取出时:
User user=(User)al.a.get(...);
list.add(user);第一次回复别人的问题。。
User user = new User(XX,XX,XX,XX,XX);//属性赋值
list.add(user);
list.add(user);
取的时候
User user=(User)list.get(*);
查看API自学
如果集合是User类型而不是Object类型的话 取值时便不需转换了
* 编写一个程序,使用ArrayList存储客户的邮件地址。地址中应包含姓名、
* 街道、市(县)、省(自治区)、国家(地区)和代码,然后显示ArrayList中的内容.
*/
import java.util.*;class user{
String name;
String street;
String city;
String country;
int pincode;
user(String name,String street,String city,String country,int pincode){
this.name = name;
this.street = street;
this.city = city;
this.country = country;
this.pincode = pincode;
}
}class al{
ArrayList<user> a;
al(){
a = new ArrayList<user>();
}
void addobj(user s){
a.add(s);
}
void disp(){
System.out.println("客户信息如下");
for(int i = 0;i<a.size();i++){
System.out.println("name:"+a.get(i).name);
System.out.println("street:"+a.get(i).street);
System.out.println("city:"+a.get(i).city);
System.out.println("country:"+a.get(i).country);
System.out.println("pincode:"+a.get(i).pincode);
System.out.println(",");
}
}
}public class arraylistTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
user user1 = new user("David Clarke","10 Downing Street","LondonState","United Kingdom",110022) ;
user user2 = new user("John Lenon","12 Park Avenu","California","USA",210033);
user user3 = new user("Stefii Graff","14 Maple","Sydney","Asutralia",412033);
al k = new al();
k.addobj(user1);
k.addobj(user2);
k.addobj(user3);
k.disp();
}
}
多谢各位的指点,自学java,查API查过了这里主要是不知道怎么弄,经过你们一指点,一下就好了,这个程序算是弄通了,学到一个知识点。
/**
*
*/
public class userTest implements Serializable
{
String name;
String street;
String city;
String country;
String pincode;
/**
* @return the name
*/
public String getName ()
{
return name;
}
/**
* @param name the name to set.
*/
public void setName (String name)
{
this.name = name;
}
/**
* @return the street
*/
public String getStreet ()
{
return street;
}
/**
* @param street the street to set.
*/
public void setStreet (String street)
{
this.street = street;
}
/**
* @return the city
*/
public String getCity ()
{
return city;
}
/**
* @param city the city to set.
*/
public void setCity (String city)
{
this.city = city;
}
/**
* @return the country
*/
public String getCountry ()
{
return country;
}
/**
* @param country the country to set.
*/
public void setCountry (String country)
{
this.country = country;
}
/**
* @return the pincode
*/
public String getPincode ()
{
return pincode;
}
/**
* @param pincode the pincode to set.
*/
public void setPincode (String pincode)
{
this.pincode = pincode;
}
}List <userTest> user = new ArrayList <userTest> ();
for (int i = 0; i <= 5; i++)
{
userTest e = new userTest ();
e.setName ("第" + i + "医院");
e.setStreet ("公主坟");
e.setCity ("海淀区");
e.setCountry ("北京市");
e.setPincode ("100000");
user.add (e);
}