[英]How to Call Data structure from other classes
我想從另一個類調用數據結構,但我在這里發現了一個問題,你能幫我嗎?
這里是源代碼
SimBetWithFairRouting 類的數據結構
public Map<DTNHost, ArrayList<Double>> neighborsHistory;
我將在 NeighbourhoodSimilarity 類的這個方法中調用它
private double countDirectSimilarity(double[][] matrixEgoNetwork, int index) {
double sim=0;
for (int i = 0; i < matrixEgoNetwork.length; i++) {
//here the problem
if (matrixEgoNetwork[i][0]==this.countAggrIntStrength(*i will call it in here*) && matrixEgoNetwork[i][index]==1) {
sim++;
}
}
return sim;
}
有什么辦法可以在不將地圖更改為靜態形式的情況下完成這項工作? 線索:在類 SimBetWithFairRouting 中有復制方法,你能幫我嗎?
要訪問地圖,您必須將該類導入到您編寫方法的類中。 並且要在不創建實例的情況下訪問它,您必須將其設為靜態。
private double countDirectSimilarity(double[][] matrixEgoNetwork, int index) {
double sim=0;
for (int i = 0; i < matrixEgoNetwork.length; i++) {
if (matrixEgoNetwork[i][0]==this.countAggrIntStrength(SimBetWithFairRouting.neighborsHistory) && matrixEgoNetwork[i][index]==1) {
sim++;
}
}
return sim;
}
使您的地圖靜態
public static Map<DTNHost, ArrayList<Double>> neighborsHistory;
首先導入 SimBetWithFairRouting 類所在的包。 然后將該 Map (neighborsHistory) 設為靜態。
並訪問您可以使用的地圖
SimBetWithFairRouting.neighborsHistory
即 (ClassName.MapName)
從NeighbourhoodSimilarity擴展SimBetWithFairRouting類還可以讓您訪問鄰居歷史(如果SimBetWithFairRouting類不是最終的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.