Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java switch语句的问题_Java_Android_Performance_Switch Statement - Fatal编程技术网

Java switch语句的问题

Java switch语句的问题,java,android,performance,switch-statement,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 Marke

我最近编写了一些重复部分(冗余)的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语句,而不是其他任何东西。有人能帮我确认或拒绝吗?

如果
route
listRoutes.get(i)
的值不相同,通常会发生这种情况


如果
route
不是字符串,那么一个好的选择是:
route.equals()
会将对象与
“A”
进行比较,这将失败,但是
println
语句会导致调用
route.toString()

刚刚注意到,您在两个示例中使用了不同的“A”值。我认为第一点应该是:

BitmapDescriptorFactory.fromResource(R.mipmap.bus_gaza)

而不是android.R.drawable.btn_star_big_off

将你的问题分解成更小的部分。你的第一个选择很难通读

1) 您需要为
列表路由(i)
做些什么。您要考虑的输入是<代码>“A”/“代码>,<代码>”B“< /代码>和<代码>”C“< /代码> . 注意,您的原始程序在所显示的代码块之外的其他地方定义了一个冗余变量
route
。通过将代码分解成只做一件事的小方法来简化代码,将有助于避免此类问题

2) 这些字母对应于您的
mipmap
资源(
bus_gaza
brunei_bus
commercial_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\u location
假定为类变量,因为它不在代码中的任何位置(考虑将其命名为
mStartLocation

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

Log.i("YOUR_TAG", "Whatever you want to log");
然后,程序的大部分将变为

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

请注意,在此示例中,大小写
“C”
也是默认大小写,任何不是
“A”
“B”
的值都将被视为
“C”

请提供一个。不清楚您在运行第一个代码时所说的是什么。什么意思是“case未执行”,而system.out.println正在工作?!这个案例还应该如何执行?(为什么你要切换,然后立即有条件?)。我们最初认为图像已损坏,无法渲染
mark(toResourceId(listRoutes.get(i)))