【问题标题】:How to get the value of two columns and store in ArrayList如何获取两列的值并存储在ArrayList中
【发布时间】: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();

}

【问题讨论】:

  • 嗨@mar​​ia salahuddin 你调试过它吗,因为它看起来像解释中所说的对象和原始类型之间的转换有一个简单的问题,也许你可以发现你的代码中的差异。
  • @burhancerit 但是当我从数据库中只选择纬度时它工作正常
  • @burhancerit 从数据库中选择两列时出现问题
  • 这很简单。您想将 2 个值存储在 1 个 double 中。如果只查询 1 列,则可以直接映射到 arraylist。用地图试试。一张地图可以一次取 2 个值(键和值)

标签: java database spring exception arraylist


【解决方案1】:

试试这个,

public float Location(Double lattitude,Double longitude)
    {
        Double lat2=24.927437;
        Double lng2=67.094412;

          List<RiderLocation> allRidersLocation  = riderLocationRepository.findAll();
          for (RiderLocation riderLocation : allRidersLocation) {
            float distance=distFrom(riderLocation.getLattitude(), riderLocation.getLongitude(), lat2, lng2);
            System.out.println("Distance : " + (distance/1000));
            return distance;
    }
    }

默认情况下,CrudRepository 上提供findAll() 方法

public interface RiderLocationRepository extends JpaRepository<RiderLocation, Long>{

}

【讨论】:

  • "java.util.ArrayList 无法转换为 com.ivl.townsmate.model.RiderLocation"
  • 我认为问题已经解决了
  • 将 List 更改为 List,错误抱歉
  • 这是我原始答案的剪切和粘贴,它甚至有我纠正的错误。
【解决方案2】:

您的查询返回一个 Object 数组,如下所示:

@Query("select r.latitude, r.longitude from RiderLocation r")
ArrayList<Object[]> array =  //execute_your_query();

要得到你的结果,你可以使用这个:

for(Object[] obj : array){//<<---------------list of object with two columns
   double d1 = (Double) obj[0];//<<-------col 1
   double d2 = (Double) obj[1];//<<-------col 2
}

【讨论】:

    【解决方案3】:

    使用 JpaRepository 默认为你提供的 findAll 方法,那么你需要这样的东西:

    public float Location(Double lattitude,Double longitude)
        {
            Double lat2=24.927437;
            Double lng2=67.094412;
    
              List<RiderLocation> allRidersLocation  = riderLocationRepository.findAll();
              for (RiderLocation riderLocation : allRidersLocation) {
                // use the riderLocation as required, it's not clear to me what methods this has!!
                System.out.println("Distance : " + (distance/1000));
                return distance;
        }
    

    【讨论】:

    • java.util.ArrayList cannot be cast to com.ivl.townsmate.model.RiderLocation 出现此错误:/
    猜你喜欢
    • 1970-01-01
    • 2012-06-30
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多