【发布时间】:2017-04-20 08:38:58
【问题描述】:
我正在使用 Spring 框架,在下面的代码中,我想从数据库中获取经纬度并将其存储在 ArrayList 中,然后计算它们之间的距离。
问题是我无法弄清楚如何获取变量中两列的值,当我按照以下方式执行此操作时 [Ljava.lang.Object; cannot be cast to java.lang.Double 抛出异常
有人可以帮我吗?我读到我们可以使用 2D ArrayList 但我如何在以下代码中使用 ArrayList
RiderService.java
@Component
public class RiderService {
@Autowired RiderLocationRepository riderLocationRepository;
public float Location(Double lattitude,Double longitude)
{
Double lat2=24.927437;
Double lng2=67.094412;
ArrayList<Double> allRidersLocation = new ArrayList<Double>();
allRidersLocation= riderLocationRepository.findAllRidersLocation();
Iterator<Double> iterator = allRidersLocation.iterator();
while (iterator.hasNext()) {
System.out.println(" DB value " + iterator.next().doubleValue());
}
float distance=distFrom(lattitude, longitude, lat2, lng2);
System.out.println("Distance : " + (distance/1000));
return distance;
}
RiderRepository
public interface RiderLocationRepository extends JpaRepository<RiderLocation, Long>{
@Query("select r.latitude,r.longitude from RiderLocation r ")
ArrayList<Double> findAllRidersLocation();
}
【问题讨论】:
-
嗨@maria salahuddin 你调试过它吗,因为它看起来像解释中所说的对象和原始类型之间的转换有一个简单的问题,也许你可以发现你的代码中的差异。
-
@burhancerit 但是当我从数据库中只选择纬度时它工作正常
-
@burhancerit 从数据库中选择两列时出现问题
-
这很简单。您想将 2 个值存储在 1 个 double 中。如果只查询 1 列,则可以直接映射到 arraylist。用地图试试。一张地图可以一次取 2 个值(键和值)
标签: java database spring exception arraylist