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 13:42:26 浏览: 28
可以使用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类没有像数组那样有预先定义的大小。
相关问题
public class StrDemo{ public static void main(String[] args){ String str1=”We are Students”; int n1=str1.length; int n2=str1.indexof(“students”) boolean b=str1.contains(“good”) System.out.println(n1); System.out.println(n2); System.out.println(b); }
这段代码有一些语法错误,应该修改为:
```
public class StrDemo {
public static void main(String[] args) {
String str1 = "We are Students";
int n1 = str1.length();
int n2 = str1.indexOf("students");
boolean b = str1.contains("good");
System.out.println(n1);
System.out.println(n2);
System.out.println(b);
}
}
```
这段代码的作用是定义一个 `StrDemo` 类,其中包含一个 `main` 方法。在 `main` 方法中,定义了一个字符串变量 `str1`,并获取了它的长度和包含子串 `"students"` 的位置。同时,使用 `contains` 方法判断字符串中是否包含子串 `"good"`,并将结果输出到控制台。
class Test{ public static void main(String[] args){ { int i =10; } System.out.println(i); }
这段代码会报错,因为变量 `i` 的作用域仅限于内部的代码块中,无法在外部代码块中访问。在 `System.out.println(i)` 的位置,编译器会提示找不到符号 `i` 的错误。如果你想在外部访问变量 `i`,需要将其声明在外部代码块中。以下是修改后的代码:
```java
class Test {
public static void main(String[] args) {
int i;
{
i = 10;
}
System.out.println(i);
}
}
```
这样修改后,代码就可以成功输出变量 `i` 的值 10。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)