【问题标题】:Java - for loop cycling through string arrayJava - 通过字符串数组循环循环
【发布时间】:2017-03-21 07:58:23
【问题描述】:

所以我是编程新手。我有一个名为 values 的字符串数组,其中包含大约 150 个字符串。而不是使用大量的 if 语句,我不想​​使用 for 循环,每次循环通过循环递增到数组中的下一个元素。我确定这是一个超级简单的修复,但我无法解决它。感谢您的建议!

routeListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,     int position, long id) {

                    String route = values[position];

                    int i;
                    for (i=0; i < values.length;i++) {

                        if (route.equals(values[0])) {

                            Intent intent = new Intent(view.getContext(), RouteDetails.class);
                            intent.putExtra("route", routeDetail[0]);
                            startActivity(intent);
                      }
                    values++;
                    }
                    /*if (route.equals(values[0])) {

                        Intent intent = new Intent(view.getContext(), RouteDetails.class);
                        intent.putExtra("route", routeDetail[1]);
                        startActivity(intent);

                    }
                    if (route.equals("Main Wall")) {

                        Intent intent = new Intent(view.getContext(), RouteDetails.class);
                        intent.putExtra("route", "Map of Main Wall");
                        startActivity(intent);

                    }
                    if (route.equals("1. Shark Bait - 5.9")) {

                        Intent intent = new Intent(MainActivity.this,     RouteDetails.class);
                        intent.putExtra("route", "Shark Bait");
                        startActivity(intent);

                    }
*/
                }

【问题讨论】:

  • 这段代码可以编译吗? valuesString[],如何使用 values++ 递增?

标签: android arrays string for-loop


【解决方案1】:

看起来你可以使用开关...如果你需要 int 和 string 比较,你可以使用两个开关来做到这一点。

    String route = values[position];

    switch(position) {
        case 0:
            Intent intent = new Intent(MainActivity.this, RouteDetails.class);
            intent.putExtra("route", routeDetail[0]);
            startActivity(intent);
            return;
        case 1:
            // Do stuff
            return;
    }

    switch(route) {
        case "Main Wall":
            Intent intent = new Intent(MainActivity.this, RouteDetails.class);
            intent.putExtra("route", "Map of Main Wall");
            startActivity(intent);
            return;

        case "Shark Bait":
            Intent intent = new Intent(MainActivity.this, RouteDetails.class);
            intent.putExtra("route", "Shark Bait");
            startActivity(intent);
            return;
    }

【讨论】:

  • 谢谢。所以有没有办法不用像 150 if 语句或开关?
【解决方案2】:

在循环内部,将硬编码的 0 替换为“i”。这将允许您的代码在循环的每次迭代中运行。例如,i 将被替换为 0,然后是 1,然后是 2,等等。

for (int i=0; i<values.length; i++) {

 if (route.equals(values[i])) {

    Intent intent = new Intent(view.getContext(), RouteDetails.class);
    intent.putExtra("route", routeDetail[i]);
    startActivity(intent);
 }
}

此外,不需要在末尾添加值计数器,因为它是由 for 循环中的 i++ 处理的。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-23
    • 2015-08-20
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多