Results 1 to 11 of 11
  1. #1
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default Out of Bounds. I dont know Why :L

    I get an out of bounds when i run this program and i am unsure why! ive been trying to debug this for quite a while now and im still stuckk

    Java Code:
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.Collections;
    
    
    public class SaintNickSorting {
    	public static void main(String[] args)
    	{
    		//creating and populating the array
    		Integer[] sortArray = new Integer[20];
    		sortArray[0] = 1;
    		sortArray[1] = 17;
    		sortArray[2] = 18;
    		sortArray[3] = 3;
    		sortArray[4] = 8;
    		sortArray[5] = 6;
    		sortArray[6] = 11;
    		sortArray[7] = 20;
    		sortArray[8] = 19;
    		sortArray[9] = 2;
    		sortArray[10] = 4;
    		sortArray[11] = 5;
    		sortArray[12] = 12;
    		sortArray[13] = 16;
    		sortArray[14] = 7;
    		sortArray[15] = 10;
    		sortArray[16] = 14;
    		sortArray[17] = 13;
    		sortArray[18] = 9;
    		sortArray[19] = 15;
    		
    		// Printing the array
    		System.out.println(sortArray
    		System.out.println("Merry Christmas Mr. Spiegel");		
    	}
    	
    	public static ArrayList<Integer> JollySorting(Integer [] sortArray) //METHOD to sort the array
    	{
    		Arrays.sort(sortArray);
    		Integer[] Sort1 = new Integer[10];
    		for (int i = 0; i < sortArray.length/2; i++)
    		{
    			Sort1[i] = sortArray[i];
    		}
    		Integer[] Sort2 = new Integer[10];
    		for (int a = 10; a < sortArray.length - 1; a++)
    		{
    			Sort2[a - 10] = sortArray[a];
    		}
    		
    		Arrays.sort(Sort2, Collections.reverseOrder());
    		ArrayList<Integer> sortedNums = new ArrayList<Integer>();
    		
    		for (int a = 0; a < sortArray.length - 1; a++)
    		{
    			sortedNums.add(Sort1[a]);
    			sortedNums.add(Sort2[a]);
    		}
    		return sortedNums;
    	}
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I compiled it and ran what you gave us but since the main method doesn't call the JollySorting method I didn't run into any problems except:
    the print array line:
    Java Code:
    System.out.println(sortArray);
    outputs a memory address since that's not the correct way to print items in an array, I am assuming you wanted to have it put out the numbers in the array.

    you need to loop through and print item each iteration for that
    Java Code:
    for(int i = 0; i < sortArray.length; i++)
    {
      System.out.print(sortArray[i] + ", ");
    }
    System.out.println();
    System.out.println("Merry Christmas Mr. Spiegel");
    Another thing, you can initialize the array much faster with:
    Java Code:
    Integer[] sortArray = {
    			1, 17, 18, 3, 8, 6, 11, 20, 19, 2, 
    			4, 5, 12, 16, 7, 10, 14, 13, 9, 15,
    		};
    works the same, just easier for you to type.

    Also, can you explain what you want the program to do exactly?

    when I know more about what you want it to do I can help you out a bit better.
    This seems wrong to me:
    Java Code:
    Integer[] Sort2 = new Integer[10];
    		for (int a = 10; a < sortArray.length - 1; a++)
    		{
    			Sort2[a - 10] = sortArray[a];
    		}
    I know for sure that you shouldn't need the - 1, int a = 10; sortArray.length is 20, sa - 1 = 19, so it will stop putting items into sort2 at 18 since 19 is not less than 19.
    This statement will put 9 items into the array that fits 10 items.
    Java Code:
    a=10
    a=11
    a=12
    a=13
    a=14
    a=15
    a=16
    a=17
    a=18
    Last edited by sunde887; 01-13-2011 at 12:03 AM.

  3. #3
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    I need the program to sort like this
    A set of numbers is jolly sorted if the elements are in alternating ascending and descending order.
    For example, consider the set of numbers {1, 5, 6, 8, 9, 2, 3, 4, 7}. An example of a jolly sort of this set would be {1, 5, 2, 4, 3, 9, 6, 8, 7}.

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by socboy6579 View Post
    I get an out of bounds when i run this program and i am unsure why! ive been trying to debug this for quite a while now and im still stuckk

    Java Code:
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.Collections;
    
    
    public class SaintNickSorting {
    	public static void main(String[] args)
    	{
    		//creating and populating the array
    		Integer[] sortArray = new Integer[20];
    		sortArray[0] = 1;
    		sortArray[1] = 17;
    		sortArray[2] = 18;
    		sortArray[3] = 3;
    		sortArray[4] = 8;
    		sortArray[5] = 6;
    		sortArray[6] = 11;
    		sortArray[7] = 20;
    		sortArray[8] = 19;
    		sortArray[9] = 2;
    		sortArray[10] = 4;
    		sortArray[11] = 5;
    		sortArray[12] = 12;
    		sortArray[13] = 16;
    		sortArray[14] = 7;
    		sortArray[15] = 10;
    		sortArray[16] = 14;
    		sortArray[17] = 13;
    		sortArray[18] = 9;
    		sortArray[19] = 15;
    		
    		// Printing the array
    		System.out.println(sortArray
    		System.out.println("Merry Christmas Mr. Spiegel");		
    	}
    	
    	public static ArrayList<Integer> JollySorting(Integer [] sortArray) //METHOD to sort the array
    	{
    		Arrays.sort(sortArray);
    		Integer[] Sort1 = new Integer[10];
    		for (int i = 0; i < sortArray.length/2; i++)
    		{
    			Sort1[i] = sortArray[i];
    		}
    		Integer[] Sort2 = new Integer[10];
    		for (int a = 10; a < sortArray.length - 1; a++)
    		{
    			Sort2[a - 10] = sortArray[a];
    		}
    		
    		Arrays.sort(Sort2, Collections.reverseOrder());
    		ArrayList<Integer> sortedNums = new ArrayList<Integer>();
    		
    		for (int a = 0; a < sortArray.length - 1; a++)
    		{
    			sortedNums.add(Sort1[a]);
    			sortedNums.add(Sort2[a]);
    		}
    		return sortedNums;
    	}
    }
    So which lines are causing the problem? Can you please show the code that actually calls your JollySorting method, your main method currently looks incomplete? Also why are you switching between Arrays and ArrayLists?
    --user0--

  5. #5
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Java Code:
    		for (int a = 0; a < sortArray.length - 1; a++)
    		{
    			sortedNums.add(Sort1[a]);
    			sortedNums.add(Sort2[a]);
    		}
    Sort1 and Sort2 are dimensioned to 10 here, but loop goes 0 to 19.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, I understand what you are trying, why switch to arraylist as user0 said though?

    You should be able to cut the list in half, and loop through a new array of 20 ints setting i as sort1, i + 1 as sort2

    Java Code:
    Arrays.sort(sortArray);
    int[] sort1 = new int[10];
    int[] sort2 = new int[10];
    for(int i = 0; i < sortArray / 2; i++)
    {
      sort1[i] = sortArray[i];
    }
    for(int i = 10; i < sortArray.length; i++)
    {
      sort2[i - 10] = sortArray[i];
    }
    int[] newSort = new int[20];
    for(int i = 0, j = 0; i < newSort.length; i += 2)
    {
      newSort[i] = sort1[j];
      newSort[i + 1] = sort2[j];
      j++;
    }
    I made that change and a few more changes, mainly, I defined everything as a primitive int[] instead of Integer, sorted both arrays, then created a new method to reverse arrays, output was:
    Java Code:
    1, 20, 2, 19, 3, 18, 4, 17, 5, 16, 6, 15, 7, 14, 8, 13, 9, 12, 10, 11,
    Merry Christmas Mr. Spiegel
    also, JollySorting can be thought of differently, it doesn't need to return anything since it's changing the input, you can make it have void return type.
    Last edited by sunde887; 01-13-2011 at 12:37 AM.

  7. #7
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    ok right now i have an output of the numbers, but i have them printed in order that they are listed not in a Jolly Sorting manner.
    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.ArrayList;
    
    public class Jolly {
    
    	public static void main(String[] args){
    
    		// creating and populating the array
    		Integer[] sortArray = new Integer[20];
    		sortArray[0] = 1;
    		sortArray[1] = 17;
    		sortArray[2] = 18;
    		sortArray[3] = 3;
    		sortArray[4] = 8;
    		sortArray[5] = 6;
    		sortArray[6] = 11;
    		sortArray[7] = 20;
    		sortArray[8] = 19;
    		sortArray[9] = 2;
    		sortArray[10] = 4;
    		sortArray[11] = 5;
    		sortArray[12] = 12;
    		sortArray[13] = 16;
    		sortArray[14] = 7;
    		sortArray[15] = 10;
    		sortArray[16] = 14;
    		sortArray[17] = 13;
    		sortArray[18] = 9;
    		sortArray[19] = 15;
    		
    		// Printing array
    		for(int i = 0; i < sortArray.length; i++)
    		{
    			System.out.print(sortArray[i] + ", ");
    		}
    		System.out.println();
    		
    	}
    	public static ArrayList<Integer> JollySorting(Integer []sortArray) // METHOD to sort the array
    	{
    		Arrays.sort(sortArray);
    			Integer[] Sort1 = new Integer[10];
    			for (int i = 0; i < sortArray.length/2; i++)
    			{
    				Sort1[i] = sortArray[i];
    			}
    			Integer[] Sort2 = new Integer[10];
    			for (int a = 10; a < sortArray.length; a++)
    			{
    			
    		Sort2[a-10]= sortArray[a];
    			}
    		
    		Arrays.sort(Sort2, Collections.reverseOrder());
    		
    		ArrayList<Integer> sortedList = new ArrayList<Integer>();
    		
    		for (int a = 0; a < sortArray.length; a++)
    		{
    			sortedList.add(Sort1[a]);
    			sortedList.add(Sort2[a]);
    		}
    		
    		return sortedList;
    	}
    }

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    because you never called jolly sorting in the main method, the jolly sorting method is defined and ready, but remains unused. since your function returns a new array you want to declare a new array, and set it to jollysort(array), then print this new item

    Java Code:
    ArrayList<Integer> a = JollySorting(sortArray);
    
    print a;
    Last edited by sunde887; 01-13-2011 at 12:53 AM.

  9. #9
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    I get an outta bounds again
    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.ArrayList;
    
    public class Jolly {
    
    	public static void main(String[] args){
    
    		// creating and populating the array
    		Integer[] sortArray = new Integer[20];
    		sortArray[0] = 1;
    		sortArray[1] = 17;
    		sortArray[2] = 18;
    		sortArray[3] = 3;
    		sortArray[4] = 8;
    		sortArray[5] = 6;
    		sortArray[6] = 11;
    		sortArray[7] = 20;
    		sortArray[8] = 19;
    		sortArray[9] = 2;
    		sortArray[10] = 4;
    		sortArray[11] = 5;
    		sortArray[12] = 12;
    		sortArray[13] = 16;
    		sortArray[14] = 7;
    		sortArray[15] = 10;
    		sortArray[16] = 14;
    		sortArray[17] = 13;
    		sortArray[18] = 9;
    		sortArray[19] = 15;
    		
    		// Printing array
    		for(int i = 0; i < sortArray.length; i++)
    		{
    			System.out.print(sortArray[i] + ", ");
    		}
    		System.out.println(JollySorting(sortArray));
    		
    	}
    	public static ArrayList<Integer> JollySorting(Integer []sortArray) // METHOD to sort the array
    	{
    		Arrays.sort(sortArray);
    			Integer[] Sort1 = new Integer[10];
    			for (int i = 0; i < sortArray.length/2; i++)
    			{
    				Sort1[i] = sortArray[i];
    			}
    			Integer[] Sort2 = new Integer[10];
    			for (int a = 10; a < sortArray.length; a++)
    			{
    			
    		Sort2[a-10]= sortArray[a];
    			}
    		
    		Arrays.sort(Sort2, Collections.reverseOrder());
    		
    		ArrayList<Integer> sortedList = new ArrayList<Integer>();
    		
    		for (int a = 0; a < sortArray.length; a++)
    		{
    			sortedList.add(Sort1[a]);
    			sortedList.add(Sort2[a]);
    		}
    		
    		return sortedList;
    	}
    }
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Jolly.JollySorting(Jolly.java:61)
    at Jolly.main(Jolly.java:37)

  10. #10
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    System.out.println(JollySorting(sortArray)); line 37 <<<
    sortedList.add(Sort1[a]); line 61<<<<

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I see two errors, first things first:
    Java Code:
    System.out.println(JollySorting(sortArray));
    won't print an array, it will print the arrays memory location.
    Java Code:
    [I@732dacd1 [I@3bad086a54321
    so anytime you print arrays you want to use a loop.

    The second error
    Java Code:
    for (int a = 0; a < sortArray.length; a++)
    		{
    			sortedList.add(Sort1[a]);
    			sortedList.add(Sort2[a]);
    		}
    the length of sortArray is 20, the capacity of sort1 and sort2 is 10, so what happens when it tries adding Sort1[10...20] and sort2[10...20]?

Similar Threads

  1. Replies: 3
    Last Post: 07-28-2010, 03:49 PM
  2. Hello! and I need help. I dont know were to start
    By Fall0ut in forum New To Java
    Replies: 10
    Last Post: 05-19-2010, 07:26 PM
  3. Using libraries that others dont have(j3d)
    By scorpion9 in forum Advanced Java
    Replies: 7
    Last Post: 02-19-2010, 12:24 PM
  4. Sometimes get the right results sometimes dont
    By Battlefeldt in forum New To Java
    Replies: 0
    Last Post: 12-18-2009, 02:03 AM
  5. Replies: 6
    Last Post: 09-14-2009, 11:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •