当使用循环Java向ArrayList添加值超出限制时,请尝试捕获
我想将所有值添加为0.0到一个ArrayList中,该列表的大小设置为MAX=5。我使用for循环来添加值。当列表超过最大大小时,我想捕获一个越界异常。消息将输出列表大小应为5的原因。然后它将打印出列表值,即5“0.0”。 我将通过将最大值更改为8来测试它:当使用循环Java向ArrayList添加值超出限制时,请尝试捕获,java,for-loop,while-loop,try-catch,indexoutofboundsexception,Java,For Loop,While Loop,Try Catch,Indexoutofboundsexception,我想将所有值添加为0.0到一个ArrayList中,该列表的大小设置为MAX=5。我使用for循环来添加值。当列表超过最大大小时,我想捕获一个越界异常。消息将输出列表大小应为5的原因。然后它将打印出列表值,即5“0.0”。 我将通过将最大值更改为8来测试它: try { for (int i=0; i<8; i++) myList.add(0.0); 试试{ for(int i=0;i在for语句中声明的i变量的范围仅限于for 因此,您将永远无法在while语句中使用它 消息将
try {
for (int i=0; i<8; i++)
myList.add(0.0);
试试{
for(int i=0;i在for
语句中声明的i
变量的范围仅限于for
因此,您将永远无法在while
语句中使用它
消息将输出列表大小应为5的原因
IndexOutOfBoundsException
在这里对您没有帮助,因为它永远不会出现。
您无法访问列表范围之外的索引。
只能在和List中添加元素。add()
和ArrayList。add()
不会在其javadoc中指定任何异常。
现在,如果您在一个大小为4的列表上执行了list.get(4)
,则异常将上升为:
E java.util.ArrayList.get(int-index)
抛出:
IndexOutOfBoundsException-如果索引超出范围(索引<0||
索引>=大小()
为了确保不超过列表中特定数量的元素,必须显式检查它
在实际代码中,您可以指定一个入口点来在列表中添加元素,并在那里进行检查:
for (int i=0; i< MAX; i++) {
addInList(0.0));
}
public void addInList(Double value){
if (doubles.size() > MAX){
System.out.println(e.getMessage());
System.out.println("ArrayList size allowed at" + MAX);}
for (Double myList: myList)
System.out.println(myList);
}
}
doubles.add(value);
}
for(int i=0;i最大值){
System.out.println(e.getMessage());
System.out.println(“ArrayList大小允许在“+MAX”);}
for(双myList:myList)
System.out.println(myList);
}
}
双倍。加(值);
}
在for
语句中声明的i
变量的范围仅限于for
因此,您将永远无法在while
语句中使用它
消息将输出列表大小应为5的原因
IndexOutOfBoundsException
在这里对您没有帮助,因为它永远不会出现。
您无法访问列表范围之外的索引。
只能在和List中添加元素。add()
和ArrayList。add()
不会在其javadoc中指定任何异常。
现在,如果您在一个大小为4的列表上执行了list.get(4)
,则异常将上升为:
E java.util.ArrayList.get(int-index)
抛出:
IndexOutOfBoundsException-如果索引超出范围(索引<0||
索引>=大小()
为了确保不超过列表中特定数量的元素,必须显式检查它
在实际代码中,您可以指定一个入口点来在列表中添加元素,并在那里进行检查:
for (int i=0; i< MAX; i++) {
addInList(0.0));
}
public void addInList(Double value){
if (doubles.size() > MAX){
System.out.println(e.getMessage());
System.out.println("ArrayList size allowed at" + MAX);}
for (Double myList: myList)
System.out.println(myList);
}
}
doubles.add(value);
}
for(int i=0;i最大值){
System.out.println(e.getMessage());
System.out.println(“ArrayList大小允许在“+MAX”);}
for(双myList:myList)
System.out.println(myList);
}
}
双倍。加(值);
}
1st:
出现错误是因为必须在for循环外部声明i
,因为i
的范围在for
循环内
第二名:
ByArrayList myList=新的ArrayList(最大值)代码>您没有固定列表的大小。它只是设置它的初始容量
您需要执行以下操作(简而言之):
int MAX=5;
ArrayList myList=新的ArrayList(5);
试一试{
for(int i=0;i<8;i++)//引发IndexOutOfBoundsException
{
如果(myList.size()>MAX)
抛出新的IndexOutOfBoundsException(“我的消息”);
myList.add(0.0);
}
}catch(IndexOutOfBoundsException e){
System.out.println(“异常:+e.getMessage());
}
N.B:此处不需要使用indexootfboundsexception
。仅针对您的问题显示。1:
出现错误是因为必须在for循环外部声明i
,因为i
的范围在for
循环内
第二名:
ByArrayList myList=新的ArrayList(最大值)代码>您没有固定列表的大小。它只是设置它的初始容量
您需要执行以下操作(简而言之):
int MAX=5;
ArrayList myList=新的ArrayList(5);
试一试{
for(int i=0;i<8;i++)//引发IndexOutOfBoundsException
{
如果(myList.size()>MAX)
抛出新的IndexOutOfBoundsException(“我的消息”);
myList.add(0.0);
}
}catch(IndexOutOfBoundsException e){
System.out.println(“异常:+e.getMessage());
}
N.B:此处不需要使用indexootfboundsexception
。仅针对您的问题显示。您作为ArrayList构造函数参数传递的MAX
值没有达到预期效果。这是一个初始容量。这意味着,当您第一次初始化ArrayList时,您希望它至少能够存储作为构造函数参数传递的值量
为了实现您的要求,我建议您创建自己的“YourException”(根据您的喜好命名),并使用关键字extensedexception
,使其与其他异常一样工作。那么你应该这样做
int MAX = 5;
ArrayList<Double> myList = new ArrayList<Double>(5);
try {
for (int i = 0; i < 8; i++)// to throw IndexOutOfBoundsException
{
if(myList.size()>MAX)
throw new IndexOutOfBoundsException("My message");
myList.add(0.0);
}
} catch (IndexOutOfBoundsException e) {
System.out.println("Exception : "+e.getMessage());
}