i have problem in code , need me create api of randomizedqueue array , when call method
public static <item> void enqueue(item item) { if(item == null)throw new nullpointerexception(); s[n++] = item; }
in client class return error , think problem @ array how create static <item> item[]
(generic type).
you cannot declare static generic fields in class. not when class generic, see declaring static generic variables in generic class.
write generic class randomizedqueue
, , instantiate (create objects):
public class randomizedqueue<item> { private item[] items = new item[constant]; private int count = 0; public void enqueue(item item) { if(item == null) throw new illegalargumentexception("item mustn't null"); if(count == constant) throw new illegalstateexception("queue full"); items[count++] = item; } public item dequeue() { if(count == 0) throw new illegalstateexception("queue empty"); int index = (int)(math.random() * count); item item = items[index]; items[index] = items[--count]; return item; } public int getcount() { return count; } }
Comments
Post a Comment