算上一个数组中的元素出现的次数 - Java的 [英] Count how many times an element occurs in an array - Java

查看:190
本文介绍了算上一个数组中的元素出现的次数 - Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在Python一个非常简单的练习程序,即需要用户输入和滚动骰子。在code是:

I recently made a very simple practice program in Python, that takes user input and rolls dice. The code is:

import random
import sys
import math

def roll(rolls, sides, results):
    for rolls in range(1, rolls + 1):
        result = random.randrange(1, sides + 1)
        print result
        results.append(result)
def countf(rolls, sides, results):
    i = 1
    print "There were", rolls, "rolls."
    for sides in range(1, sides + 1):
        if results.count(i) != 1:
            print "There were", results.count(i), i,"s."
        else:
            print "There was", results.count(i), i
        i = i + 1
        if i == sides:
            break
    rolls = input("How many rolls? ")
        sides = input("How many sides of the die? ")
        results = []

        roll(rolls, sides, results)
        countf(rolls, sides, results)

(其实这是一个更大的计划的一部分,所以我不得不cut'n'paste位,我可能会错过一些东西)。

(actually this is part of a larger program, so I had to cut'n'paste bits, and I might have missed something out).

所以我决定把这一去渣。请注意这里的算法:得到随机数,打印,将其追加到一个数组,再算上在最后阵列中的每个数字量,并打印出该值。问题是,我不知道该怎么办 someArray.count(someIndex)的Java语法的等价物。所以我的Java程序看起来像这样至今:

And so I decided to translate that to Java. Notice the algorithm here: get random number, print it, append it to an array, then count the amount of each number in the array at the end, and print out that value. Problem is, I don't know how to do the equivalent of someArray.count(someIndex) in Java syntax. So my Java program looks like this so far:

import java.util.*;

public class Dice {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        final static int TIMES_TO_ROLL = getInt("Times to roll?");
        Random flip = new Random();
        int[] results = new int[TIMES_TO_ROLL];
        for (int i = 0; i < TIMES_TO_ROLL; i++) {
            int result = flip.nextInt(6);
            System.out.println(result);
            results[i] = result;
        }
    }
    public static int getInt(String prompt) {
        System.out.print(prompt + " ");
        int integer = input.nextInt();
        input.nextLine();
        return integer;
    }
}

所以,有人可以帮我阵列计数code?我明白,这可能不是一个定义的方法,因为Python的的更高毕竟水平,所以我可以做我自己的数组计数法,但我想知道如果用Java代码,如Python,有一个predefined之一。

So can someone help me with the array counting code? I understand that this might not be a defined method, since Python is higher level after all, so I could make my own array counting method, but I was wondering if Java, like Python, has a predefined one.

编辑:我管理的是这样的:

I managed something like this:

public static int arrayCount(int[] array, int item) {
    int amt = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] == item) {
            amt++;
        }
        else {
            amt = amt;
        }
    }
    return amt;
}

编辑:只是出于兴趣,假设我用命令提示符下运行(对于Python命令提示符控制台),哪一个会更快我的Java程序和Python.exe(换句话说,对于同一个code,这语言有更好的表现?)?

Just out of interest, assuming I use Command prompt to run my Java program and Python.exe (command prompt console for Python), which one will be faster (in other words, for the same code, which language has better performance?)?

推荐答案

有几个库,会为你做这一点:

There are a couple libraries that will do this for you:


  1. 谷歌番石榴的<一个href=\"http://docs.guava-libraries.google$c$c.com/git/javadoc/com/google/common/collect/Multiset.html\"相对=nofollow>多重集

  2. 阿帕奇常见的

  1. Google Guava's MultiSet
  2. Apache Common's Bag

但是对于这么简单的东西,你可以考虑一个额外的库有点过分了。

But for something so simple, you may consider an extra library a bit excessive.

您还可以用 INT [] 自己做。假设你的骰子是用完整的数字,已经推出数指的是索引到数组,然后递增该索引处的值。当您需要检索值对于给定的数字,通过索引查找它的价值。

You can also do this yourself with an int[]. Assuming your dice is using whole numbers, have the number rolled refer to the index into the array, and then increment the value at that index. When you need to retrieve the value for a given number, look up its value by the index.

private static final int NUMBER_DICE_SIDES = 6;
public static void main(String[] args) {
    final static int TIMES_TO_ROLL = getInt("Times to roll?");
    Random flip = new Random(NUMBER_DICE_SIDES);
    int[] results = new int[NUMBER_DICE_SIDES];
    for (int i = 0; i < TIMES_TO_ROLL; i++) {
        int result = flip.nextInt;
        System.out.println(result);
        results[result]++;
    }

    for(int i = 0; i < NUMBER_DICE_SIDES; ++i) {
        System.out.println((i+1)+"'s: " + arraysCount(results, i));
    }
}

public static int arrayCount(int[] array, int item) {
    return array[item];
}

这篇关于算上一个数组中的元素出现的次数 - Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆