import java.util.*;
public class CommaSepratedNumMax
{
public static void main(String args[])
{
Scanner san = new Scanner(System.in);
ArrayList <Integer> Al = new ArrayList<Integer>();
String line;
String[] lineVector;
System.out.println("enter the series");
line = san.nextLine();
lineVector = line.split(",");
for(int i=0;i<lineVector.length;i++)
{
try
{
Al.add(i,Integer.parseInt(lineVector[i]));
}
catch(NumberFormatException e )
{
System.out.println(" ERROR: You did not enter a Ineger value");
break;
}
}
int max=Al.get(0);
for(int i=0;i<Al.size();i++)
{
if (Al.get(i)>max)
max=Al.get(i);
}
System.out.println("max no in series is="+max);
}
}
如果用户没有在代码上面输入整数值,则会给索引超出绑定异常以及更多异常如何解决这些错误
答案 0 :(得分:3)
如果i
抛出异常,则不会向List的i+1
位置添加元素。因此,在下一次迭代中,当您尝试将元素添加到Al.add(i,Integer.parseInt(lineVector[i]));
位置时,将获得IndexOutOfBoundsException。
添加到列表时不必指定索引。
更改:
Al.add(Integer.parseInt(lineVector[i]));
到
var container = $('.table-body');
//Create an empty container
var $trs = $();
['A', 'B'].forEach(function(index) {
//Create TR and append TDs to it
var $tr = $('<tr/>', {class: 'table-row', id: 'table-row-id-'+index});
$tr.append(
$('<td />', {class: 'edit', id: 'edit-id-'+index, value: index}).
add($('<td />', {class: 'name', id: 'name-id-'+index, text: 'Mr. '+index})).
add($('<td />', {class: 'status', id: 'status-'+index, text: 'MSc'}))
);
//Add each tr to the container
$trs = $trs.add($tr);
});
//Append all TRs to the container.
container.append($trs);
$(".table-body").on('click', 'tr', function() {
alert( 'Clicked row '+ ($(this).index()+1) );
//Use .text() as td doesn't have method .val()
//Empty first time as the td:first has no text until clicked.
alert( $(this).find('td:first').text() );
$(this).find('td:first').text('clicked');
});
答案 1 :(得分:0)
AL
的大小为0,因为没有给出整数。但是您正在尝试访问第一个项目(索引0)。这是不可能的,因为列表中没有项目。
在尝试从中获取任何内容之前,请检查列表是否有任何项目。
if (AL.size() > 0) {
int max = Al.get(0);
}