【问题标题】:Java ArrayList of Strings Throwing ArrayIndexOutOfBoundException [duplicate]抛出ArrayIndexOutOfBoundException的Java ArrayList [重复]
【发布时间】:2016-02-02 16:21:54
【问题描述】:

这是我的问题:

  1. 我有一个从我的 Web 服务返回的 Java 字符串,格式如下:

    21,6.417,0.3055714,27,0.7778,0.04761905

  2. 现在我已经通过使用逗号分隔符的拆分功能将此字符串拆分为单独的字符串。

    dataList = Arrays.asList(data.split(","));

  3. 我需要遍历这个列表并将这些值分配到单独的字符串中,如下所示:

    int playsCount = Integer.parseInt(dataList.get(0));
    float sumTimeViewed = Float.valueOf(dataList.get(1));
    float avgTimeViewed = Float.valueOf(dataList.get(2));
    int loadsCount = Integer.parseInt(dataList.get(3));
    float loadPlayRatio = Float.valueOf(dataList.get(4));
    float avgViewDropOff = Float.valueOf(dataList.get(5));
    
  4. 现在在获取值并分配给单个 int 和浮点数时,得到 ArrayIndexOutofBoundsException。这是因为有时列表返回的大小为 4 而不是 6。这是代码:

    reportDataList = getReportData(entry.id);
        //System.out.println("reportDataList.size()"+reportDataList.size());
        if(reportDataList.size() >= 1) {
            for(int i=0;i<reportDataList.size();i++) { 
                if(!reportDataList.get(0).equals("")) {
                    playsCount = Integer.parseInt(reportDataList.get(0));
                    }
                if(!reportDataList.get(1).equals("")) {
                    sumTimeViewed = Float.valueOf(reportDataList.get(1));
                    }
                if(!reportDataList.get(2).equals("")) {
                    avgTimeViewed = Float.valueOf(reportDataList.get(2));
                    }
                if(!reportDataList.get(3).equals("")) {
                    loadsCount = Integer.parseInt(reportDataList.get(3));
                    }
                if(!reportDataList.get(4).equals("")) {
                    loadPlayRatio =Float.valueOf(reportDataList.get(4));
                    }
                if(!reportDataList.get(5).equals("")) {
                    avgViewDropOff = Float.valueOf(reportDataList.get(5));
            }
            }
        }
    

这里是 getReportData 方法:

private List<String> getReportData(String id) throws KalturaApiException {
    List<String> headerList = null;
    List<String> dataList = new ArrayList<String>();
    ReportService reportService = client.getReportService();
    ReportType reportType = ReportType.TOP_CONTENT;
    ReportInputFilter reportInputFilter = new ReportInputFilter();
    reportInputFilter.fromDate = 1390156200;
    reportInputFilter.toDate = 1453660200;
    ReportTotal reportTotal = reportService.getTotal(reportType, reportInputFilter, id);
    String data = reportTotal.data;
    if(data != null) {
        dataList = Arrays.asList(data.split(","));
    }
    if(dataList.size() >= 1) {
        System.out.println("dataList.size() ------->"+dataList.size());
    }
    return dataList;
    }

对于任何可接受的列表大小,如何解决此问题?

提前致谢 拉吉

【问题讨论】:

  • "有时列表返回的大小为 4 而不是 6";嗯,有你的解释。您还需要了解什么?
  • 好的,然后检查尺寸并相应地处理?
  • 或者只显示产生 4 个元素的输入字符串。我敢打赌只有 3 个逗号
  • @Andrew - 我正在检查尺寸并进行相应的处理。但不确定这是执行此操作的正确方法 ... if(reportDataList.size() == 4) { //处理所有为 4 的日期 } else { // 处理所有不是 4 的数据 }
  • 如果列表大小是 4 而不是 6,这里没有人会知道您的程序需要做什么。这取决于您或您的用户或任何指定您的程序应该做什么的人。

标签: java string arraylist


【解决方案1】:

我认为您的问题是由于从互联网返回的字符串的长度不可预测。

如果你的字符串可以有 4 个数字,用 ',' 分隔,你会得到 4 个字符串,索引从 0 到 3。

但是如果你有 6 个用逗号分隔的数字,你会得到 6 个索引从 0 到 5 的字符串。

因此,我认为您必须检测列表的大小(使用 dataList.size() ),然后处理不同部分的代码,例如 size=4 或 size=6。

此外,如果您从 Internet 输入的字符串始终具有“固定”不同的可能大小(即 siez=4 或 size=6 而没有其他大小),您可以使用 switch(dataList.size()) 来解决这个问题,但如果你的长度是不可预测的,所以你可以有不同的大小,我建议你以语义方式修改你的代码,这可以引导你编写更好的代码,因为我认为你的代码对于特定情况来说是“固定的”。如果是这种情况,那么请继续您的编码。但如果不是,我会建议考虑更具适应性的解决方案。

【讨论】:

    【解决方案2】:

    这是我现在修复的方法:

    if(reportDataList.size() == 4) {
                //System.out.println("Size is 4");
                for(int i=0;i<reportDataList.size();i++) { 
                    if(!reportDataList.get(0).equals("")) {
                        playsCount = Integer.parseInt(reportDataList.get(0));
                        //System.out.println("playsCount :"+playsCount);
                        accountVO.setMediaPlays(playsCount);
                    }
                    if(!reportDataList.get(1).equals("")) {
                        sumTimeViewed = Float.valueOf(reportDataList.get(1));
                        //System.out.println("sumTimeViewed :"+sumTimeViewed);
                        accountVO.setSumTimeViewed(sumTimeViewed);
                    }
                    if(!reportDataList.get(2).equals("")) {
                        avgTimeViewed = Float.valueOf(reportDataList.get(2));
                        //System.out.println("avgTimeViewed :"+avgTimeViewed);
                        accountVO.setAvgViewTime(avgTimeViewed);
                    }
                    if(!reportDataList.get(3).equals("")) {
                        loadsCount = Integer.parseInt(reportDataList.get(3));
                        //System.out.println("loadsCount :"+loadsCount);
    
                    }
                }
            }
            else {
                //System.out.println("Size is not 4");
                for(int i=0;i<reportDataList.size();i++) { 
                    if(!reportDataList.get(0).equals("")) {
                        playsCount = Integer.parseInt(reportDataList.get(0));
                        accountVO.setMediaPlays(playsCount);
                        //System.out.println("playsCount :"+playsCount);
                    }
                    if(!reportDataList.get(1).equals("")) {
                        sumTimeViewed = Float.valueOf(reportDataList.get(1));
                        //System.out.println("sumTimeViewed :"+sumTimeViewed);
                        accountVO.setSumTimeViewed(sumTimeViewed);
                    }
                    if(!reportDataList.get(2).equals("")) {
                        avgTimeViewed = Float.valueOf(reportDataList.get(2));
                        //System.out.println("avgTimeViewed :"+avgTimeViewed);
                        accountVO.setSumTimeViewed(sumTimeViewed);
                    }
                    if(!reportDataList.get(3).equals("")) {
                        loadsCount = Integer.parseInt(reportDataList.get(3));
                        //System.out.println("loadsCount :"+loadsCount);
                    }
                    if(!reportDataList.get(4).equals("")) {
                        loadPlayRatio =Float.valueOf(reportDataList.get(4));
                        //System.out.println("loadPlayRatio :"+loadPlayRatio);
                        accountVO.setPlayToImpressionRatio(loadPlayRatio);
                    }
                    if(!reportDataList.get(5).equals("")) {
                        avgViewDropOff = Float.valueOf(reportDataList.get(5));
                        //System.out.println("avgViewDropOff :"+avgViewDropOff);
                        accountVO.setAvgViewDropOff(avgViewDropOff);
                    }
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多