首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么将list转成数组

2012-02-13 
如何将list转成数组?我要访问一个类中的数据:Java codepackage Userimport java.util.Listimport Commun

如何将list转成数组?
我要访问一个类中的数据:

Java code
package User;import java.util.List;import Community.CommunityEntity;public class TestDemo{    public static void main(String[] args)     {        System.out.println("这是测试CommunityEntity类中的数据:");        DbStore abcd = new DbStore();        List<CommunityEntity> list = abcd.getCommList();        CommunityEntity ce = new CommunityEntity();             String commList[] = list.toArray(new String[]{});                for(int i=0;i<abcd.getCommList().size();i++)        {            System.out.println("小区编号:");            System.out.println("小区名称:");            System.out.println("小区经理:");            System.out.println("区域:");            System.out.println("状态:");            System.out.println("渗透率:");            System.out.println("覆盖用户数:");            System.out.println("\r\n");        }    }}


剩下的代码:
Java code
package User;import java.util.ArrayList;import java.util.List;import Community.CommunityEntity;public class DbStore {    private List<CommunityEntity> commList = new ArrayList<CommunityEntity>();    public DbStore() //初始化用户实体数组    {        CommunityEntity com1 = new CommunityEntity();        com1.setCommId("001");        com1.setCommName("新升新苑");        com1.setCommManager("大漠");        com1.setCommArea("高新区");        com1.setCommStatue("可放装");        com1.setPermeability(61);        com1.setUserCount(221);                CommunityEntity com2 = new CommunityEntity();        com2.setCommId("002");        com2.setCommName("新盛花园");        com2.setCommManager("大漠");        com2.setCommArea("吴中区");        com2.setCommStatue("可放装");        com2.setPermeability(62);        com2.setUserCount(222);                CommunityEntity com3 = new CommunityEntity();        com3.setCommId("003");        com3.setCommName("新唯家园");        com3.setCommManager("大漠");        com3.setCommArea("工业园区");        com3.setCommStatue("可放装");        com3.setPermeability(63);        com3.setUserCount(223);                CommunityEntity com4 = new CommunityEntity();        com4.setCommId("004");        com4.setCommName("蠡东花园");        com4.setCommManager("大漠");        com4.setCommArea("相城区");        com4.setCommStatue("可放装");        com4.setPermeability(64);        com4.setUserCount(224);                CommunityEntity com5 = new CommunityEntity();        com5.setCommId("005");        com5.setCommName("朱家庄新村");        com5.setCommManager("大漠");        com5.setCommArea("金阊区");        com5.setCommStatue("可放装");        com5.setPermeability(65);        com5.setUserCount(225);                commList.add(com1);        commList.add(com2);        commList.add(com3);        commList.add(com4);        commList.add(com5);    }    public void setCommList(List<CommunityEntity> commList)     {        this.commList = commList;    }    public List<CommunityEntity> getCommList()    {        return commList;    }}

Java code
package Community;public class CommunityEntity {    private String commId;    private String commName;    private String commManager;    private String commArea;    private String commStatue;    private double permeability;    private int userCount;    public String getCommId()     {        return commId;    }    public void setCommId(String commId)    {        this.commId = commId;    }    public String getCommName()     {        return commName;    }    public void setCommName(String commName)     {        this.commName = commName;    }    public String getCommManager()     {        return commManager;    }    public void setCommManager(String commManager)    {        this.commManager = commManager;    }    public String getCommArea()    {        return commArea;    }    public void setCommArea(String commArea)    {        this.commArea = commArea;    }    public String getCommStatue()     {        return commStatue;    }    public void setCommStatue(String commStatue)     {        this.commStatue = commStatue;    }    public double getPermeability()     {        return permeability;    }    public void setPermeability(double permeability)    {        this.permeability = permeability;    }    public int getUserCount()     {        return userCount;    }    public void setUserCount(int userCount)     {        this.userCount = userCount;    }} 



[解决办法]
for(int i=0;i<abcd.getCommList().size();i++)
{
CommunityEntity entity=abcd.getCommList().get(i);
System.out.println("小区编号:"+entity.get....());
System.out.println("小区名称:");
System.out.println("小区经理:");
System.out.println("区域:");
System.out.println("状态:");
System.out.println("渗透率:");
System.out.println("覆盖用户数:");
System.out.println("\r\n");
}

[解决办法]
如何将list转成数组?
我看LZ不是转了吗?
难道出了什么问题,是的话把问题贴出来
Java code
Object[] arr = new LinkedList<Object>().toArray();
[解决办法]
你代码里面有个packe名称是大写的 Community.CommunityEntity; 不符合命名规范 ,刚刚已经给你写了一个
CommunityEntity 类,里面重写了toString方法,用那个类,否则下面这个代码输出ce是十六进制内存地址
Java code
import java.util.List;public class TestDemo{    public static void main(String[] args)    {        System.out.println("这是测试CommunityEntity类中的数据:");        DbStore abcd = new DbStore();        List<CommunityEntity> list = abcd.getCommList();//        CommunityEntity ce = new CommunityEntity();//             String commList[] = list.toArray(new String[]{});        CommunityEntity[] ceAry=new CommunityEntity[list.size()];        list.toArray(ceAry);        for(CommunityEntity ce:ceAry){            System.out.println("info:"+ce);        }    }}
[解决办法]
List<CommunityEntity> list = abcd.getCommList();
CommunityEntity ce = new CommunityEntity();
String commList[] = list.toArray(new String[]{}); //list保存的是CommunityEntity类型,怎么能转成String类型的数组呢?
你的测试代码根本就不需要把list转换成数组
Java code
        DbStore abcd = new DbStore();        List<CommunityEntity> list = abcd.getCommList();        CommunityEntity ce = new CommunityEntity();        //     String commList[] = list.toArray(new String[]{});                for(int i=0;i<list.size();i++)        {            System.out.println("小区编号:" + list.get(i).getCommId());            System.out.println("小区名称:" + list.get(i).getCommName());            System.out.println("小区经理:" + list.get(i).getCommManager());            System.out.println("区域:" + list.get(i).getCommArea());            System.out.println("状态:" + list.get(i).getCommStatue());            System.out.println("渗透率:" + list.get(i).getPermeability(););            System.out.println("覆盖用户数:" + list.get(i).getUserCount());            System.out.println("\r\n");        } 

热点排行