簡體   English   中英

如何在if語句和for循環之外獲取數組

[英]how to get an array outside of an if statement and for loop

我正在開發應用程序,但遇到了問題……我無法在if語句之外訪問數組……這是我的代碼:

public class MainActivity extends Activity {

    TextView textLat;
    TextView textLong;
    TextView textChange;
    TextView testid;
    float gpsSpeed = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textLat = (TextView)findViewById(R.id.textLat);
        textLong = (TextView)findViewById(R.id.textLong);
        textChange = (TextView)findViewById(R.id.textChange);
        testid = (TextView)findViewById(R.id.testid);

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);


    }
      @SuppressWarnings("unchecked")
    public static void main(String argv[]) {

            try {

            File fXmlFile = new File("/Users/mkyong/staff.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            //optional, but recommended
            //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("station");
            int rows = nList.getLength();
            int cols = 9;

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    String[][]options = new String[cols][rows];

                    options[0][0] = eElement.getElementsByTagName("name").item(0).getTextContent();
                    options[1][0] = eElement.getElementsByTagName("code").item(0).getTextContent();
                    options[2][0] = eElement.getElementsByTagName("lat").item(0).getTextContent();
                    options[3][0] = eElement.getElementsByTagName("long").item(0).getTextContent();
                    options[4][0] = eElement.getElementsByTagName("longlat").item(0).getTextContent();
                    options[5][0] = eElement.getElementsByTagName("uziadvies").item(0).getTextContent();
                    options[6][0] = eElement.getElementsByTagName("bijzonderheden").item(0).getTextContent();
                    options[7][0] = eElement.getElementsByTagName("ref").item(0).getTextContent();
                    options[8][0] = eElement.getElementsByTagName("groterkleiner").item(0).getTextContent();

                }
            }
            } catch (Exception e) {
            e.printStackTrace();
            }
          }
    class mylocationlistener implements LocationListener {

            @Override
            public void onLocationChanged(Location location) {
                if(location != null)
                {
                    double pLong = location.getLongitude();
                    double pLat = location.getLatitude();
                    gpsSpeed = ((location.getSpeed()*3600)/1000);

                    textLat.setText(Double.toString(pLat));
                    textLong.setText("jo");

                    double compareLatCoords = 51.704704;
                    double compareLongCoords = 4.854369;

                    testid.setText(Float.toString(gpsSpeed));

                    if (pLat <= compareLatCoords)
                    {
                        textChange.setText("Je bent net de bedaulxstraat doorgereden");
                    }
                    if (pLong >= compareLongCoords)
                    {
                        textChange.setText("U bent door een andere straat gereden dan daarnet.");
                    }
                }

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }


        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

它是關於數組的:options [i] [j],在if語句之外無法訪問...我已經進行了兩天的工作,但找不到解決方案...

編輯:感謝所有的答案,但是當我嘗試在public void onLocationChanged(Location location) {...}訪問變量(例如: options[1][0] )時。 我收到錯誤消息:選項無法解析為變量。

肯尼思,問候

for-loop之外聲明數組String[][]options ,並在必要時進行定義。

String[][]options = null;
for (int temp = 0; temp < nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        options = new String[cols][rows];
        ...

如果您在if語句內定義任何變量,它將是local variable ->“,並且local variable的范圍是括起來的括號,您只能在if塊內部訪問它”,因此在if語句外部定義options

 String[][]options = new String[cols][rows];
if (nNode.getNodeType() == Node.ELEMENT_NODE) {

             ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM