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.