简体   繁体   English

无法使用Java在ArrayList中计数

[英]Cannot count number in ArrayList using java

I am using the List as shown below:- 我正在使用列表,如下所示:

List<Integer> integerlist = new ArrayList<Integer>();

integerlist=primeFactors(numberToBERadical);

The primeFactors method return me some number when i sysOut as:- 当我sysOut时,primeFactors方法返回给我一些数字:-

System.out.println("integer list=="+integerlist);
integer list==[2, 2, 2, 3, 5]

My Problem is i want to count how many time that each number appears so that i can show Like this:- 我的问题是我想计算每个数字出现多少次,以便我可以像这样显示:

2^3 * 3 * 5

You need to associate each factor with the number of times it occurs (the exponent). 您需要将每个因子与其发生的次数(指数)相关联。

To do that, a better data structure than a List would be a Map<Integer,Integer> . 为此,比List更好的数据结构是Map<Integer,Integer>

Let's assume you still get a List from your primeFactors(numberToBERadical); 假设您仍然从primeFactors(numberToBERadical);获取List primeFactors(numberToBERadical); (assuming you can't change it or don't want to for some reason). (假设您无法更改它或出于某种原因而不想更改)。

You can do something like: 您可以执行以下操作:

public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();

    list.add(2);
    list.add(2);
    list.add(2);
    list.add(3);
    list.add(3);
    list.add(5);

    Map<Integer, Integer> factors = new HashMap<>();
    for(Integer fact: list){
        if(!factors.containsKey(fact)) { factors.put(fact, 1);}
        else {
            Integer value = factors.get(fact);
            factors.put(fact, ++value);
        }
    }
    System.out.println(factors);
}

Prints: {2=3, 3=2, 5=1} 打印: {2=3, 3=2, 5=1}

If the array is sorted, you can count them like: 如果数组已排序,则可以像这样对它们进行计数:

int count = 0;
int last = 0; // zero is not possible for prime-factors
for(int i=0;i<list.size();i++) {
    if(i == 0 || last == list.get(i)) count++;
    else {
        if(last > list.get(0)) {
            System.out.print(" * ");
        }

        System.out.print(last + (count > 1 ? "^" + count: ""));
        count = 1;
    }
    last = list.get(i);
}
//print last sequence.
if(last > 0) {
    if(last > list.get(0)) {
        System.out.print(" * ");
    }

    System.out.print(last + (count > 1 ? "^" + count: ""));
}

One possible, but not the simplest solution: 一种可能,但不是最简单的解决方案:

public Map<Integer, AtomicInteger> primeFactors(List<Integer integerList) {
  final Map<Integer, AtomicInteger> count = new HashMap<Integer, AtomicInteger>();

  for (final Integer number : integerlist) {
    if (count.containsKey(number)) {
      count.get(number).incrementAndGet();
    } else {
      count.put(number, new AtomicInteger(1));
    }
  }

  return count;
}

You need to use a Map<Integer, Integer> which you can override the put method in in order to count the instances of each Integer . 您需要使用Map<Integer, Integer> ,您可以重写put方法以便计算每个Integer的实例。

final Map<Integer, Integer> myStringMap = new HashMap<>(){
 @override
 public String put(final Integer key, final Integer value) {
   if(contains(key)) {
     return put(key, get(key) + 1);
   } else {
     return put(key, 1);
   } 
 }
};

You could even use a TreeMap to have the prime factors sorted by size. 您甚至可以使用TreeMap将主要因子按大小排序。 I answered a very similar question here . 在这里回答了一个非常类似的问题。 Simply loop over your List and dump it into the map 只需遍历您的List并将其转储到地图中

for(final Integer integer : myList) {
  myCountingMap.put(integer, 1);
}

Even better would be to change your factorising method to return a Map to begin with. 更好的办法是更改分解方法以返回Map

List<Integer> inputList = new ArrayList<Integer>();

    inputList.add(2);
    inputList.add(2);
    inputList.add(2);
    inputList.add(3);
    inputList.add(3);
    inputList.add(4);

    Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();

    boolean flag = false;

    for(int val : inputList)
    {
        if(resultMap.get(val) == null)
        {
            resultMap.put(val, 1);
        }
        else
        {
            resultMap.put(val, (resultMap.get(val).intValue())+1);
        }
    }

    for(int key : resultMap.keySet())
    {
        if(resultMap.get(key) == 1)
        {
            if(!flag)
            {
                System.out.print(key);
                flag = true;
            }
            else
            {
                System.out.print("*"+key);
            }
        }
        else
        {
            if(!flag)
            {
                System.out.print(key+"^"+resultMap.get(key));
                flag = true;
            }
            else
            {
                System.out.print("*"+key+"^"+resultMap.get(key));
            }
        }
    }

    System.out.println();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM