使用java打印数组列表中元素的出现次数

时间:2017-01-21 10:08:25

标签: java arraylist

您好我已经创建了一个逻辑来计算数组列表中元素的出现次数,但它不是按照我想要的确切顺序打印。下面我提供了我的代码和我的要求。

我需要以下格式,

list: [1, 1, 5, 3, 7 ,7 ,7 , 3, 11, 2, 2, 3, 1]
number: 1, count: 2
number: 5, count: 1
number: 3, count: 1
number: 7, count: 3
number: 3, count: 1
number: 11, count: 1
number: 2, count: 2
number: 3, count: 1
number: 1, count: 1

但是,我的格式正在以下,

list: [1, 1, 5, 3, 7 ,7 ,7 , 3, 11, 2, 2, 3, 1]
number: 1, count: 3
number: 5, count: 1
number: 3, count: 3
number: 7, count: 3
number: 11, count: 1
number: 2, count: 2

这是我的代码

package com.abc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyArrayListSort
{
public static void main(String []args){
new MyArrayListSort().start();
}
public void start() {
    List<Integer> list = getList(1, 1, 5, 3, 7 ,7 ,7 , 3, 11, 2, 2, 3, 1);

    Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
    for (Integer i : list) {
        Integer retrievedValue = map.get(i);
        if (null == retrievedValue) {
            map.put(i, 1);
        }
        else {
            map.put(i, retrievedValue + 1);
        }
    }

    System.out.println("list: " + list);
    printCount(map);
}

private List<Integer> getList(int... numbers)
{
List<Integer> list = new ArrayList<Integer>();
for (int i : numbers)
{
    list.add(i);
}
return list;
}

private void printCount(Map<Integer, Integer> map)
{
for (Integer key : map.keySet())
{
    System.out.println("number: " + key + ", count: " + map.get(key));
}
}
}

2 个答案:

答案 0 :(得分:1)

<强>逻辑: 跟踪currentValue及其以连续顺序重复的次数。数字更改后,将值和计数到目前为止插入列表中 最后是剩余值(列表中的最终值),推送该值并计算。

修改 您可以完全删除newList并编写print语句,就像@Andreas在评论中指出的那样。

<强>代码:

List<Integer> list = Arrays.asList(1, 1, 5, 3, 7, 7, 7, 3, 11, 2, 2, 3, 1);

List<Pair<Integer, Integer>> newList = new ArrayList<>();

Integer previousValue = null;
Integer previousCount = 0;
for(Integer value : list) {
    if(previousValue == null) {
        previousValue = value;
        previousCount++;
    } else if(previousValue.intValue() == value.intValue()) {
        previousCount++;
    } else {
        newList.add(new Pair<>(previousValue, previousCount));
        previousValue = value;
        previousCount = 1;
    }
}
if(previousValue != null) {
    newList.add(new Pair<>(previousValue, previousCount));
}

for(Pair<Integer,Integer> pair : newList) {
    System.out.println(pair.getKey()  + ":" + pair.getValue());
}

<强>输出:

1:2
5:1
3:1
7:3
3:1
11:1
2:2
3:1
1:1

答案 1 :(得分:1)

这可能是解决此问题的最短代码版本:

List<Integer> list = Arrays.asList(1, 1, 5, 3, 7, 7, 7, 3, 11, 2, 2, 3, 1);
System.out.println("list: " + list);
for (int i = 0, prev = -1; i < list.size(); i++) {
    if (i == list.size() - 1 || ! list.get(i).equals(list.get(i + 1))) {
        System.out.printf("number: %d, count: %d%n", list.get(i), i - prev);
        prev = i;
    }
}

输出

list: [1, 1, 5, 3, 7, 7, 7, 3, 11, 2, 2, 3, 1]
number: 1, count: 2
number: 5, count: 1
number: 3, count: 1
number: 7, count: 3
number: 3, count: 1
number: 11, count: 1
number: 2, count: 2
number: 3, count: 1
number: 1, count: 1