Advertisement
StormWingDelta

UnknownJavaArrayContainsArrayIssue

Apr 2nd, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. /*Here's some of the inputs
  2. int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8};
  3. int[] list2 = {1, 2, 1};
  4. System.out.println(contains(list1, list2)); // true
  5. System.out.println(contains(list2, list1)); // false
  6. System.out.println(contains(new int[]{1, 2, 1, 2, 3}, new int[]{1, 2, 3})); //true
  7. System.out.println(contains(new int[]{8, 8, 8, 8, 4, 8, 8, 8, 8, 2, 8}, new int[]{8, 8, 8, 8, 4})); // this one is returning true but it shouldn't!
  8. */
  9. //something wrong with this code I have no clue what though and it's driving me nuts!!!!!!!
  10. public static boolean contains(int arr1[], int arr2[])
  11.     {
  12.         int arr2slot = 0;
  13.         //boolean
  14.         for(int i = 0; i < arr1.length; i++)
  15.         {
  16.             if(arr1[i] != arr2[arr2slot])
  17.             {
  18.                 arr2slot = 0;
  19.             }
  20.             if(arr1[i] == arr2[arr2slot] )
  21.             {
  22.                 if(arr2slot < arr2.length - 1)
  23.                 {
  24.                     arr2slot++;
  25.                 }
  26.             }
  27.            
  28.             if(arr2slot == arr2.length - 1 && arr1[i] == arr2[arr2slot])
  29.             {
  30.                 return true;
  31.             }
  32.            
  33.         }
  34.        
  35.         return false;
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement