Results 1 to 7 of 7
- 05-23-2010, 07:23 AM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
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; } }
- 05-23-2010, 07:51 AM #2
- 05-23-2010, 08:02 AM #3
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
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; } }
- 05-23-2010, 08:28 AM #4
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
please anyone...really need a hand
- 05-23-2010, 08:58 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 05-23-2010, 09:35 AM #6
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
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!
- 05-23-2010, 09:57 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
PLEASE HELP! i need some code by tomorrow for my exam :-(
By madouva in forum New To JavaReplies: 10Last Post: 02-11-2009, 07:38 AM -
Need help~~~ Midterm tomorrow!!!
By str4kt in forum New To JavaReplies: 23Last Post: 10-10-2008, 09:12 AM -
viewing records ordered by date
By nitish.dhar in forum New To JavaReplies: 3Last Post: 07-31-2008, 07:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks