2

I want to call data structure from another classes but i find a problem here, can you help me?

here the source code

The data structure from class SimBetWithFairRouting

public  Map<DTNHost, ArrayList<Double>> neighborsHistory;

and i will call it in this method from class 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;

}

any way i can make this work without changing the map into static form? clue : in the class SimBetWithFairRouting had replicate method, can you help me?

3 Answers3

1

To access the map, you have to import that class to the class where you write the method. And to access it without creating an instance you have to make it static.

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;

}

Make your map static

public static Map<DTNHost, ArrayList<Double>> neighborsHistory;
0

First import that package where your SimBetWithFairRouting class resides. and then make that Map (neighborsHistory) as static.

and to access that map you can use

    SimBetWithFairRouting.neighborsHistory

which is (ClassName.MapName)

Onkar Musale
  • 909
  • 10
  • 25
  • can i make it without static?, because i had method replicate in the class SimBetWithFairRouting, isn't static is the same with replicate? – Peter Gigih Mar 14 '19 at 06:29
  • You have to make it static else you have to create object of that class for accessing that variable. – Onkar Musale Mar 14 '19 at 06:34
0

Extending SimBetWithFairRouting class from NeighbourhoodSimilarity can also give you access to neighborsHistory (if SimBetWithFairRouting class is not final).