OoOoOoOoOoO
OoOoOoOoOoO

Reputation: 131

Finding a duplication of an integer pattern in an array list

I have to find a pattern in the array that I am creating and then stop adding numbers to the array when the pattern is seen to be duplicated twice. The pattern that I am trying to find is 4-2-1, so when that is repeated twice in a row in this arithmetic sequence, the program is finished. Currently my output gives me one sequence of the pattern with an error following: [10, 5, 16, 8, 4, 2, 1] Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 0

**YES I HAVE TRIED TO DEBUG USING ECLIPSE BUT I AM STILL STUCK So if someone could help me figure out why it is only printing the sequence with one repetition of the pattern at the end, that would help me profusely. Thanks. My code:

Scanner inData = new Scanner(new File("test.txt"));
    ArrayList<Integer> list= new ArrayList<Integer>();

        while (inData.hasNext())
        {

            int numA=inData.nextInt(); 
            int var;
            boolean done=false;


            list.add(numA);

            while(!done)
            {
                var=list.size();

                for(int i=var;i>0;i--)
                {
                    numA=list.get(var-1);
                    if(numA%2==0)
                    {
                        numA/=2;
                        list.add(numA);

                    }
                    else
                    {
                        numA=(numA*3)+1;
                        list.add(numA);
                    }
                    var=list.size();

                    if (var>6)
                    {
                    for(int j=1;j>=6;j++)
                    {
                        if(list.get(var-1)==1)
                        {
                            if(list.get(var-4)==1)
                            {
                                if(list.get(var-2)==2)
                                    {
                                        if(list.get(var-5)==2)
                                        {
                                            if(list.get(var-3)==4)
                                            {
                                                if(list.get(var-6)==4)
                                                {
                                                    done=true;
                                                }
                                            }
                                        }
                                    }

                            }

                    }

                }

                System.out.print(list);
                list.clear(); 
            }

        }

    }Scanner inData = new Scanner(new File("test.txt"));
    ArrayList<Integer> list= new ArrayList<Integer>();

        while (inData.hasNext())
        {

            int numA=inData.nextInt(); 
            int var;
            boolean done=false;


            list.add(numA);

            while(!done)
            {
                var=list.size();

                for(int i=var;i>0;i--)
                {
                    numA=list.get(var-1);
                    if(numA%2==0)
                    {
                        numA/=2;
                        list.add(numA);

                    }
                    else
                    {
                        numA=(numA*3)+1;
                        list.add(numA);
                    }
                    var=list.size();

                    if (var>6)
                    {
                    for(int j=1;j>=6;j++)
                    {
                        if(list.get(var-1)==1)
                        {
                            if(list.get(var-4)==1)
                            {
                                if(list.get(var-2)==2)
                                    {
                                        if(list.get(var-5)==2)
                                        {
                                            if(list.get(var-3)==4)
                                            {
                                                if(list.get(var-6)==4)
                                                {
                                                    done=true;
                                                }
                                            }
                                        }
                                    }

                            }

                    }

                }

                System.out.print(list);
                list.clear(); 
            }

        }

    }

Upvotes: 0

Views: 554

Answers (2)

Reza
Reza

Reputation: 1546

The simplest approach is using Collections.indexOfSubList. Something like this.

int a [] = {4,2,1,4,2,1};
if(Collections.indexOfSubList(list, Arrays.asList(a))!=-1){
       return true;
}
return false

Upvotes: 1

k_g
k_g

Reputation: 4464

Well, there appear to be a few errors with your code.

I'm going to enumerate some here: fix as many as possible, then check to see if your code works.

  1. int i=var; i>0; i--. Since var is the size of your array, it is outside the bounds. Java uses 0-based arrays, meaning that valid indeces are in the range [0,size-1] (inclusive). Replace with int i=var - 1; i >= 0; i--
  2. The j loop does nothing but prevent anything from happening (nothing can enter, since you start at 1 and immediately break the loop by imposing the restriction >=6 and j is never used. Remove the loop.

Upvotes: 0

Related Questions