form clusters of number pairs using java
<p>I have to print the similarity matrix of 110 documents which im able to print it and the output is the document pairs having highest similarity.</p>

<p>Now i have to form the clusters based on these document pairs.</p>

<p>The problem is im not able to store these document pairs as<code>(4,29),(29,76),(45,89)</code> the output has to be <code>(4,29,76),(45,89)</code> but here im not getting because the array list is getting overwritten whenever the maxarray method is called and the <strong>output is same as input</strong>.</p>

<pre><code>    public class SimilarityMatrix {


               public static void main(String[] args) throws IOException  {

                         int array[][]=new int[120][120];

      outputArray(array);
      }

     public static void outputArray(int[][] array) throws IOException {
                                            FileOutputStream out=new FileOutputStream("similaritymatrix.txt");
                    PrintStream p=new PrintStream(out); 

     int rowSize = array.length;
     int columnSize = array[0].length;
     for(int i = 1; i &lt;= 110; i++) {
     System.out.print("[");
     p.print("[");

     for(int j = 1; j &lt;= 109; j++) {
         if(i!=j &amp;&amp; i&lt;j)
         {
             app obj=new app();


             array[i][j]=         obj.main(i,j);


         }

         else if(i&gt;j || i==j)
         {
                  array[i][j]=0;
         }        
                            System.out.print(" " + array[i][j]);
                            p.print(" " + array[i][j]);






     }
     System.out.println(" ]");
      p.println(" ]");

     }
     System.out.println();
      p.println();
      max(array);
     }
      public static void max(int array[][]) throws FileNotFoundException
      {
                                                  FileOutputStream out=new FileOutputStream("similaritymatrix1.txt");
                    PrintStream p=new PrintStream(out); 

     int maxValue = 1;

      int i=1;

      int j=1;
     for (  i = 1; i &lt;= 110; i++) {
            for ( j = 1; j &lt;= 109; j++)
            {

            if (array[i][j] &gt; maxValue) {
            maxValue = array[i][j];

            }
            }}

             for ( i = 1; i &lt;= 110; i++) {
            for ( j = 1; j &lt;= 109; j++)
            {
            if(maxValue==array[i][j]){
                                  System.out.println("\nMax values in 2D array at d"+i+"d"+j+"max value ="+maxValue);


                                  p.println("\nMax values in 2D array at d"+i+"d"+j+"max value ="+maxValue);

    array[i][j]=0;
 ArrayList a = new ArrayList();
             a.add(i);
            a.add(j);
             if(!a.contains(i)&amp;&amp; !a.contains(j))
             {
                 a.add(i);
                 a.add(j);
                                   System.out.println("the cluster is" +a);

             }
                else  if(a.contains(i)&amp;&amp; !a.contains(j))
                  {
                      a.add(j);
                                        System.out.println("the cluster is" +a);

                  }
                else  if(!a.contains(i)&amp;&amp; a.contains(j))

                  {
                      a.add(i);
                                        System.out.println("the cluster is" +a);

                  }


                }}}


     for(  i=1;i&lt;=110;i++)
              {
                  p.print("[");
              System.out.print("[");

                  for( j=1;j&lt;=109;j++)
                  {
                      System.out.print(" "+ array[i][j]);
                      p.print(" "+ array[i][j]);

                  }

                p.println(" ]");
                System.out.println("]");


              }
        if(maxValue&gt;1)         
      max(array);


      }
    }

     class app {



        private static ArrayList&lt;String&gt; load(String f1) throws FileNotFoundException{

            Scanner reader = new Scanner(new File(f1));

            ArrayList&lt;String&gt; out = new ArrayList&lt;String&gt;();

            while (reader.hasNext()){

                String temp = reader.nextLine();

                String[] sts = temp.split(" ");

                for (int i = 0;i&lt;sts.length;i++){

                    if(sts[i] != "" &amp;&amp; sts[i] != " " &amp;&amp; sts[i] != "\n")

                        out.add(sts[i]);

                }

            }

            return out;

        }



        private static void write(ArrayList&lt;String&gt; out, String fname) throws IOException{


            FileWriter writer = new FileWriter(new File(fname));

            for (int i = 0;i&lt;out.size();i++){

                writer.write(out.get(i) + "\t");

            }

            writer.close();

        }



        public static int main(int a,int b) throws IOException {

    int count=0;

            ArrayList&lt;String&gt; file1;

            ArrayList&lt;String&gt; file2;

            ArrayList&lt;String&gt; out = new ArrayList&lt;String&gt;();

            file1 = load(a+".txt");

            file2 = load(b+".txt");

            for(int i = 0;i&lt;file1.size();i++){

        String word1 = file1.get(i);

        for (int z = 0; z &lt;file2.size(); z++){

            if (word1.equalsIgnoreCase(file2.get(z))){


                        boolean already = false;

                        for (int q = 0;q&lt;out.size();q++){

                            if (out.get(q).equalsIgnoreCase(file1.get(i))){

                                already = true;


                            }

                        }


                        if (already==false){

                            out.add(file1.get(i));

    count++;
                        }}}}


                    return count;








        }


        }
</code></pre>

<p>this is the code i've tried so far and im getting the output but it is same as input</p>
