有以下一段代码,看得不是很懂,望高人指点一下其中List<String>和List<List<String>>的具体运作过程:
是什么意思啊?为什么要用<>呢?
public DeviceInfo(List<String> tmp, List<List<String>> chanList)
{
this.deviceID = tmp.get(0);
this.stationInfo = InfoManager.getStationInfo(tmp.get(1));
this.phyRef = InfoManager.getPhysicalInfo(tmp.get(2));
FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID);
this.epdPath = FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID + File.separator + "epd");
this.ftpPath = FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID + File.separator + "ftp");
this.orgPath = FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID + File.separator + "org");
this.tmpPath = FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID + File.separator + "tmp");
this.channels = new ArrayList<ChannelInfo>();
for (List<String> tmpList : chanList)
{
ChannelInfo chanInfo = new ChannelInfo(this, tmpList);
this.channels.add(chanInfo);
}
}
是什么意思啊?为什么要用<>呢?
public DeviceInfo(List<String> tmp, List<List<String>> chanList)
{
this.deviceID = tmp.get(0);
this.stationInfo = InfoManager.getStationInfo(tmp.get(1));
this.phyRef = InfoManager.getPhysicalInfo(tmp.get(2));
FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID);
this.epdPath = FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID + File.separator + "epd");
this.ftpPath = FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID + File.separator + "ftp");
this.orgPath = FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID + File.separator + "org");
this.tmpPath = FileTransform.newDir(this.stationInfo.orgPath + File.separator + this.deviceID + File.separator + "tmp");
this.channels = new ArrayList<ChannelInfo>();
for (List<String> tmpList : chanList)
{
ChannelInfo chanInfo = new ChannelInfo(this, tmpList);
this.channels.add(chanInfo);
}
}
楼主【zelatanibra】截止到2008-07-06 10:54:44的历史汇总数据(不包括此帖):
发帖的总数量:24 发帖的总分数:415
结贴的总数量:18 结贴的总分数:315
无满意结贴数:5 无满意结贴分:140
未结的帖子数:6 未结的总分数:100
结贴的百分比:75.00 % 结分的百分比:75.90 %
无满意结贴率:27.78 % 无满意结分率:44.44 %
楼主加油
tmp里面的元素都是stringList <List <String>> chanList
chanList里面的元素都是list,而这个list里面的元素都是string泛型没怎么用过,觉得这个还是很恶心的
每个List的元素均是String呢?
网上大多数资料都是针对C#的,针对Java的比较少。谢谢
看看 JDK API 文档,说明很全。不是资料少,而是你没找对地方。
List<String>中List中装的是String
List<List<String>>中装的是List。
泛型好比是个收纳箱,不但可以放对象,还要在上面标明箱子里面放什么东西
<>使用了泛型,就是告诉JVM该LIST里面装的是什么类型的东西.
List <String>中List中装的是String
List <List <String>>中装的是List,里面的LIST装的是String.
四楼说的蛮形象的 差不多就是这样他是为了解决 数组定义了之后 大小不能改变的问用这个泛形的ADD方法 就算容器初始化的大小已装满了 如果再装的话 泛形他可以自动增加容量如此之外差不多的泛形还有 LinkList<>等等意思知道了是一回事 还得多用才能体会
先看看不用泛型的时候:
List list=new ArrayList();
//list.add();
//填充list
for(int i=0;i<list.size();i++){
User user=(User)list.get(i);
}
每次取出来的时候都要把Object强转成User类型,据Sun的技术人员说,这样会损失不少性能
用了泛型之后:
List<User> list=new ArrayList<User>();
//list.add();
//填充list
for(int i=0;i<list.size();i++){
User user = list.get(i);
}
代码变得健壮了不说,而且性能的提高也是非常可观的。