在android中搜索数组

时间:2016-05-27 18:58:14

标签: java arrays

所以,我试图让我的应用搜索数组,其中每个对象包含多个值,如下所示:

public static String name;
public Boolean inUse;
public static Boolean covered;
parkingSpots[] spots = new parkingSpots[9];

 spots[0] = new parkingSpots("A1", false, false);
 spots[1] = new parkingSpots("A2", false, false);
 spots[2] = new parkingSpots("A3", false, false);
 spots[3] = new parkingSpots("B1", false, false);
 spots[4] = new parkingSpots("B2", false, false);
 spots[5] = new parkingSpots("B3", false, false);
 spots[6] = new parkingSpots("C1", false, true);
 spots[7] = new parkingSpots("C2", false, true); 
 spots[8] = new parkingSpots("C3", false, true);

我想要做的是搜索该地点是否被覆盖并且未被使用,然后返回具有该标准的第一个可用地点的名称,所以我这样做:

 public boolean coveredSearch() {
    if (Arrays.asList(spots).contains(covered = true)) {
        if (Arrays.asList(spots).contains(inUse = false)) {
            return Arrays.asList(spots).contains(name);

        }

    }

但出于某种原因,运行此方法会导致我的应用崩溃。有谁知道原因?

4 个答案:

答案 0 :(得分:2)

在内部包含使用等于。您将一个赋值作为参数传递,猜测它会推断您要比较哪个属性以便定义相等性,但这不起作用。 您的问题可能与documentation有关。

在这种情况下,什么定义了相等,你的类等于实现。

答案 1 :(得分:1)

您的代码会检查是否有任何覆盖的广告位,以及是否有任何未使用的广告位,但是没有检查是否存在同时存在的广告位同时。您不能使用独立的contains()来电,即使您正确使用它们,也不是(请参阅其他答案)。

您需要的是一个简单的搜索循环,以找到已覆盖和未使用的 点,然后返回 点的名称:

public String findCovered() {
    for (parkingSpots spot : this.spots)
        if (spot.isCovered() and ! spot.isInUse())
            return spot.getName();
   return null; // No unused covered parking spots available
}

或者,如果使用Java 8:

public Optional<String> findCovered() {
    return Arrays.stream(this.spots)
                 .filter(s -> s.isCovered() and ! s.isInUse())
                 .map(parkingSpots::getName)
                 .findAny();
}

另外,请注意您的字段错误。 namecovered字段不得staticinUsecovered字段应为boolean ,而不是Boolean

也许它只是实际代码的缩写,但spots不应该是parkingSpots的字段。如果必须在那里,则必须是static

public class parkingSpots { // Should be name ParkingSpots
    private String name;
    private boolean inUse;
    private boolean covered;
    // constructors and methods here
}
public class ParkingLot {
    private parkingSpots[] spots;
    // constructors and methods here, incl. findCovered()
}

答案 2 :(得分:1)

如上所述,你不能使用那样的包含。包含使用equals()方法检查元素在List中的位置。

将此方法放入ParkingSpots类中。并呼吁它找到你想要的地方。

public String coveredSearch(List<ParkingSpots> parkingSpots) {
        for (ParkingSpots p: parkingSpots) {
            if (!p.inUse && p.covered) {
                return p.name;
            }
        }
        return "Cannot find parking space";
    }

测试:

public class SpacesTest {

    public static void main(String[] args) {

        ParkingSpots ps = new ParkingSpots("Dummy", false, false);

        ParkingSpots[] spots = new ParkingSpots[9];

         spots[0] = new ParkingSpots("A1", false, false);
         spots[1] = new ParkingSpots("A2", false, false);
         spots[2] = new ParkingSpots("A3", false, false);
         spots[3] = new ParkingSpots("B1", false, false);
         spots[4] = new ParkingSpots("B2", false, false);
         spots[5] = new ParkingSpots("B3", false, false);
         spots[6] = new ParkingSpots("C1", false, true);
         spots[7] = new ParkingSpots("C2", false, true); 
         spots[8] = new ParkingSpots("C3", false, true);

         System.out.println(ps.coveredSearch(Arrays.asList(spots)));
    }    
}

输出:C3

同样如我所说,我建议您将ParkingSpots重命名为ParkingSpot。

答案 3 :(得分:0)

我不知道错误是什么,但你的代码不起作用。

您有以下

Arrays.asList(spots).contains(covered = true)

这与以下代码基本相同。

covered = true;
Arrays.asList(spots).contains(covered)

因为true永远不会等于你的parkingSpots个实例,所以它总是会返回false。

关于命名的注意事项

按照惯例,您的类应该调用ParkingSpot而不是parkingSpots,这样可以让您的代码更易于阅读。