【问题标题】:how to get an array outside of an if statement and for loop如何在 if 语句和 for 循环之外获取数组
【发布时间】:2013-05-30 18:01:30
【问题描述】:

我正在开发一个应用程序,但遇到了一个问题... 我无法访问 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])时。 我收到错误:选项无法解析为变量。

您好, 肯尼斯

【问题讨论】:

  • 那是因为您的数组在 if 语句之外不存在。请了解一下variable scope。 (除此之外还有顺序编程。)
  • 您可以通过删除所有不相关的代码使问题更容易回答。

标签: java android arrays if-statement for-loop


【解决方案1】:

如果你在if语句中定义了任何变量,它将是local variable-->“并且局部变量的范围是括号,你只能在if块内访问它”,所以定义@ 987654323@外if声明

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

             ...
}

【讨论】:

    【解决方案2】:

    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];
            ...
    

    【讨论】:

    • 感谢您的回答!但是,当我尝试在 public void onLocationChanged(...){ 中创建变量时,我仍然收到一条错误消息:“选项无法解析为变量”
    猜你喜欢
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多