Note that there are some explanatory texts on larger screens.

plurals
  1. POCount how many times an element occurs in an array - Java
    primarykey
    data
    text
    <p>I recently made a very simple practice program in Python, that takes user input and rolls dice. The code is:</p> <pre><code>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) </code></pre> <p>(actually this is part of a larger program, so I had to cut'n'paste bits, and I might have missed something out).</p> <p>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 <code>someArray.count(someIndex)</code> in Java syntax. So my Java program looks like this so far:</p> <pre><code>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 &lt; 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></pre> <p>So can someone help me with the array counting code? I understand that this might not be a defined method, since Python <em>is</em> 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.</p> <p>EDIT: I managed something like this:</p> <pre><code>public static int arrayCount(int[] array, int item) { int amt = 0; for (int i = 0; i &lt; array.length; i++) { if (array[i] == item) { amt++; } else { amt = amt; } } return amt; } </code></pre> <p>EDIT: 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?)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload