Peter Gigih
Peter Gigih

Reputation: 55

How to Call Data structure from other classes

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?

Upvotes: 2

Views: 797

Answers (3)

Onkar Musale
Onkar Musale

Reputation: 909

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)

Upvotes: 0

Rohit sharma
Rohit sharma

Reputation: 71

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

Upvotes: 0

Pabasara Mahindapala
Pabasara Mahindapala

Reputation: 322

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;

Upvotes: 1

Related Questions