Where I'm I wrong? I need to do a count of the number of each element in an array
I cant get this to work. Im trying to count how many times a number s put into the location of a multidimensional array...I will bold what I mean:
Code Java:
import java.util.*;
public class Assign1b
{
//-------------------------------
// Variable Declaration
//-------------------------------
static int[][] dataArray = new int [10][10]; //Creates a ten by ten integer array
public static void arrayFiller(int[][] inputArray)
{ //Input: Accepts a 2D integer array as a parameter.
//Output: This method does not print any arrays.
//Return: This method does not return anything.
//Descr: This method fills an array with random numbers.
Random generator = new Random(); //creates a generator object from the random class
//for loops that fills array.
for(int col = 0; col < dataArray.length; col++)
{
for (int row = 0; row < dataArray[col].length; row++)
{
dataArray[col][row] = generator.nextInt(26);
[B]digitCount[(dataArray[col][row])]++;[/B]
^
|
//can I do this? say the number at dataArray[1][1] is 13...can I add a count to a location in digit count like this? or how would I do it? I want to do it as soon as each random number is put into the 2D array as I believe this would be much easier. Thanks in advance!!!
}//end of inner for loop
}//end of outer for loop
}//end of arrayFiller()
public static void print2DArray (int[][] newArray)
{ //Input: This method accepts a 2D array as a parameter.
//Output: This method prints out the 2D array filled with random numbers.
//Return: This method does not return anything.
//Descr: This method prints out the 2D array filled with random numbers.
//Prints out header
System.out.print ("Array: dataArray |");
for (int z = 1; z <= newArray[0].length; z++)
{
System.out.printf("%5d", z);
}//end of for loop
System.out.print(" |\n");
System.out.print(" \t\t\t\t");
for (int q = 0; q <= newArray[0].length; q++)
{
System.out.print("-----");
}//end of for loop
System.out.println();
//Prints out body
for (int x = 0; x < newArray.length; x++)
{
System.out.printf("%16d |",(x + 1));
for(int y = 0; y < newArray[x].length; y++)
{
System.out.printf("%5d",newArray[y][x]);
}//End of inner for loop
System.out.print(" |\n");
}//End of outer for loop
//Prints out footer
System.out.print(" \t\t\t\t");
for (int q = 0; q <= newArray[0].length; q++)
{
System.out.print("-----");
}//end of for loop
}//end of print2DArray()
public static void main (String[] args)
{
arrayFiller(dataArray);
print2DArray(dataArray);
}//End of main()
}//End of class: Assign1b
Re: Where I'm I wrong? I need to do a count of the number of each element in an array
also, I havent declared digitCount as a 1D array yet, I just put it in there atm to see if this was possible.
Re: Where I'm I wrong? I need to do a count of the number of each element in an array
Code :
digitCount[(dataArray[col][row])]++;
your code is correct.
But what would be the size of your count 1d array .It should be the maximum number your random no generator
returns you which i think will be wastage of memory.
If you generate no between 1-100 then size of the count array should be 100 in this case.
Eg:
Random No generated are
1,23,45,2,67,5,2,1,2,8,5
then at least 67 index must be avialable.
If it is possible to allocate memory to the particular index of the count array which contains some count
that can save memory.
I think you can use Hash table for this.
click here for hash table eg: