近来要对一个有序的list进行分组,由于我是初学者,以前没有编程经验,所以出了许多bug。下面分享一下。
List里面放着author,author有属性id、name和writeCount三个属性,分别表示作者id、昵称和编辑文章数量。对于按照id排序的list,统计每个作者编辑了多少文章。
public class author
{
private int id;
private String name;
private int writeCount;
//set get 方法
}
List的初始值是:original list
author id=1 author name=1ligang
author id=2 author name=2ligang
author id=2 author name=2ligang
author id=3 author name=3ligang
author id=3 author name=3ligang
author id=3 author name=3ligang
author id=4 author name=4ligang
author id=4 author name=4ligang
author id=4 author name=4ligang
author id=4 author name=4ligang
开始我想都没有想打算用2个for循环遍历,可是后面就出现了bug。哈哈,死循环.....代码如下:
public void groupList(List<author> list)
{
if(list==null || list.size()==0)
{
return;
}
List<author> resultList=new ArrayList<author>();
int tempWriteCount=1;
author tempAuthor=new author();
for(int i=0;i<list.size();i++)
{
//tempAuthor=list.get(i);
for(int j=1;j<list.size();j++)
{
if(list.get(i).getId()==list.get(j).getId())
{
tempWriteCount+=1;
}else {
tempAuthor.setWriteCount(tempWriteCount);
resultList.add(tempAuthor);
tempWriteCount=1;
i=j;
}
}
}
tempAuthor.setWriteCount(tempWriteCount);
resultList.add(tempAuthor);
for(author temp:resultList)
{
System.out.println("id="+temp.getId()+" name="+temp.getName()+" write "+temp.getWriteCount()+" book");
}
}
结果出错,Exception in thread "main" java.lang.OutOfMemoryError: Java heap space。自己想想就算是i=j没执行,也只执行10*10次不至于没存耗尽。最后发现在i=j处出现了问题,让i永远都小于10,出错。囧
后来改了下:
public void groupList(List<author> list)
{
if(list==null || list.size()==0)
{
return;
}
List<author> resultList=new ArrayList<author>();
int tempWriteCount=1;
author tempAuthor=new author();
for(int i=0;i<list.size();i++)
{
tempAuthor=list.get(i);
for(int j=i+1;j<list.size();)
{
if(tempAuthor.getId()==list.get(j).getId())
{
tempWriteCount+=1;
break;
}else {
tempAuthor.setWriteCount(tempWriteCount);
resultList.add(tempAuthor);
tempWriteCount=1;
i=j;
break;
}
}
}
tempAuthor.setWriteCount(tempWriteCount);
resultList.add(tempAuthor);
for(author temp:resultList)
{
System.out.println("id="+temp.getId()+" name="+temp.getName()+" write "+temp.getWriteCount()+" book");
}
}
运行结果
id=1 name=1ligang write 1 book
id=2 name=2ligang write 1 book
id=3 name=3ligang write 2 book
id=4 name=4ligang write 3 book
分组对了,计算结果不对。
改进时候将i=j;改为i=j-1;。成功。
再后来一想,只用遍历一遍list比较相邻的id是否相同即可。代码如下:
public void groupList(List<author> list)
{
if (list == null || list.size() == 0)
return;
int tempWriterCount=1;
author tempAuthor = new author();
List<author> resultList = new ArrayList<author>();
for (int i = 0; i < list.size()-1; i++)
{
tempAuthor = list.get(i);//get i
if(tempAuthor.getId()==list.get(i+1).getId())
{
tempWriterCount+=1;
}else {
tempAuthor.setWriteCount(tempWriterCount);
resultList.add(tempAuthor);
tempAuthor=new author();
tempWriterCount=1;
}
}
tempAuthor.setWriteCount(tempWriterCount);
resultList.add(tempAuthor);//add the last one
System.out.println("after group:");
for (author temp : resultList)
{
temp.setAuthorCount(resultList.size());
System.out.println(temp.getId()+" "+temp.getName()+" "+temp.getWriteCount());
}
}
最后还可以用List<List<author>>来分组,代码如下:
public void groupOrderedList(List<author> list)
{
List<List<author>> tempList = new ArrayList<List<author>>();
List<author> group = new ArrayList<author>();
List<author> displayList=new ArrayList<author>();
author temp = list.get(0);
group.add(temp);
for(int i=1;i<list.size();i++)
{
if(temp.getId()==list.get(i).getId())
{
group.add(temp);
}else {
tempList.add(group);
temp=list.get(i);
group=new ArrayList<author>();
group.add(temp);
}
}
tempList.add(group);
for (List<author> tempDisplayList : tempList)
{
author displayAuthor=new author();
displayAuthor=tempDisplayList.get(0);
displayAuthor.setWriteCount(tempDisplayList.size());
displayList.add(displayAuthor);
}
System.out.println("grouped list:");
for(author tempAuthor:displayList)
{
System.out.println(tempAuthor.getId()+" "+tempAuthor.getName()+" "+tempAuthor.getWriteCount());
}
System.out.println("total author is "+displayList.size());
}
分享到:
相关推荐
leetcode切割分组 leetcode 加减乘除运算 002_add_two_numbers.py # 链表数字做加法 029_divide_two_integers*.py # 实现整除 050_pow.py # 实现乘幂 066_plus_one.py # 数列末尾值+1 069_sqrt.py # 实现开根号 136_...
也称为 "双标记" , 必须成对出现 语法:<标记>内容标记> Demo : 1、创建 p 标记 --<p>...</p> 2、创建 div 标记 -- <div></div> 3、创建 header 标记 -- <header></header> 2、非封闭类型的标记 ...
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 16. Native SQL查询 16.1. 创建一个基于SQL的Query 16.2. 别名和属性引用 16.3. 命名SQL查询 16.3.1. 使用...
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 16. Native SQL查询 16.1. 创建一个基于SQL的Query 16.2. 别名和属性引用 16.3. 命名SQL查询 16.3.1. 使用...
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.1.1....
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.1.1....
投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.2. 别名和...
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.1.1....
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.1.1....
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.1.1. 标量...
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.1.1....
投影(Projections)、聚合(aggregation)和分组(grouping) 16.8. 离线(detached)查询和子查询 17. Native SQL查询 17.1. 创建一个基于SQL的Query 17.2. 别名和属性引用 17.3. 命名SQL查询 17.3.1. 使用...
15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.1.1. 标量...
16.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 16.8. 离线(detached)查询和子查询 17. Native SQL查询 17.1. 创建一个基于SQL的Query 17.2. 别名和属性引用 17.3. 命名SQL查询 17.3.1. ...
16.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 16.8. 离线(detached)查询和子查询 17. Native SQL查询 17.1. 创建一个基于SQL的Query 17.2. 别名和属性引用 17.3. 命名SQL查询 17.3.1. 使用...
13.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 13.8. 离线(detached)查询和子查询 14. 原生SQL查询 14.1. 使用ISQLQuery 14.1.1. 标量查询(Scalar queries) 14.1.2. 实体查询(Entity queries) ...
13.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 13.8. 离线(detached)查询和子查询 14. 原生SQL查询 14.1. 使用ISQLQuery 14.1.1. 标量查询(Scalar queries) 14.1.2. 实体查询(Entity queries) ...
A.11 修正的In-List 420 A.12 传递闭包 420 A.13 sysdate算术修正 421 A.14 对空值的索引 422 A.15 pga_aggregate_target 422 A.16 排序 422 A.17 分组 423 A.18 完备性检查 423 A.19 超出界限的情况 423 A.20 关于...
1. 层次结构模型: 层次结构模型实质上是一种有根结点的定向有序树,IMS(Information Manage-mentSystem)是其典型代表。 2. 网状结构模型:按照网状数据结构建立的数据库系统称为网状数据库系统,其典型代表是DBTG...
有序列表.htm 演示有序列表用法。 欲格式化文本.htm 欲格式化文本标记用法。 欲格式化文本行.html 欲格式化文本行标记用法。 第5章(源代码\第5章) 示例...