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中的一个简单的Linked List程序,它使用java.util包中的内置链表列表。链表用于使用DependencyDataCollection Class ..
存储不同数量的数据现在我的问题是
1)请评估这个程序, 和temme我尊重所有java 像私人成员的概念 访问,我已经完成,等等。
2)我遇到问题找到了 indexOf特定会话。
例如节点1包含 1,“a”,0,0,0 ...............节点2 包含2,“b”,0,0,0
现在我想找到indexOf了 包含其中一个的节点 数据为“b”或“a”。什么可能是最短的内置方法可以这样做,因为我已经创建了一个名为“public int returnIndexOfSession(String sessionID)”的函数,它使用for循环,我发现这非常耗费时间。还有其他出路。 。
请评估和指导,因为我是java的新手。
答案 0 :(得分:2)
以下是我要做的更改,理由是在评论中。
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
class DependencyDataCollection
{
// makte them fnal, then you hava an immutible object and your code is much safer.
// in your case you had noset methods so it was safe, but always try to make things final.
private final int sNo;
private final String sessionID;
private final int noOfDependency;
private final int noOfRejection;
private final int totalValue;
public DependencyDataCollection(final int sNo,
final String sessionID,
final int noOfDependency,
final int noOfRejection,
final 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;
}
}
class DependencyStack
{
// change the type to be as generic as poosible - List interface
// added generics so you get compile time safety and don't use casts later on
// renamed it to something meaningful
private final List<DependencyDataCollection> dependencies;
// use an ArrayList instead of a LinkedList, it'll be faster since you are not inserting/deleting
// into the middle of the list
{
dependencies = new ArrayList<DependencyDataCollection>();
}
// your Stack shouldn't know how to make the collections... (in my opinion)
public void add(final DependencyDataCollection ddc)
{
dependencies.add(ddc);
}
public int size()
{
return dependencies.size();
}
// the next 3 methods are just convenience since you don't know if someione
// will want to write to a file or a writer instead of a stream
public void show()
{
show(System.out);
}
public void show(final OutputStream out)
{
show(new OutputStreamWriter(out));
}
public void show(final Writer writer)
{
show(new PrintWriter(writer));
}
public void show(final PrintWriter writer)
{
// use the new for-each instead of the old style for loop
// this also uses an iterator which is faster than calling get
// (well on an ArrayList it probably is about the same, but a LinkedList it'll be faster)
for(final DependencyDataCollection ddc : dependencies)
{
writer.println(ddc.getSNo() + " " +
ddc.getSessionID() + " " +
ddc.getNoOfDependency() + " " +
ddc.getNoOfRejection() + " " +
ddc.getTotalValue());
}
}
public int returnIndexOfSession(final String sessionID)
{
DependencyDataCollection foundDDC;
final int retVal;
foundDDC = null;
for(final DependencyDataCollection ddc : dependencies)
{
if(ddc.getSessionID().equals(sessionID))
{
foundDDC = ddc;
break;
}
}
// deal with the fact that you might have not found the item and it would be null.
// this assumes -1 is an invalid session id
if(foundDDC == null)
{
retVal = -1;
}
else
{
retVal = foundDDC.getSNo();
}
return (retVal);
}
public static void main(final String[] args)
{
DependencyStack ds = new DependencyStack();
ds.add(new DependencyDataCollection(1,"a",0,0,0));
ds.add(new DependencyDataCollection(1,"a",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));
}
}
编辑:
这个会加快查找(和删除)。
class DependencyStack
{
// A Map provides quick lookup
private final Map<String, DependencyDataCollection> dependencies;
// a LinkedHashMap allows for quick lookup, but iterates in the order they were added... if that matters for show.
{
dependencies = new LinkedHashMap<String, DependencyDataCollection>();
}
// your Stack shouldn't know how to make the collections... (in my opinion)
public void add(final DependencyDataCollection ddc)
{
if(ddc == null)
{
throw new IllegalArgumentException("ddc cannot be null");
}
dependencies.put(ddc.getSessionID(), ddc);
}
public int size()
{
return dependencies.size();
}
// the next 3 methods are just convenience since you don't know if someione
// will want to write to a file or a writer instead of a stream
public void show()
{
show(System.out);
}
public void show(final OutputStream out)
{
show(new OutputStreamWriter(out));
}
public void show(final Writer writer)
{
show(new PrintWriter(writer));
}
public void show(final PrintWriter writer)
{
// use the new for-each instead of the old style for loop
// this also uses an iterator which is faster than calling get
// (well on an ArrayList it probably is about the same, but a LinkedList it'll be faster)
for(final DependencyDataCollection ddc : dependencies.values())
{
writer.println(ddc.getSNo() + " " +
ddc.getSessionID() + " " +
ddc.getNoOfDependency() + " " +
ddc.getNoOfRejection() + " " +
ddc.getTotalValue());
}
}
public int returnIndexOfSession(final String sessionID)
{
final DependencyDataCollection ddc;
final int retVal;
if(sessionID == null)
{
throw new IllegalArgumentException("sessionID cannot be null");
}
// get it if it exists, this is much faster then looping through a list
ddc = dependencies.get(sessionID);
// deal with the fact that you might have not found the item and it would be null.
// this assumes -1 is an invalid session id
if(ddc == null)
{
retVal = -1;
}
else
{
retVal = ddc.getSNo();
}
return (retVal);
}
public static void main(final String[] args)
{
DependencyStack ds = new DependencyStack();
ds.add(new DependencyDataCollection(1,"a",0,0,0));
ds.add(new DependencyDataCollection(1,"a",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));
}
}
答案 1 :(得分:1)
最突出的一点是缺少javadoc风格的评论 (来自http://en.wikipedia.org/wiki/Javadoc)
/** * Validates a chess move. Use {@link #doMove(int, int, int, int)} to move a piece. * * @param theFromFile file from which a piece is being moved * @param theFromRank rank from which a piece is being moved * @param theToFile file to which a piece is being moved * @param theToRank rank to which a piece is being moved * @return true if the chess move is valid, otherwise false */
答案 2 :(得分:1)
您想使用泛型:
List<DependencyDataCollection> lList;
此外,在您的变量定义中,您应该使用接口List而不是具体类型(LinkedList)。
要使indexOf起作用,你的元素类型(DependencyDataCollection)需要实现比较器的相等性:
class DependencyDataCollection{
@Override
public boolean equals(Object o){
...
}
}
然后,您可以使用List接口提供的内置indexOf()。但它会执行与您现在相同的循环。如果那太耗时(真的吗?)那么你需要一个哈希支持的列表或其他东西(在这种情况下你还需要实现hashCode())。
更新:它将执行相同类型的循环,但比现在更有效。不要通过索引访问链接列表它不是为此构建的,使用foreach循环或迭代器(或ArrayList):
for(DependencyDataCollection d: iList){
... }
答案 3 :(得分:1)
你有一个良好的开端。你可以做的改进:
泛型。您的链接列表可能是
LinkedList&lt; DependencyDataCollection&gt; LLIST; 这样可以进行类型检查。
您在此处看到您的链接列表不便于搜索 - 您必须检查每个值,这是缓慢而笨拙的。这就是人们使用哈希映射的原因。
查找构建器模式以找到构建器args长列表的方法。
答案 4 :(得分:1)
1)你为什么不使用泛型:
LinkedList<DependencyDataCollection> lList;
2)这是LinkedList的缺点,你也可以使用HashMap或其他数据结构
答案 5 :(得分:0)
在最新版本的Java中,您可以使用generics,这样就不必在调用lList.get(i)
时强制转换生成的对象。例如:
LinkedList<DependencyDataCollection> lList = new LinkedList<DependencyDataCollection>();
...
DependencyDataCollection ddc = lList.get(0);
要获取特定元素的索引,请手动遍历列表。对于列表范围内的每个索引i,在那里获取DependencyDataCollection并查看它是否具有所需的属性。如果是,请保存索引。
答案 6 :(得分:0)
LinkedList在java.util中,可以包含任何类型的类。例如,我使用一个名为Application的类。所以在下面的代码中我只有一个应用程序列表。然后我把一个应用程序放在列表中。我也可以迭代我的应用程序,用我的对象做我想做的事情。
LinkedList<Application> app = new LinkedList<Application>();
app.add(new Application("firefox"));
Iterator<Application> iterable = app.iterator();
while(iterable.hasNext()){
Application eachapp = iterable.next();
}
如果你需要使用indexOf找到一个对象,你可以在我的情况“应用程序”中覆盖你的对象的相等方法,所以如果相等我声明它和应用程序等于和其他应用程序如果字段名称是相同的那么
app.indexOf(new Application(“firefox”))将返回我刚刚插入的应用程序的索引。