package five; public class five { public static void main(String[] args) { try { int a=args.length; System.out.println("\na = "+a); a=22; int b=42/a; int c[]= {1}; c[42]=99; } catch (ArithmeticException e) { System.out.println("发生了被0除: "+e); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("数组下标越界: "+e); } finally { System.out.println("finally 语句被执行");} } }请用vector类改写一下
时间: 2023-12-03 12:42:26 浏览: 57
C#中static void Main(string[] args) 参数示例详解
可以使用Vector类来代替数组:
```java
import java.util.Vector;
public class Five {
public static void main(String[] args) {
try {
int a = args.length;
System.out.println("\na = " + a);
a = 22;
int b = 42 / a;
Vector<Integer> c = new Vector<Integer>(1);
c.set(42, 99);
} catch (ArithmeticException e) {
System.out.println("发生了被0除: " + e);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界: " + e);
} finally {
System.out.println("finally 语句被执行");
}
}
}
```
注意,这里使用了Vector类的set方法来设置元素值,因为Vector类没有像数组那样有预先定义的大小。
阅读全文