Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Where I'm I wrong? I need to do a count of the number of each element in an array

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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
    Last edited by JavaPF; January 21st, 2011 at 05:25 AM. Reason: Please use [highlight=Java] code [/highlight] tags


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  3. #3
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Where I'm I wrong? I need to do a count of the number of each element in an array

    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:
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Replies: 3
    Last Post: January 28th, 2011, 09:37 AM
  2. Java Dos Logic Test count all non increasing number
    By Jhovarie in forum Object Oriented Programming
    Replies: 3
    Last Post: January 13th, 2011, 03:28 PM
  3. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 03:26 PM
  4. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 06:22 PM
  5. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 09:04 AM