SRL Archive - Java Help and Tutorials

Casting Troubles, is this possible?


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:
/**
* 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.

Nava2

2010-02-06 11:40:01

So, I tried:
@SuppressWarnings("unchecked")
private T[] toObjectArray() {
Object[] result = new Object[length];
LinearNode<T> currNode = head;
for (int i = 0; i < length && currNode != null; i++) {
result[i] = currNode.getElement();
currNode = currNode.getNext();
}
return (T[])(result);
}

@SuppressWarnings("unchecked")
private T[] toStringArray() {
String[] result = new String[length];
LinearNode<T> currNode = head;
for (int i = 0; i < length && currNode != null; i++) {
result[i] = (String)(currNode.getElement());
currNode = currNode.getNext();
}
return (T[])(result);
}

/**
* Converts the Linked Array to a regular array.
* @return The regular array.
*/

public T[] toArray() {
Object temp = (T)(new Object());
if (temp instanceof String) {
return toStringArray();
} else {
return toObjectArray();
}
}

But instanceof doesn't like references to 'T'.

Wizzup?

2010-02-06 12:27:12

I have no time to look at what exactly you are doing, but let me tell you that you should always be able to get rid of those warnings, so don't suppress them.
Why not cast it to an Object? (Even though it kind of ruins the point of generics)

Jahooma

2010-02-10 16:29:09

I took java last year. Here's my guess at how to do it:

public T[] toArray() {
T[] result = new T[length];
LinearNode<T> currNode = head;
for (int i = 0; i < length && currNode != null; i++) {
result[i] = currNode.getElement();
currNode = currNode.getNext();
}
return result;
}
While you can cast an Object into a different type, I don't think you can cast an array of Objects into an array of a different type.

Good luck!

arash

2010-02-11 02:22:27

What are you doing?, Comments were invented so people like me who are not inside your head know what you are doing and how to help.

From: Nava2
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:
/**
* 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.

Nava2

2010-02-11 14:53:12

I explained it, and I used the <E> E[] toArray(E[] a) method from the OpenJDK List.java