Warm tip: This article is reproduced from stackoverflow.com, please click
arrays java list static

List array of a static class returns always same values

发布于 2020-03-31 23:01:12

I am trying to fill-in a static list of Arrays. The function RTTWifiPositionsToJava is used from Unity (C#) to pass the Parameters for each WifiPosition. But each item of the list m_RTTWifiPositions has the same value as the last added value.
Can somebody help please, thanks :)

static class RTTWifiPos
{  
    static String strMacAddress;
    static double posX, posY, posZ;
    static double distance; 
    static double distanceDev;
}

static List<RTTWifiPos> m_RTTWifiPositions;

public static boolean RTTWifiPositionsToJava(int nIndex, String strMacAddress, double PosX, double PosY, double PosZ, double distance, double distanceStdDev)
{  
    if(nIndex == 0)
    {
       if(m_RTTWifiPositions != null)  
            m_RTTWifiPositions.clear();
       else
            m_RTTWifiPositions = new ArrayList<RTTWifiPos>();
    }
    RTTWifiPos rttpos = new RTTWifiPos();

    rttpos.strMacAddress = strMacAddress;
    rttpos.posX = PosX;
    rttpos.posY = PosY;
    rttpos.posZ = PosZ;
    rttpos.distance = distance;
    rttpos.distanceDev = distanceStdDev;

    m_RTTWifiPositions.add(rttpos);

    return true;
}

The function RTTWifiPositionsToJava is called from Unity3D like this:

for (nIndex = 0; nIndex < WifiRTTSignals.Count; nIndex++)
{
    result = m_pluginWifiRTTClass.CallStatic<bool>("RTTWifiPositionsToJava", nIndex, WifiRTTSignals[nIndex].macAddress, (double) WifiRTTSignals[nIndex].pos.x, (double) WifiRTTSignals[nIndex].pos.y, (double) WifiRTTSignals[nIndex].pos.z, (double) WifiRTTSignals[nIndex].distance, (double) 0f);
}
Questioner
NaScAR
Viewed
16
flaimme 2020-01-31 20:44

The variables in RTTWifiPos will always be the same becus the are static. Static variables will always have the same value independent of objects explanation. Example:

static class RTTWifiPos
{  
    String strMacAddress;
    double posX, posY, posZ;
    double distance; 
    double distanceDev;
}

I would also make them private and with getter/setter methods instead for better control.