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

• 05-23-2010, 07:23 AM
canyonist
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

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
j2me64
Quote:

Originally Posted by canyonist
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.
• 05-23-2010, 08:02 AM
canyonist
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:

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
canyonist
• 05-23-2010, 08:58 AM
JosAH
Quote:

Originally Posted by canyonist

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
canyonist
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
JosAH
Quote:

Originally Posted by canyonist
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