I need to create a program that uses arrays that shuffles a deck back into order. I am supposed to cut the original deck of 52 in half and place the cards in a new pile by alternately placing cards from either pile. The original order 1,2,3,4....49,50,51,52 should come out in the order 1,27,2,28,3...26,52.
public static void main(String[] args)
{
int[] array = new int[52];
int[] top = new int[26];
int[] bottom = new int[26];
for(int i=0; i < 52; i++)
{
array[i] = i+1;
}
for(int i = 0; i < 26; i++)
{
top[i] = array[i];
}
for(int i = 0; i < 26; i++)
{
bottom[i] = array[i+26];
}
System.out.println(shuffle(array, top, bottom));
}
public static int[] shuffle(int[] array, int[] top, int[] bottom)
{
for(int j = 0; j < top.length; j--)
{
array[j*2] = top[j];
array[j*2-1] = bottom[j];
}
return array;
}
My problem is that I am having an out of bounds exception. I am not sure how to fix this. Even if I do array[j*2+1] I still have this issue.