-1

我有一堂课如下

public class queueCollection implements java.io.Serializable{
private String QueueName1;
private String QueueName2;
private Boolean Avail;
public queueCollection(String queueName1, String queueName2, Boolean avail){
this.QueueName1=queueName1;
this.QueueName2=queueName2;
this.Avail=avail;
}

我在另一个类中访问这个类,它是:

public class QueueSelection{
queueCollection d1 = new queueCollection("QUEUE_A1", "QUEUE_A2", false);
queueCollection d2 = new queueCollection("QUEUE_B1", "QUEUE_B2", false);
queueCollection d3 = new queueCollection("QUEUE_C1", "QUEUE_C1", true);
queueCollection d4 = new queueCollection("QUEUE_D1", "QUEUE_D2", true);
List<queueCollection> list= new ArrayList<queueCollection>();
list.add(d1);
list.add(d2);
list.add(d3);
list.add(d4);
}

当我运行它时,我收到了 stackoverflow 错误。有人可以帮助我吗?

4

2 回答 2

2

主要方法在哪里?

更改QueueSelection

public class QueueSelection {

    public static void main(String[] args) {
        queueCollection d1 = new queueCollection("QUEUE_A1", "QUEUE_A2", false);
        queueCollection d2 = new queueCollection("QUEUE_B1", "QUEUE_B2", false);
        queueCollection d3 = new queueCollection("QUEUE_C1", "QUEUE_C1", true);
        queueCollection d4 = new queueCollection("QUEUE_D1", "QUEUE_D2", true);
        List<queueCollection> list = new ArrayList<queueCollection>();
        list.add(d1);
        list.add(d2);
        list.add(d3);
        list.add(d4);
        System.out.println(list.toString());
    }

}
于 2013-11-15T04:58:16.993 回答
1

根据注释更新代码。

队列集合 ==>

public class queueCollection implements java.io.Serializable {
private String QueueName1;

public String getQueueName1() {
    return QueueName1;
}

public void setQueueName1(String queueName1) {
    QueueName1 = queueName1;
}

public String getQueueName2() {
    return QueueName2;
}

public void setQueueName2(String queueName2) {
    QueueName2 = queueName2;
}

public Boolean getAvail() {
    return Avail;
}

public void setAvail(Boolean avail) {
    Avail = avail;
}

private String QueueName2;
private Boolean Avail;

public queueCollection(String queueName1, String queueName2, Boolean avail) {
    this.QueueName1 = queueName1;
    this.QueueName2 = queueName2;
    this.Avail = avail;
}
}

队列选择==>

这里提供了两种方法,包括initQueueCollectionprintQueueCollection

第一个用于检索初始化列表,第二个用于打印列表中元素的名称。

import java.util.ArrayList;
import java.util.List;

public class QueueSelection {

public List<queueCollection> initQueueCollection()
{
    queueCollection d1 = new queueCollection("QUEUE_A1", "QUEUE_A2", false);
    queueCollection d2 = new queueCollection("QUEUE_B1", "QUEUE_B2", false);
    queueCollection d3 = new queueCollection("QUEUE_C1", "QUEUE_C1", true);
    queueCollection d4 = new queueCollection("QUEUE_D1", "QUEUE_D2", true);
    List<queueCollection> list = new ArrayList<queueCollection>();
    list.add(d1);
    list.add(d2);
    list.add(d3);
    list.add(d4);

    return list;
}

public void printQueueCollection(List<queueCollection> queueList)
{
    for(queueCollection queue : queueList)
    {
        System.out.printf("QueueName1=[%s], QueueName2=[%s] \n",queue.getQueueName1(),queue.getQueueName2());
    }
}
}

测试的主类==>

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

public static void main(String[] args) {
    QueueSelection selection = new QueueSelection();
    // Get queueCollection list
    List<queueCollection> queueList = selection.initQueueCollection();

    // Print List
    selection.printQueueCollection(queueList);

    /*
     * Map example
     */
    Map<String, List<queueCollection>> map = new HashMap<String, List<queueCollection>>();
    // Put entire collection into this map
    map.put("QUEUE", queueList);

    /*
     * Iterate the Map to find a collection with name 'QUEUE_A1 '
     */

    queueCollection particularCollection = null;

    for (queueCollection collection : map.get("QUEUE")) {
        if ("QUEUE_A1".equals(collection.getQueueName1())
                && "QUEUE_A2".equals(collection.getQueueName2())) {
            particularCollection = collection;
        }
    }

    // /Print Names using Particular Collection, which is found above.

    if (null == particularCollection) {
        System.out.println("No Particular collection is found!");
    } else {
        System.out.println("\n Particular collection is found from entire collection, its value is as follows:!");
        System.out.printf("[Name1]=>%s , [Name2]=>%s",
                particularCollection.getQueueName1(),
                particularCollection.getQueueName2());
    }

}
}

在 Cnsole 中输出==>

QueueName1=[QUEUE_A1], QueueName2=[QUEUE_A2] 
QueueName1=[QUEUE_B1], QueueName2=[QUEUE_B2] 
QueueName1=[QUEUE_C1], QueueName2=[QUEUE_C1] 
QueueName1=[QUEUE_D1], QueueName2=[QUEUE_D2] 

Particular collection is found from entire collection, its value is as follows:!
[Name1]=>QUEUE_A1 , [Name2]=>QUEUE_A2
于 2013-11-15T04:59:27.190 回答