switch语句出现问题

时间:2018-04-26 19:01:12

标签: java android performance switch-statement

我最近写了一些带有重复部分(冗余)的Java代码,就像这样

    switch (listRoutes.get(i)) {
            case "A":
                if (route.equals("A")) {
                    System.out.println("Red icon displayed.");
                    Marker mCurrent = mMap.addMarker(new MarkerOptions().position(start_location)
                            .title("Arrival Time of Shuttle: " + Coordinates[0])
                            .icon(BitmapDescriptorFactory.fromResource(android.R.drawable.btn_star_big_off)));

                    System.out.println("mCurrent location"+ mCurrent.toString());

                    if (!start_location.equals(end_location)) {
                        animateMarker(end_location, mCurrent);
                    }

                }
                break;
            case "C":
                if (route.equals("C")) {
                    System.out.println("Yellow icon displayed");
                    mCurrent = mMap.addMarker(new MarkerOptions().position(start_location)
                            .title("Arrival Time of Shuttle: " + Coordinates[0])
                            .icon(BitmapDescriptorFactory.fromResource(R.mipmap.commercial_bus)));
                    //animateCarMove(mCurrent,start_location,end_location,3000,"C");
                    if (!start_location.equals(end_location))
                        animateMarker(end_location, mCurrent);

                }
                break;
            case "B":
                if (route.equals("B")) {
                    System.out.println("Blue icon displayed");
                    mCurrent = mMap.addMarker(new MarkerOptions().position(start_location)
                            .title("Arrival Time of Shuttle: " + Coordinates[0])
                            .icon(BitmapDescriptorFactory.fromResource(R.mipmap.brunei_bus)));


                    if (!start_location.equals(end_location))
                        animateMarker(end_location, mCurrent);
                    //animateCarMove(mCurrent, start_location, end_location, 3000, "B");
                }
                break;
            default:
                if (route.equals("C")) {
                    System.out.println("Default Icon displayed");
                    mCurrent = mMap.addMarker(new MarkerOptions().position(start_location)
                            .title("Arrival Time of Shuttle: " + Coordinates[0])
                            .icon(BitmapDescriptorFactory.fromResource(R.mipmap.commercial_bus)));
                    if (!start_location.equals(end_location))
                        animateMarker(end_location, mCurrent);
                    //animateCarMove(mCurrent,start_location,end_location,3000,"C");
                }
                break;
        }

即使System.out.println正在运作,发生的情况是“A”从未发生过。

我重构了这样:

int iconResource = R.mipmap.bus_gaza;
    switch (listRoutes.get(i)) {
            case "A":
                if (route.equals("A")) {
                    iconResource = R.mipmap.bus_gaza;
                }
                break;
            case "B":
                if (route.equals("B")) {
                    iconResource = R.mipmap.brunei_bus;
                }
                break;
            case "C":
            default:
                if (route.equals("C")) {
                    iconResource = R.mipmap.commercial_bus;
                }
                break;
        }
        mCurrent = mMap.addMarker(new MarkerOptions().position(start_location)
                .title("Arrival Time of Shuttle: " + Coordinates[0])
                .icon(BitmapDescriptorFactory.fromResource(iconResource)));

        if (!start_location.equals(end_location))
            animateMarker(end_location, mCurrent);

这样运行。我怀疑它来自switch语句而不是其他任何东西。有人可以为我确认或否认吗?

3 个答案:

答案 0 :(得分:0)

如果routelistRoutes.get(i)没有相同的值,通常会发生这种情况。

如果route不是字符串,那么一个很好的选择:route.equals()会将对象"A"进行比较,但这会失败,但是{{1}语句将导致println被调用。

答案 1 :(得分:0)

注意到你在2个例子中使用了“A”的不同值。我相信第一个应该是:

BitmapDescriptorFactory.fromResource(R.mipmap.bus_gaza)

而不是android.R.drawable.btn_star_big_off

答案 2 :(得分:0)

将问题分解成更小的部分。你的第一个选择非常难以阅读。

1)你需要为listRoutes(i)做点什么。您要考虑的输入是"A""B""C"

注意您的原始程序在您显示的代码块之外的其他位置定义了冗余变量route。通过将代码简化为仅执行一项操作的小方法来简化代码将有助于您避免此类问题。

2)这些字母与您的mipmap资源(bus_gazabrunei_buscommercial_bus)相对应。

3)然后你想根据(1)中的值在某处放置一个标记。

创建一些处理它的方法,例如:

public int toResourceId(String route) {
    int resourceId;
    switch (route) {
        case "A":
            resourceId = R.mipmap.bus_gaza;
            break;
        case "B":
            resourceId = R.mipmap.brunei_bus;
            break;
        case "C":
        default:
            resourceId = R.mipmap.commercial_bus;
    }
    return resourceId;
}

然后对resourceId执行某些操作,其中start_location被假定为类变量,因为它不在代码中的任何位置(考虑将其命名为mStartLocation

public void mark(int resourceId) {
    mCurrent = mMap.addMarker(new MarkerOptions().position(start_location)
            .title("Arrival Time of Shuttle: " + Coordinates[0])
            .icon(BitmapDescriptorFactory.fromResource(resourceId)));
}

如果您要记录开始和结束位置或当前位置信息,如您在示例中所述,为什么不使用Android的Log?您可以将逻辑添加到mark方法中:

Log.i("YOUR_TAG", "Whatever you want to log");

您的程序的大部分将变为

mark(toResourceId(listRoutes.get(i)))

请注意,在此示例中,案例"C"也是默认情况,任何非"A""B"的值都将被视为"C"