刚学JAVA,编了个程序可以编译通过,运行时就出异常了。帮我看下,谢谢了。
//: c09:E09_StringContainer.java
//+M java E09_StringContainer
import java.util.*;/****************** Exercise 9 ******************
* Create a container that encapsulates an array
* of String, and that only adds Strings and gets
* Strings, so that there are no casting issues
* during use. If the internal array isn't big
* enough for the next add, your container should
* automatically resize it. In main(), compare
* the performance of your container with an
* ArrayList holding Strings.
***********************************************/
class StringContainer { //class container
private int size = 10;
private String[] strings = new String[size];
private int index = 0;
public int setSize() { //if the array isn't big enough,resize
return size = size*2;
}
public void add(String s) {//add string
if(index>=size) {
setSize();
}
strings[index] = s;//||Exception here||.
index++;
}
public void print(){//print the list.
for(int i = 0;i<strings.length;i++) {
System.out.println(strings[i].toString());
}
}
public int getSize() {//get the size
return size;
}
public int getIndex() {//get the index which record the
// real string's numbers。
return index;
}
}
public class E09_StringContainer {
public static void main(String[] args) {
StringContainer strc = new StringContainer();
String[] strs = {"def","ghi","jkl","dadf","gfgai","gfadio","gaodafd","fdfdcvsfso","fdafdacims","fdafi","fdifadkm","daida","idak","aok","dia","fdm"};
for(int i =0;i<strs.length;i++) {
strc.add(strs[i]);//||exception||.
}
strc.print();
System.out.println(strc.getSize());
System.out.println(strc.getIndex());
}
}异常是这样:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at StringContainer.add(E09_StringContainer.java:26)
at E09_StringContainer.main(E09_StringContainer.java:46)
//: c09:E09_StringContainer.java
//+M java E09_StringContainer
import java.util.*;/****************** Exercise 9 ******************
* Create a container that encapsulates an array
* of String, and that only adds Strings and gets
* Strings, so that there are no casting issues
* during use. If the internal array isn't big
* enough for the next add, your container should
* automatically resize it. In main(), compare
* the performance of your container with an
* ArrayList holding Strings.
***********************************************/
class StringContainer { //class container
private int size = 10;
private String[] strings = new String[size];
private int index = 0;
public int setSize() { //if the array isn't big enough,resize
return size = size*2;
}
public void add(String s) {//add string
if(index>=size) {
setSize();
}
strings[index] = s;//||Exception here||.
index++;
}
public void print(){//print the list.
for(int i = 0;i<strings.length;i++) {
System.out.println(strings[i].toString());
}
}
public int getSize() {//get the size
return size;
}
public int getIndex() {//get the index which record the
// real string's numbers。
return index;
}
}
public class E09_StringContainer {
public static void main(String[] args) {
StringContainer strc = new StringContainer();
String[] strs = {"def","ghi","jkl","dadf","gfgai","gfadio","gaodafd","fdfdcvsfso","fdafdacims","fdafi","fdifadkm","daida","idak","aok","dia","fdm"};
for(int i =0;i<strs.length;i++) {
strc.add(strs[i]);//||exception||.
}
strc.print();
System.out.println(strc.getSize());
System.out.println(strc.getIndex());
}
}异常是这样:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at StringContainer.add(E09_StringContainer.java:26)
at E09_StringContainer.main(E09_StringContainer.java:46)
解决方案 »
- httpclient返回302后,其location的值为./ 跳转出错,请大侠帮着看看是什么原因。
- Java 正则表达式 解析方法名
- 這個異常怎麼解決
- 新手弱问一个调用问题
- 有关事件的问题???
- Oracle连接中下面的驱动有什么不同Class.forName(dirveName);与Class.forName(dirveName").newInstance();我很迷惑,望指点
- 想问一下Intellij IDEA编写控制台程序 要使用的图片放在那里才能被读取到啊
- 用jar命令打包时不能生成Main-Class:怎么办
- 哪里有<Java 2核心技术 2 高级特性>下载?
- 最短路径,顶点对频率的统计
- poolman连接池问题无法立即刷新的问题!!
- 应届生求助各位前辈!
public static void main(String[] args){
int size = 10;
String[] strings = new String[size];
size=20;
System.out.print(strings.length);
}
}
public class Test {
public static void main(String[] args){
int size = 10;
int[] ints = new int[size];
for(int i = 0;i<=ints.length;i++)
{
ints[i] = i ;
System.out.println(ints[i]);
}
System.out.print(ints.length);
}
}
这才是数组越界异常了。