這裏是可能的解決方案。我試圖儘可能通用。 我做了一些假設.. i。我沒有設置數據庫。我通過構造函數填充Node類對象。在你的情況下,你通過數據庫行創建Node類對象。數據庫中的每一行對應於Node類的一個對象。
public class Node {
private String mediName;
private String dayOfWeek;
private String expiry;
private String manufacturer;
public Node(String mediName,String dayOfWeek,String expiry,String manufacturer)
{
this.mediName=mediName;
this.dayOfWeek=dayOfWeek;
this.expiry=expiry;
this.manufacturer=manufacturer;
}
public String getMediName() {
return mediName;
}
public void setMediName(String mediName) {
this.mediName = mediName;
}
public String getDayOfWeek() {
return dayOfWeek;
}
public void setDayOfWeek(String dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}
public String getExpiry() {
return expiry;
}
public void setExpiry(String expiry) {
this.expiry = expiry;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
}
以下類被設計成一個隊列數據結構進行操作。
public class QueueNode {
private int front=-1;
private int rear=-1;
private int capacity;
private Node[] arr;
public QueueNode(int capacity)
{
this.capacity=capacity;
arr=new Node[capacity];
}
private boolean isEmpty()
{
return front==-1;
}
private boolean isFull()
{
return (rear+1)%capacity==front;
}
public void push(Node data) throws Exception
{
if(isFull())
throw new Exception("Queue is Full");
else
{
rear=(rear+1)%capacity;
arr[rear]=data;
if(front==-1)
front=rear;
}
}
public Node pull() throws Exception
{
Node data=null;
if(isEmpty())
{
throw new Exception("Queue is Empty");
}else
{
data=arr[front];
if(front==rear)
front=rear-1;
else
{
front=(front+1)%capacity;
}
}
return data;
}
}
下面類的功能是測試節點和QueueNode類..
public class MainClassForQueue {
public static void main(String[] args) throws Exception {
Node n1=new Node("A","1000000","2017-02-16","ABC");
Node n2=new Node("B","1100000","2017-02-16","ABC");
Node n3=new Node("C","0100000","2017-02-16","ABC");
Node n4=new Node("D","0010000","2017-02-16","ABC");
Node n5=new Node("F","1010000","2017-02-16","ABC");
Node n6=new Node("G","0000001","2017-02-16","ABC");
Node n7=new Node("H","0000011","2017-02-16","ABC");
Node n8=new Node("I","0001000","2017-02-16","ABC");
Node n9=new Node("J","0000100","2017-02-16","ABC");
Node n10=new Node("K","0000010","2017-02-16","ABC");
Node n11=new Node("L","1010101","2017-02-16","ABC");
Node n12=new Node("M","1001011","2017-02-16","ABC");
Node n13=new Node("N","1111111","2017-02-16","ABC");
List<Node> listNode=new ArrayList<Node>();
listNode.add(n1);
listNode.add(n2);
listNode.add(n3);
listNode.add(n4);
listNode.add(n5);
listNode.add(n6);
listNode.add(n7);
listNode.add(n8);
listNode.add(n9);
listNode.add(n10);
listNode.add(n11);
listNode.add(n12);
listNode.add(n13);
int size=listNode.size();
QueueNode queue=new QueueNode(7*size);
for(int j=0;j<7;j++)
{
for (int i = 0; i < listNode.size(); i++) {
Node data=listNode.get(i);
String day=data.getDayOfWeek();
if(day.charAt(j)=='1')
queue.push(data);
}
}
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
System.out.println(queue.pull().getMediName());
}
}
爲什麼不能在Java的集合框架利用現有的隊列? – eatSleepCode
@eatSleepCode那是什麼,你在說什麼?我不知道「Java Collection Framework」是什麼。請你解釋一下嗎? –
我希望你可以通過day_of_week_to_take從db order中選擇行。如果不是如何獲取行? – kaushik