I have a 2D array and i am trying to look for Similarities in the data, via number patterns.

For Example:

1) 3,4,5,6,7
2) 1,4,8,23,56
3) 14,15,16,34,45

Now what i would like to do is search this array to find patterns like in example 1. it goes 3,4,5,6,7 or in 3. it goes 14,15,16.

I have no clue how to start this if anyone could point me in the right direction that would be great!

Recommended Answers

All 3 Replies

Well,you need to define different functions that check for different patterns.If the patterns is very large,then it would be tedious to write the amount of code to verify any pattern.For exampl to check if a line numbered n from a vxv dimensional array contains a pattern defined like:
n1,n2,n3 are related by n3-n2=n2-n1
Then a corresponding checking program would look like:

class PatternFinder{
    public static boolean pattern0(int[] x){
        /*Code to check if it is a pattern    * 
         *If the condition holds for the      *
         *first three numbers n1,n2,n3 then   *
         *check for n2,n3,n4,else return false*/
    }

    public static void main(String args[]){
        int[][] myarray={{1,2,3,4},
                         {9,12,15,18},
                         {10,12,20,76},
                         {-3,0,3,7}};
        int[] temp;
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                temp[j]=myarray[i][j];
            }
            if(pattern0(temp)){
                System.out.println("Pattern found between");
                for(int v=0;v<j;v++)
                    System.out.print(temp[v] + "  ");

            }
        }
    }
}

Thank you so much, that helps so much!

I have one more question, if i had an array with the values 1,2,3.

How would I go about getting the out put of all possible scenarios via,

1 2
1 3
2 3

Except I am trying to do this with an array with the numbers 1-35, and 5 values would be printed.

Again Thank you so much you have already help me so much!

/*Say you have a one-dimensional array with non-repeating*
 *elements as follows: int[] v={1,2,3,4,5}.Then you would*
 *require nested loop(for inside for to accomplish it. */

 class Alpha{

    public static void main(String args[]){
        int[] v={1,2,3,4,5};
        for(int i=0;i<5;i++){
            for(int j=i+1;j<5;j++){  //Mark this as Flag0
                System.out.println(v[i] + " " + v[j]);
            }
        }
    }

}

This will print only pairs like (x,y) where x!=y and also (x,y) is the same as (y,x).To do this for a triplet or quad,you need to define three or four nested loops respectively.

Note:In the line marked as Flag0 changing the initialization condition for the for loop to this:
for(int j=i;j<5;j++)
will make the program print pairs of(x,y) where x=y

And writing this:
for(int j=0;j<5;j++)
will make it print all possible values(all 25 of them) where x may be equal to y and (x,y) and (x,y) will be printed.

Hope this answered your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.