简体   繁体   中英

Stack overflow error

I have a class which is as below

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;
}

I am accessing this class in another class which is:

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);
}

When I run this I am getting stackoverflow error. Can some one help me on this?

Where is main method?

Change QueueSelection to

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());
    }

}

Update the code according to the comment.

queueCollection ==>

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;
}
}

QueueSelection ==>

Two methods are provided here, including initQueueCollection and printQueueCollection .

the first one is used to retrieve an initialized list and the second one is used to print names of element in List.

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());
    }
}
}

Main class for test==>

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());
    }

}
}

Output in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM