I am trying to implement a toArray() method in my PriorityQueue. The PriorityQueue returns an array of Objects, but I want to do is have an array of the original type of objects which in this case is Job (which is marked by the 'T1'.
The following is the toArray() method from my PriorityQueue.java class.
cpn = current priority node

public Object[] toArray() {
		ArrayList<T1> al = new ArrayList<T1>(length);
		PriorityNode<T1,T2> cpn = head;
		for (int i = 0; i < length; i++) {
			al.add(cpn.getData());
			cpn = cpn.getNext();
		}
		return al.toArray();
	}

The class definition is:

public class PriorityQueue<T1, T2 extends Comparable> {

I'm trying to call that method from my getJobList() method in my ShortestJobNext class.
Pardon the comments from trying out different solutions.

public Job[] getJobList() {
		//Job[] jobs = new Job[queue.size()];
		//Object[] tmp = new Object[this.numberOfJobs];
		//tmp = this.queue.toArray();
		Job[] tmp = new Job[this.numberOfJobs];
		Job[] jobs = new Job[this.numberOfJobs];
		jobs = queue.toArray();
		//for (int i=0; i < this.numberOfJobs; i++) {
		//	jobs[i] = (Job) tmp[i];
		//}
		return jobs;
		//for (int i=0; i<queue.size(); i++) {
		//	jobs[i]=this.queue.get(i);
		//}
		//return jobs;
	}

I want to be able to return an array of Job with this method not of type Object. What I've tried so far has given me 'java.lang.ClassCastException'. I guess what's needed is to somehow convert from type Object to Job.
Anticipated thanks, Kev.

Attachments
package uk.ac.aber.rcs.cs211.adt;

public class PriorityNode<T1, T2 extends Comparable> implements Comparable<T2> {
	private T1 data;
	private T2 priority;
	private PriorityNode<T1, T2> next;
	public PriorityNode() {
		this.data = null;
		this.next = null;
		this.priority = null;
	}
	public PriorityNode(T1 data) {
		this.data = data;
		this.next = null;
		this.priority = null;
	}
	public PriorityNode(T1 data, T2 priority) {
		this.data = data;
		this.next = null;
		this.priority = priority;
	}
	public void setData(T1 data) {
		this.data = data;
	}
	public T1 getData() {
		return data;
	}
	public void setPriority(T2 priority) {
		this.priority = priority;
	}
	public T2 getPriority() {
		return priority;
	}
	/*
	 * setNext
	 */
	public void setNext(PriorityNode<T1, T2> next) {
		this.next = next;
	}
	/*
	 * getNext - returns the next PriorityNode.
	 * @return PriorityNode
	 */
	public PriorityNode<T1, T2> getNext() {
		return this.next;
	}
	/*
	 * compareTo
	 */
	public int compareTo(T2 otherPriority) {
		return this.getPriority().compareTo(otherPriority);
	}
}
package uk.ac.aber.rcs.cs211.adt;

import java.util.ArrayList;

public class PriorityQueue<T1, T2 extends Comparable> {
	private PriorityNode<T1, T2> head;
	private int length;
	/*
	 * Insert - into the correct position in the sorted linked list.
	 */
	public PriorityQueue() {
		head = new PriorityNode<T1, T2>();
		length = 0;
	}
	public void insert(T1 o, T2 priority) {
		// If the new element has higher priority than the first one
		// then it should take its place. 
		PriorityNode<T1,T2> npn = new PriorityNode<T1,T2>(o, priority);// New element.
		if(length == 0) { // Check for empty
			head = npn;
			length++;
			System.out.println("Tst 1");
		} else {
			PriorityNode<T1,T2> ppn = new PriorityNode<T1,T2>();// Previous element.
			PriorityNode<T1,T2> cpn = new PriorityNode<T1,T2>();// Current element.
			System.out.println("Tst 2");
			cpn = head; // Current element is first.
			for (int i=0; i < length; i++) {
				// Pg763 - at the bottom is a bit with brackets Sect Generic
				System.out.println("i ndex = " + i + 
						" " + cpn.getPriority() + " ");
				if (cpn.compareTo(npn.getPriority()) <= 0) {//If new node is gtr than the current
					System.out.println("INSERT");
					npn.setNext(cpn);// insert the new node
					length++;
					if (i != 0){ // If we're not at the beginning
						System.out.println("There is a previous Node");
						ppn.setNext(npn);// We point the previous' next pointer to the new node
						break; // We're done
					} else { // We're at the beginning
						System.out.println("No previous Node");
						head = npn;	// we don't have a previous node
						break; // We're done
					}
				} else if(i == length-1) {// else if we're at the end and we haven't insert then
					// we need to append the new node to the end.
					cpn.setNext(npn);
					length++;
					System.out.println("Append to end");
					break; //Probably unnecessary.
				}
				// Prepare for next loop
				ppn = cpn; // Previous node becomes the current
				cpn = cpn.getNext(); // current become the next node
			}
		}
	}
	/*
	 * Remove - the first element in the list.
	 * The proceeding PriorityNode then becomes the head.
	 */
	public void remove() {
		// Check whether length = 0 - throw exception if so
		if (length > 0) {
			this.head = this.head.getNext();
			this.length--;
		} //else throw something
		/* 
		if (length > 1) {
			head = head.getNext();
			length--;
		} else if (length == 1) {
			head = null;
		} else if (length <= 0)
			System.out.println("Remove failed - PQ is empty");
		// Need to throw an exception ^
		 */
	}
	public Object getHighestPriority() {
		return head;
	}
	public Object[] toArray() {
		ArrayList<T1> al = new ArrayList<T1>(length);
		PriorityNode<T1,T2> cpn = head;
		for (int i = 0; i < length; i++) {
			al.add(cpn.getData());
			cpn = cpn.getNext();
		}
		return al.toArray();
	}
	public int size() {
		return length;
	}
	public boolean isEmpty() {
		return length == 0;
	}
	public boolean contains(T1 o) {
		// TODO
		PriorityNode<T1,T2> cpn = new PriorityNode<T1,T2>();// Current element.
		cpn = this.head;
		for (int i=0; i<length; i++) {
			if (cpn.getData().equals(o))
				return true;
			cpn = cpn.getNext();
		}
		return false;
	}
	public String toString() {
		StringBuffer tempString = new StringBuffer();
		PriorityNode<T1,T2> cpn = this.head;
		for (int i = 0; i < 0; i++) {
			tempString.append("" + cpn.getData().toString() + "\n");
			cpn = cpn.getNext();
		}
		return tempString.toString();
	}
	public void clear() {
		this.head = new PriorityNode<T1, T2>();
	}
}
package uk.ac.aber.rcs.cs211.schedulersim.scheduler;

import uk.ac.aber.rcs.cs211.schedulersim.*;
import uk.ac.aber.rcs.cs211.adt.PriorityQueue;


/**
 * A Shortest Job Next algorithm
 * 
 * 
 * 
 * @author kevinroyprice rcs
 * @see uk.ac.aber.rcs.cs211.schedulersim.Simulator
 *
 */
public class ShortestJobNext implements Scheduler {

	private PriorityQueue<Job, Integer> queue;
	private int numberOfJobs;
	private Job lastJobReturned;

	public ShortestJobNext () {
		this.queue = new PriorityQueue<Job, Integer>();
		this.numberOfJobs = 0;
	}
	
	public void addNewJob(Job job) throws SchedulerException {
		// When adding a job to the queue, it needs to be inserted according to its job length.  
		if (this.queue.contains(job)) throw new SchedulerException("Job already on Queue");
		this.queue.insert(job, job.getLength());
		this.numberOfJobs++;
		/*
		if (queue.isEmpty()) {
			Integer jobLength = new Integer(job.getLength());
			queue.insert(job, jobLength);
			this.numberOfJobs++;
			return;
		}
		if (queue.size() == 1) {
			queue.add(job);
			this.numberOfJobs++;
		} else {
			for (int i = 1; i < this.numberOfJobs; i++) {
				if (queue.get(i).getLength() >= job.getLength()) {
					queue.add(i, job);
					this.numberOfJobs++;
					break;
				}
				else if (i >= this.numberOfJobs -1) {
					queue.add(job);
					this.numberOfJobs++;
					break;
				}
			}
		}
		*/
	}

	public Job getNextJob() throws SchedulerException {
		if (this.numberOfJobs<1) throw new SchedulerException("Empty Queue");
		this.lastJobReturned = (Job)this.queue.getHighestPriority(); // Returns the head of the queue.
		return this.lastJobReturned;
	}

	public void returnJob(Job job) throws SchedulerException {
		if (!this.queue.contains(job)) throw new SchedulerException("Job not on Queue");
		// Do nothing here as the job needs to continue to completion ???
	}

	public void removeJob(Job job) throws SchedulerException {
		if (!this.queue.contains(job)) throw new SchedulerException("Job not on Queue");
		this.queue.remove();
		this.numberOfJobs--;
	}

	public void reset() {
		this.queue.clear();
		this.numberOfJobs=0;
		this.lastJobReturned=null;
	}

	public Job[] getJobList() {
		//Job[] jobs = new Job[queue.size()];
		//Object[] tmp = new Object[this.numberOfJobs];
		//tmp = this.queue.toArray();
		Job[] tmp = new Job[this.numberOfJobs];
		Job[] jobs = new Job[this.numberOfJobs];
		jobs = (Job[]) queue.toArray(tmp);
		//for (int i=0; i < this.numberOfJobs; i++) {
		//	jobs[i] = (Job) tmp[i];
		//}
		return jobs;
		//for (int i=0; i<queue.size(); i++) {
		//	jobs[i]=this.queue.get(i);
		//}
		//return jobs;
	}

}

there is some missing class in you poste so I could not compile you code but please try this modification:

public Object[] toArray() {
		ArrayList<T1> al = new ArrayList<T1>(length);
		PriorityNode<T1,T2> cpn = head;
		for (int i = 0; i < length; i++) {
			al.add(cpn.getData());
			cpn = cpn.getNext();
		}
		return (Object[])  al.toArray(new Object [al.size()]);
	}

Hope it helps

What I ended up doing in the end was creating an array with the known number of elements (by getting the number of elements from my PriorityQueue by calling getLength) and then passing the array into my own toArray method in my PriorityQueue. This allowed me to keep the required type, in my case Job as opposed to type Object which was useless in my scenario.
I've since found out that this is generally the way it's done anyway. There's no such thing as original ideas!
Heres my solution:

public T1[] toArray(T1[] array) {
		PriorityNode<T1,T2> cpn = head;
		for (int i = 0; i < length; i++) {
			array[i] = cpn.getData();
			cpn = cpn.getNext();
		}
		return array;
	}

and the method which requires the array:

public Job[] getJobList() {
		Job[] tmp = new Job[this.numberOfJobs];
		Job[] jobs = new Job[this.numberOfJobs];
		jobs = queue.toArray(tmp);
		return jobs;
	}

Thanks "moutanna" for taking a look and trying to help out. Also sorry for taking so long with coming back with my solution or what I ended up doing.

This question has already been answered. Start a new discussion instead.