Nava2
2010-02-05 15:38:06
So, I want to convert my LinkedArray object (shown below), back into a regular array object. This is what I have tried to do, but it won't let me because of casting issues:
This is the entire class:
Any help is appreciated! :)
Also, if anyone has side comments, feel free to make them.
/**
* Converts the Linked Array to a regular array.
* @return The regular array.
*/
@SuppressWarnings("unchecked")
public T[] toArray() {
// sort through to the index needed.
T[] result = (T[])(new Object[length]);
LinearNode<T> currNode = head;
Object[] temp = new Object[length];
for (int i = 0; i < length && currNode != null; i++) {
temp[i] = currNode.getElement();
currNode = currNode.getNext();
}
System.arraycopy(temp, 0, result, 0, length);
return (result);
}This is the entire class:
/**
* Gives a Dynamic Memory allocation aspect to arrays.
* @author **************
*/
public class LinkedArray<T> {
/**
* attributes
*/
// holds reference to the first index of the array
private LinearNode<T> head;
// reference to the length of the array
public static int length;
/**
* Constructor which creates an array of Length 0.
*/
public LinkedArray() {
head = null;
length = 0;
}
/**
* Add a new element into the LinkedArray.
* @param elem The element to add to the array.
*/
public void add(T elem) {
// if the head of the array isn't set yet, set it.
if (head == null) {
head = new LinearNode<T>(elem);
length++;
return;
}
// loop through and get the last reference
LinearNode<T> currNode = head;
while (currNode.getNext() != null) {
currNode = currNode.getNext();
}
// add the new node
LinearNode<T> newNode = new LinearNode<T>(elem);
currNode.setNext(newNode);
length++;
}
/**
* Method to remove an element from the array.
* @param index Location of the element to remove.
* @return T element which is removed.
*/
public T remove(int index) {
if ((index < 0) || (index > length)) {
throw new ArrayIndexOutOfBoundsException(index);
}
LinearNode<T> currNode = head;
LinearNode<T> prevNode = null;
// Special case where the head is being removed.
if (index == 0) {
currNode = head;
head = head.getNext();
length--;
return (currNode.getElement());
}
// move through the array until get to the index, or there is an issue.
for (int i = 0; i != index && currNode != null; i++) {
prevNode = currNode;
currNode = currNode.getNext();
}
if (index == (length - 1)) {
// if removing the last index, set it to null rather than to another index
prevNode.setNext(null);
} else {
//set the previous to 1+its index, effectively removing the index.
prevNode.setNext(currNode.getNext());
}
length--;
return (currNode.getElement());
}
/**
* Returns the element held at index
* @param index Index of object returned
* @return returns the object held at index
*/
public T getElement(int index) {
// get the node thats needed, use the other method.
LinearNode<T> currNode = getNode(index);
return (currNode.getElement());
}
/**
* Returns the LinearNode object at an index.
* @param index Location of Node in array.
* @return LinearNode of specified index.
*/
public LinearNode<T> getNode(int index) {
if ((index < 0) || (index > length)) {
throw new ArrayIndexOutOfBoundsException(index);
}
// sort through to the index needed.
LinearNode<T> currNode = head;
for (int i = 0; i != index && currNode != null; i++) {
currNode = currNode.getNext();
}
if (currNode == null)
throw new NullPointerException("getNode found a null index.");
return (currNode);
}
/**
* Method to get the first LinearNode.
* @return First LinearNode in the array.
*/
public LinearNode<T> getHead() {
return (head);
}
/**
* Converts the Linked Array to a regular array.
* @return The regular array.
*/
@SuppressWarnings("unchecked")
public T[] toArray() {
// sort through to the index needed.
T[] result = (T[])(new Object[length]);
LinearNode<T> currNode = head;
Object[] temp = new Object[length];
for (int i = 0; i < length && currNode != null; i++) {
temp[i] = currNode.getElement();
currNode = currNode.getNext();
}
System.arraycopy(temp, 0, result, 0, length);
return (result);
}
/**
* toString Method outputs the Linked array object in an informative
* format.
* @return String information about the Object.
*/
public String toString()
{
String result = "";
LinearNode<T> current = head;
while (current != null) {
result = result + "[" + current.getElement() + "],";
current = current.getNext();
}
return "LinkedArray Length: " + length + " Contains: \n" + result;
}
}
Any help is appreciated! :)
Also, if anyone has side comments, feel free to make them.