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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.