日期:2014-05-20  浏览次数:20812 次

新手疑问。关于封装的思想动态数组arraylist
Java code

import java.util.ArrayList;


public class StrDemo {
    
    public static void main(String[] args) {
        ArrayList ary=new ArrayList();
        Str st=new Str("Dacid Clarke","10 Doening Street","LondnState : London",
                "United Kingdom","110022");
        Str st1=new Str("John Lenon","12 Park Avenue","CliforniaState : CaliFornia",
                "USA","210033");
        Str st2=new Str("Stefii Graff","14 Mape Lane","LondonState : Sydney","Australia","412033");
        ary.add(st.getArry());
        

        for(int i=0;i<=5;i++){
            System.out.println(ary.get(i));    
        }
    }
}



Java code

import java.util.ArrayList;
public class Str {
    private ArrayList arry=new ArrayList();
    private String name;
    private String Street;
    private String City;
    private String Country;
    private String Picde;
    
    public Str(String str,String st2,String st3,String st4,String st5){
        name=str;
        Street=st2;
        City=st3;
        Country=st4;
        Picde=st5;
    }
    public void setArry(){
        arry.clear();
        arry.add(name);
        arry.add(Street);
        arry.add(City);
        arry.add(Country);
        arry.add(Picde);
    }
    public ArrayList getArry(){
        return arry;
    }
    public void print(){
        for(int i=0;i<5;i++){
        System.out.println(arry.get(i));    
        }    
    }
}


 这代码报错,好像是出在size和index上。。
请高手帮我改一下那里错了,用封装的话是怎么写。。我都搞不清我弄的是不是封装
原题目是
编写一个程序,使用ArrayList存储客户的邮件地址。地址中应该包含姓名,街道,市(县),省,国家,和代码,然后显示ArrayList中的内容,此程序的输出结果该显示如下:
客户的邮件地址

[Name: Dacid Clarke
Street :10 Doening Street
City : LondonState : London
Country : United Kingdom
Picde : 110022

Name: John Lenon 
Street :12 Park Avenue
City : CliforniaState : California
Country : USA
Picde : 210033

Name:Stefii Graff
Street : 14 Mape Lane
City : LondonState : Sydney
Country : Australia
Picde : 412033]


------解决方案--------------------
首先用户信息肯定封装成一个类,例如User ,然后ArrayList里面存放User

根据你的代码修改的

Java code

import java.util.ArrayList;
 class Str {
    private String name;
    private String Street;
    private String City;
    private String Country;
    private String Picde;
    
    public Str(String str,String st2,String st3,String st4,String st5){
        name=str;
        Street=st2;
        City=st3;
        Country=st4;
        Picde=st5;
    }
    
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append("name :").append(name).append("\n");
        sb.append("Street :").append(Street).append("\n");
        sb.append("City :").append(City).append("\n");
        sb.append("Country :").append(Country).append("\n");
        sb.append("Picde :").append(Picde).append("\n");
        
        return sb.toString();

    }
}


public class StrDemo {
    
    public static void main(String[] args) {
        ArrayList<Str> ary=new ArrayList<Str>();
        Str st=new Str("Dacid Clarke","10 Doening Street","LondnState : London",
                "United Kingdom","110022");
        Str st1=new Str("John Lenon","12 Park Avenue","CliforniaState : CaliFornia",
                "USA","210033");
        Str st2=new Str("Stefii Graff","14 Mape Lane","LondonState : Sydney","Australia","412033");
        
        
        ary.add(st);
        ary.add(st1);
        ary.add(st2);
        

        for(int i=0;i<ary.size();i++){
            System.out.println(ary.get(i));    
        }
    }
}

------解决方案--------------------
UserInfo.java
Java code
package com.hundsun.zhoujl;

/**
 * @author zhoujl 
 * 2011/12/27 14:37:27
 */
public class UserInfo {

    private String name;
    private String street;
    private String city;
    private String country;
    private String picde;
    
    
    /**
     * @param name
     * @param street
     * @param city
     * @param country
     * @param picde
     */
    public UserInfo(String name, String street, String city, String country,
            String picde) {
        super();
        this.name = name;
        this.street = street;
        this.city = city;
        this.country = country;
        this.picde = picde;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getPicde() {
        return picde;
    }
    public void setPicde(String picde) {
        this.picde = picde;
    }

    @Override
    public String toString() {
        return "Name:"+name+"\n"+"Street:" + street+"\n"+"City:"+city+"\n"
        +"Country:"+country+"\n"+"Picde:"+picde;
    }
    
    

}

------解决方案--------------------
探讨

7楼 封装在一个类就是用的toString方法吗 然后ArrayList<Str> ary=new ArrayList<Str>();

这个语句 <>这个我还是第一次看到 能不能解释下。。。