`
faulware
  • 浏览: 11685 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

对有序list分组

阅读更多
    近来要对一个有序的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());
    }
分享到:
评论

相关推荐

    Q703207 list如何实现动态分组

    这里的“Q703207 list如何实现动态分组”指的是如何使用编程语言(可能是Python或其他支持list操作的语言)对list中的元素进行动态的、基于某种规则的分组操作。在Python中,`groupby`函数通常是实现这一功能的关键...

    java8 stream自定义分组求和并排序的实现

    `TreeMap` 是一个有序的映射表,可以根据 key 的自然顺序对结果进行排序。在本文中,我们使用 `compareTo` 方法来对 `GroupDetailDTO` 对象进行排序,该方法可以根据 `headsetId` 和 `time` 属性对对象进行排序。 ...

    ListView分组显示

    在Android开发中,...总之,ListView的分组显示是通过自定义Adapter和合理组织数据源来实现的,它提高了用户体验,使数据浏览更加有序。在Android开发中,熟练掌握这项技术对于构建高效、用户友好的应用至关重要。

    固定gridview列按照选择进行分组查询

    分组是数据分析的一种常见操作,可以帮助用户以更有序的方式查看数据。在GridView中,可以利用`GridView.GroupBy`方法或者在数据源查询阶段实现。用户的选择通常来自于下拉框、复选框等控件,当用户做出选择后,需要...

    redis有序集合性能 列表、集合、有序集合1

    有序集合的内部编码通常是跳跃列表(Skip List)加上字典(Dictionary)。跳跃列表用于快速查找元素,字典则用于根据成员查找分数。 1.3.3 使用场景 - 排行榜:例如用户得分排行榜,可以根据分数进行排序。 - ...

    LIST CONTROL

    或者实现排序和分组,使列表更有序。此外,还可以添加右键菜单,提供更多的操作选项,如编辑快捷方式属性、删除快捷方式等。 总的来说,"List Control"在桌面快捷方式管理中的应用,结合了Windows编程的核心技术,...

    分组listview

    在Android开发中,"分组ListView"是一种常见的数据展示方式,尤其在处理大量有序数据时。这个场景下,"仿iPhone ListView"指的是模仿iOS中类似TableView的交互效果,特别是其在滑动时保持分组标题可见的功能。...

    Vue中v-for的数据分组实例

    然而,有时候我们可能需要对这些数据进行分组,以便在界面上更有序地呈现。在这种情况下,Vue的`computed`属性就派上用场了,它允许我们创建基于已有数据的计算属性,从而实现动态数据分组。 在提供的示例中,我们...

    Java判断List中相同值元素的个数实例

    在Java编程中,有时我们需要统计一个List集合中相同值出现...这段代码将使用流API对List进行收集,通过`groupingBy`按元素分组,并使用`counting`来计算每个分组的数量,最终得到一个Map,Key为元素,Value为出现次数。

    冒泡排序、选择排序、插入排序和希尔排序

    希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。 希尔排序的关键部分是增量的选择和...

    python列表list保留顺序去重的实例

    对于特定的场景,还可以考虑使用`itertools`模块中的`groupby()`函数,它可以用于分组相同的连续元素,但这个方法通常适用于元素已排序的情况。 总结起来,Python中保留顺序去重的方法主要有以下几种: 1. 使用列表...

    C# 算法大全 集各种算法于一身

    这段代码首先通过一个初始化的增量序列`inc`(这里取为列表长度的1/9,然后每次乘以3再加1,直到1)来分组,然后对每个组进行插入排序。在插入排序部分,它通过交换元素来保证当前元素在正确的位置上,这个过程会...

    C#排序算法冒泡 选择 插入 希尔

    希尔排序是插入排序的一种更高效的改进版本,通过分组进行插入排序,使得原本较远的元素在排序过程中可以更快地接近其最终位置。其主要步骤如下: - 选择一个增量序列,例如 h = 1, 3, 9, ..., 3^k ... (1/3) * n ...

    C#所有排序算法

    下面将对每种排序算法进行详细解析。 ### 冒泡排序(Bubble Sort) 冒泡排序是一种简单的排序算法,它重复地遍历待排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地...

    CSS代码属性分组的写法例子

    在CSS(层叠样式表)编码中,属性分组是一种优化和提高代码可读性的技巧。...同时,结合其他最佳实践,如使用CSS预处理器(如Sass或Less)、模块化CSS(如BEM)以及遵循一定的命名约定,可以使CSS代码更加有序和高效。

    排序算法.txt

    其基本思想是:把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。 **代码实现:** ```csharp ...

    解密ThinkPHP3.1.2版本之独立分组功能应用

    独立分组提供了一种更加灵活、模块化的项目结构,使得开发过程更加有序,便于团队协作和后期维护。 1. **独立分组概述** - 独立分组不会影响原有的分组模式,原有分组只需调整目录结构即可升级,无需修改应用代码...

    C#排序算法大全

    希尔排序是一种改进的插入排序,通过设置不同的间隔序列来分组,然后对每个组进行插入排序。C#代码示例如下: ```csharp using System; namespace ShellSorter { public class ShellSorter { public void Sort...

    leetcode切割分组-leetcode:leetcode

    leetcode切割分组 leetcode 加减乘除运算 002_add_two_numbers.py # 链表数字做加法 029_divide_two_integers*.py # 实现整除 050_pow.py # 实现乘幂 066_plus_one.py # 数列末尾值+1 069_sqrt.py # 实现开根号 136_...

    jQuery-selectList:用于替换标准 HTML(多个)选择元素的 jQuery 插件

    3. **分组显示**:支持将选项分组,每个组有自己的标题,使数据组织更有序,便于用户浏览。 4. **多选模式**:用户可以同时选择多个选项,通过键盘或鼠标进行操作,提供了一种便捷的多选方式。 5. **事件处理**:...

Global site tag (gtag.js) - Google Analytics