title: Create a Dictionary using Array of Object
body: <p>I have a problem with this code. My purpose is to create a Dictionary that counts the frequency of a word in a text, using an Array of Objects (I can't use Hash Map or something else). I created a class Pair that contains the couple (word,count).</p>
  
  <pre><code>public class Pair
  { public String word;
    public int count;
  
    public Pair(String word,int count)
    {this.word=word;
     this.count=count;
    }
  
    public String getWord()
    {return word;}
  
    public int getCount()
    {return count;}
  
  
    public void addCount()
    {count++;}
  
  
  
    public String toString()
    { return getWord()+" "+getCount();}
  
  }
  </code></pre>
  
  <p>And the class Dict that creates an Array of object using the Pair class </p>
  
  <pre><code>public class Dict
  
      {   private Pair [] a;
       private int inputSize;
  
  
      public Dict()
       {a=new Pair[10];
        inputSize=0;
       }
  
  
     public void insert(Pair x)
       { if(a.length==inputSize)
           { Pair newA []=new Pair [2*inputSize];
              for(int i=0;i&lt;inputSize;i++)
                   { newA[i]=a[i];
                   }
                    a=newA;
           }
  
         for(int i=0;i&lt;inputSize;i++)                 // i check if x is already in the array if i find it i replace it otherwise i add it in the array
            { if(a[i].getWord().equals(x.getWord()))
                  {a[i]=x;
                   }
           }
  
          a[inputSize++]=x;
      }
  
  
  
    public Pair find(Pair x)                             // if i don't find x return null
    { for(int i=0;i&lt;inputSize;i++)
          {  if(a[i].getWord().equals(x.getWord()))
                 {return a[i];}
  
          }
       return null;
  
    }
  
  
  
    public String toString()
    {String s="";
     for(int i=0;i&lt;inputSize;i++)
         { s=s+a[i].toString()+'\n';
         }
       return s;
    }
  }
  </code></pre>
  
  <p>After I created the test class with the main method</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class MyDict
  {public static void main(String [] args)
    { Dict d=new Dict();
      Scanner c=new Scanner(System.in);
  
      while(c.hasNext())
      {String s=c.next();
       Pair p=new Pair(s,1);        // create a new pair
       Pair previous=d.find(p);
       if(previous!=null)           //if the pair is already in the stack i add 1 to the counter otherwise i insert it in the array
         {p.count++;}
         else
        {d.insert(p);}
        s="";
  
      }
  
      System.out.println(d);
    }
  }
  </code></pre>
  
  <p>But it doesn't work, in particular the variable "count" doesn't grow.
  For example, if I write "how how are are you you " I get:</p>
  
  <pre><code>how 1
  are 1
  you 1
  </code></pre>
  
  <p>Can anyone help me please?</p>