Results 1 to 6 of 6

Thread: Casting Troubles, is this possible?

  1. #1
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default Casting Troubles, is this possible?

    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:
    java Code:
    /**
         * 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:

    java Code:
    /**
     * 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.
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  2. #2
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    So, I tried:
    java Code:
    @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'.
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  3. #3
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    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)



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  4. #4
    Join Date
    Sep 2007
    Posts
    46
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I took java last year. Here's my guess at how to do it:
    Code:
        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!

  5. #5
    Join Date
    Aug 2006
    Posts
    74
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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.

    Quote Originally Posted by Nava2 View Post
    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:
    java Code:
    /**
         * 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:

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

  6. #6
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    I explained it, and I used the <E> E[] toArray(E[] a) method from the OpenJDK List.java
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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