Results 1 to 7 of 7
  1. #1
    canyonist is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default Ordered Array plz help....due tomorrow!!

    Hi guys,

    in a bit of a drama with an ordered arraylist im trying to create. keep getting null error for element[0] in else statement. It wont translate from the if statement. What am i doing wrong? I have 'elements' and 'orde'r declared as private instance variables

    Java Code:
    	public boolean add(Object element, double priority)
    	{
    
    		if (length == 0)
    		{
    			Object[] elements = new Object[1];
    			double[] order = new double[1];
    			elements[0] = element;
    			order[0] = priority;
    			length++;
    			return true;
    		}
    		
    		else
    		{
    			Object[] tempelements = new Object[length];
    			double[] temporder = new double[length];
    			for (int i = 0; i < length; i++)
    			
    			{
    				tempelements[i] = elements[i]; //problem here...keep getting null
    				temporder[i] = order[i];
    				
    			}
    			
    			for (int k = 0; k < length; k++)
    			{
    				
    				if (priority != temporder[k])
    				{
    					Object[] elements = new Object[length + 1];
    					double[] order = new double[length + 1];
    					if (priority > temporder[k] && priority < temporder[k + 1])
    					{
    						elements[k] = element;
    						order[k] = priority;
    					}
    					else if (priority > temporder[k])
    					{
    						elements[k] = tempelements[k + 1];
    						order[k] = temporder[k + 1];
    					}
    					else
    					{
    						elements[k] = tempelements[k];
    						order[k] = temporder[k];
    					}
    				}
    				else
    				{
    					return false;
    				}
    			}
    			length++;
    			return true;
    		}
    		
    	}

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by canyonist View Post
    I have 'elements' and 'orde'r declared as private instance variables

    but in your code these variables are declared as local variables. and i can't figure out what your code should compute. can you explain.

  3. #3
    canyonist is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    ok well basically when an element is added to the sortedarraylist and its empty(new), it loops through the if statement and adds it (which it does) but for every addition after that, it should go through the else and add the object to the array after the first element. But for some reason it doesnt remember the elements array and returns null. I'm probably doing something fundamentally wrong as im pretty new to java...I appreciate your patience!!

    heres the full code:

    Java Code:
    /**
     * This class implements a sorted version of the ArrayList class,
     * where each element is sorted according to their priority.
     * Elements in the SortedArrayList cannot have the same priority.
     *
     * @author: ### WRITE YOUR FULL NAME HERE
     */
    class SortedArrayList
    {
    
    // Instance Variables
    
    	// All the elements that are stored in the SortedArrayList,
    	// sorted according to each element's priority
    	private Object[] elements;
    	private Object[] tempelements;
    	private double[] order;
    	private double[] temporder;
    	// ### YOU MAY NEED TO DECLARE MORE INSTANCE VARIABLES HERE
    
    
    // Static Variables
    
    	private int length;
    
    
    // Constructor
    
    	/**
    	 * Creates a new and empty SortedArrayList
    	 */
    	public SortedArrayList()
    	{
    		length = 0;
    		Object[] elements = new Object[0];
    		double[] order = new double[0];
    	}
    
    
    // Instance Methods
    
    	/**
    	 * Causes the SortedArrayList to become empty
    	 */
    	public void clear()
    	{
    
    	}
    
    
    	/**
    	 * Returns true if the SortedArrayList has no elements;
    	 * else returns false
    	 */
    	public boolean isEmpty()
    	{
    		if (length == 0)
    		{
    			return true;
    		}
    		return false;
    	}
    
    
    	/**
    	 * Returns the number of elements in the SortedArrayList
    	 */
    	public int size()
    	{
    		return length;
    	}
    
    
    	/**
    	 * Adds the given element with the given priority into the SortedArrayList;
    	 * Returns true if the element was successfully added into the SortedArrayList;
    	 * Returns false if the element was unable to be added into the SortedArrayList
    	 * (because there already exists an element with the given priority)
    	 */
    	public boolean add(Object element, double priority)
    	{
    
    		if (length == 0)
    		{
    			Object[] elements = new Object[1];
    			double[] order = new double[1];
    			elements[0] = element;
    			order[0] = priority;
    			System.out.println(order[0]);
    			length++;
    			return true;
    		}
    		
    		else
    		{
    			Object[] tempelements = new Object[length];
    			double[] temporder = new double[length];
    			for (int i = 0; i < length; i++)
    			
    			{
    				System.out.println(order[0]);
    				tempelements[i] = elements[i];   //elements[i] returns null even after its looped through for and assigned 'element'
    				temporder[i] = order[i];
    				
    			}
    			
    			for (int k = 0; k < length; k++)
    			{
    				
    				if (priority != temporder[k])
    				{
    					Object[] elements = new Object[length + 1];
    					double[] order = new double[length + 1];
    					if (priority > temporder[k] && priority < temporder[k + 1])
    					{
    						elements[k] = element;
    						order[k] = priority;
    					}
    					else if (priority > temporder[k])
    					{
    						elements[k] = tempelements[k + 1];
    						order[k] = temporder[k + 1];
    					}
    					else
    					{
    						elements[k] = tempelements[k];
    						order[k] = temporder[k];
    					}
    				}
    				else
    				{
    					return false;
    				}
    			}
    			length++;
    			return true;
    		}
    		
    	}
    
    
    	/**
    	 * Returns the element that is stored in the given index
    	 * without removing the element from the SortedArrayList
    	 */
    	public Object get(int index)
    	{
    		System.out.println(length);
    		return elements[index];
    	}
    
    
    	/**
    	 * Returns the priority of the element that is stored in the given index
    	 * without removing the element from the SortedArrayList
    	 */
    	public double getPriority(int index)
    	{
    		return order[index];
    	}
    
    
    	/**
    	 * Returns and removes the element that is stored in the given index;
    	 * all other elements with a higher index will be shifted down so
    	 * that all the elements are at the front part of the SortedArrayList
    	 * that is, there will be no holes in the array
    	 */
    	public Object remove(int index)
    	{
    		Object[] tempelements = new Object[length];
    		double[] temporder = new double[length];
    		Object[] elements = new Object[length + 1];
    		double[] order = new double[length + 1];
    		
    		length = length - 1;
    		for (int i = 0; i < index; i++)
    		{
    			elements[i] = tempelements[i];
    			order[i] = temporder[i];
    		}
    		
    		for (int i = index; i <= length-1; i++)
    		{
    			elements[i] = tempelements[i+1];
    			order[i] = temporder[i+1];
    		}
    		return elements[index];
    	}
    
    
    	/**
    	 * Returns and removes the given element;
    	 * all other elements with a higher index will be shifted down so
    	 * that all the elements are at the front part of the SortedArrayList
    	 * that is, there will be no holes in the array;
    	 * if the element does not exist, then null should be returned
    	 */
    	public Object remove(Object element)
    	{
    		// ### REPLACE THE NEXT LINE, AND WRITE YOUR CODE HERE
    		return null;
    	}
    
    
    	/**
    	 * Returns the element with the highest priority
    	 * without removing the element from the SortedArrayList
    	 */
    	public Object getHighestPriorityElement()
    	{
    		return elements[1];
    	}
    
    
    	/**
    	 * Returns and removes the element witht he highest priority;
    	 * all other elements with a higher index will be shifted down so
    	 * that all the elements are at the front part of the SortedArrayList
    	 * that is, there will be no holes in the array
    	 */
    	public Object removeHighestPriorityElement()
    	{
    		// ### REPLACE THE NEXT LINE, AND WRITE YOUR CODE HERE
    		return null;
    	}
    
    
    	/**
    	 * Returns the priority value of the element with the highest priority
    	 */
    	public double getHighestPriority()
    	{
    		// ### REPLACE THE NEXT LINE, AND WRITE YOUR CODE HERE
    		return 0;
    	}
    
    
    	/**
    	 * Returns true if the given element is stored in the SortedArrayList;
    	 * else returns false
    	 */
    	public boolean contains(Object element)
    	{
    		// ### REPLACE THE NEXT LINE, AND WRITE YOUR CODE HERE
    		return false;
    	}
    
    
    	/**
    	 * Changes the priority of the given element to the given newPriority,
    	 * and rearranges the order of the elements so that the SortedArrayList
    	 * is still sorted;
    	 * Returns true if the operation is successful (the element exists in
    	 * the SortedArrayList);
    	 * Returns false if the operation is unsuccessful, in which case no change
    	 * is made to the SortedArrayList (e.g. the element does not exist in the
    	 * SortedArrayList, or the new priority already exists in the SortedArrayList)
    	 */
    	public boolean changePriority(Object element, double newPriority)
    	{
    		// ### REPLACE THE NEXT LINE, AND WRITE YOUR CODE HERE
    		return false;
    	}
    }

  4. #4
    canyonist is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    please anyone...really need a hand

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by canyonist View Post
    please anyone...really need a hand
    You're having a member variable 'elements' (and a few others) but your big method has a local variable 'elements' (and also a few others). The local variable(s) with the same name as your member variable(s) 'shadow' the member variables, i.e. the local variables are used, not the member variables, i.e. they stay untouched.

    The first step to a cure is not define local variables but make your method work on your member variables. Step two is not to use arrays at all, use ArrayLists instead. Step three is to use a SortedSet that uses a class that is Comparable on the priority member.

    kind regards,

    Jos

  6. #6
    canyonist is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    JosAH you are an amazing man and i wish blessings upon your children...trust me i wouldnt be using arrays if i didnt have to!!

    thanks mate!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by canyonist View Post
    JosAH you are an amazing man and i wish blessings upon your children...trust me i wouldnt be using arrays if i didnt have to!!

    thanks mate!
    You're welcome of course and thanks for the compliment; b.t.w. my wife only calls me 'amazing' when I clumsily break or ruin something again (on an almost daily basis ;-)

    kind regards,

    Jos

Similar Threads

  1. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  2. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  3. Replies: 10
    Last Post: 02-11-2009, 07:38 AM
  4. Need help~~~ Midterm tomorrow!!!
    By str4kt in forum New To Java
    Replies: 23
    Last Post: 10-10-2008, 09:12 AM
  5. viewing records ordered by date
    By nitish.dhar in forum New To Java
    Replies: 3
    Last Post: 07-31-2008, 07:14 AM

Posting Permissions

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