【发布时间】:2009-03-10 02:34:28
【问题描述】:
package abc;
class DependencyDataCollection
{
private int sNo;
private String sessionID;
private int noOfDependency;
private int noOfRejection;
private int totalValue;
/** Creates a new instance of DependencyDataCollection */
public DependencyDataCollection(int sNo, String sessionID, int noOfDependency, int noOfRejection, int totalValue)
{
this.sNo = sNo;
this.sessionID = sessionID;
this.noOfDependency = noOfDependency;
this.noOfRejection = noOfRejection;
this.totalValue = totalValue;
}
public int getSNo()
{
return sNo;
}
public String getSessionID()
{
return sessionID;
}
public int getNoOfDependency()
{
return noOfDependency;
}
public int getNoOfRejection()
{
return noOfRejection;
}
public int getTotalValue()
{
return totalValue;
}
}
public class DependencyStack {
LinkedList lList;
/** Creates a new instance of DependencyStack */
public DependencyStack()
{
lList = new LinkedList();
}
public void add(int sNo, String sessionID, int noOfDependency, int noOfRejection, int totalValue)
{
lList.add(new DependencyDataCollection(sNo,sessionID,noOfDependency,noOfRejection,totalValue));
}
public int size()
{
return lList.size();
}
public void show()
{
for(int i=0;i<lList.size();i++)
{
DependencyDataCollection ddc = (DependencyDataCollection)lList.get(i);
System.out.println(ddc.getSNo()+" "+ddc.getSessionID()+" "+ddc.getNoOfDependency()+" "+ddc.getNoOfRejection()+" "+ddc.getTotalValue());
}
}
public int returnIndexOfSession(String sessionID)
{
DependencyDataCollection ddc = null;
for(int i=0;i<lList.size();i++)
{
ddc = (DependencyDataCollection)lList.get(i);
if(ddc.getSessionID().equals(sessionID))
break;
}
return ddc.getSNo();
}
public static void main(String args[])
{
DependencyStack ds = new DependencyStack();
ds.add(1,"a",0,0,0);
ds.add(2,"b",0,0,0);
ds.show();
//System.out.println(ds.returnIndexOfSession("a"));
// DependencyDataCollection ddc = new DependencyDataCollection(1,"a",0,0,0);
// System.out.println(ds.indexOf(ddc));
}
}
这是一个简单的 java 链表程序,它使用 java.util 包中的内置链表类。链表用于存储不同数量的数据,使用 DependencyDataCollection 类..
现在我的问题是
1) 请评估这个程序, 我尊重所有的 java 私人成员等概念 访问,我已经完成了,等等。
2) 我在寻找 特定会话的索引。
例如节点 1 包含 1,"a",0,0,0......节点 2 包含 2,"b",0,0,0
现在我想找到 indexOf 包含其中之一的节点 数据为“b”或“a”。什么可能是最短的内置方法可以做到这一点,因为我制作了一个名为“public int returnIndexOfSession(String sessionID)”的函数,它使用了for循环,我发现这非常耗时..还有其他出路吗? .
由于我是java新手,请评估和指导。
【问题讨论】:
标签: java