我有一个类如下
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错误。有人可以帮我这个吗?
答案 0 :(得分: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());
}
}
答案 1 :(得分:1)
根据评论更新代码。
queueCollection ==&gt;
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 ==&gt;
此处提供了两种方法,包括initQueueCollection
和printQueueCollection
。
第一个用于检索初始化列表,第二个用于打印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());
}
}
}
测试主要类==&gt;
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 ==&gt;
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