java - how to count frequency for a 2d array -


hi im trying count frequency of 2d array . trying display frequency in way example if table :

0: 1 2 0

1: 2 0 1

2: 1 0 2

i want able count frequency :

0: 0 2 1

1: 2 0 1

2: 1 1 1

so way table should how many times 0 has appeared in first column , how many times 1 has appears in first column , on.

the code have far

    (int t = 0; t<s;t++)     {         int count= 0 ;          (int p = 0; p<s; p++)         {             if(table[p][t] ==p )             {                 count++              }             else if(t+1 != s)                 continue;             else                  table[p][t] = count;                  count = 0;         }     } 

thanks

for each column, make hashmap<int, int>. key entry, value frequency in column, e.g. column:

1) iterate on elements of column

2) every element of column, if exists in hashmap key already, value, increment 1 , add new value under same key. if has not been added yet, add value 1

make arraylist<hashmap<int, int>> contain 1 of these each column.

you keep hashmap<int, int> counts how many times element has appeared globally. way, if want preserve property every element has count every column, if count 0 column, can use global hashmap go through , add keys in every column's hashmap don't exist yet.

hashmap data structure allows associate value every key, , can add many entries unlike array, , entries don't have have keys successive integers. arraylist array can add , remove change number of elements contained in it.

http://docs.oracle.com/javase/6/docs/api/java/util/arraylist.html

http://docs.oracle.com/javase/6/docs/api/java/util/hashmap.html


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -