这是我的问题:
我有一个从我的Web服务返回的Java字符串,格式如下:
21,6.417,0.3055714,27,0.7778,0.04761905
现在我已经使用逗号分隔符的split函数将此字符串拆分为单独的字符串。
dataList = Arrays.asList(data.split(“,”));
我需要遍历此列表并将这些值分配到单独的字符串中,如下所示:
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));
现在获取值并分配给Individual int和float,获取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;
}
如何为任何可接受的列表大小解决此问题?
先谢谢 的Raji
答案 0 :(得分:0)
我认为您的问题是由于来自互联网的返回字符串的长度不可预测。
如果你的字符串可以有4个用','分隔的数字,你将获得4个字符串,索引从0到3。
但是如果你有6个用逗号分隔的数字,那么你将得到6个索引从0到5的字符串。
由于这个原因,我认为你必须检测列表的大小(使用dataList.size()
),然后处理不同的代码部分,例如,size = 4或size = 6。
此外,如果来自互联网的输入字符串将始终具有“固定”不同的可能大小(即siez = 4或大小= 6而没有其他大小),您可以使用switch(dataList.size())
来解决问题,但是如果你的长度不可预测,所以你的大小总是不同的,我建议你以语义的方式修改你的代码,这会导致你编写更好的代码,因为我认为你的代码对于特定的代码来说是“固定的”案件。如果是这种情况,那么继续编码。但如果不是,我建议考虑更多适应性解决方案。
答案 1 :(得分:0)
这是我现在修复的方式:
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);
}
}
}