我想问一下是否有人知道如何解决这个问题。 我收到了这条消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Main.add(Main.java:40) // it is for: myList[actSize++] = object;
at Main.main(Main.java:127) //it is for: ob.add(str+str1+str2);
我想问一下我的表现是否正确。我想构建一个使用泛型的程序,其中包括:方法add,remove,get,contains和size。但是当我构建程序时,我发现泛型不能创建数组,所以我在本网站的另一个帖子中使用:[如何:创建泛型数组并使用弱类型(未选中)。所以这个程序的主题是有人在控制台输入他的名字最后一个和他的电话号码,然后把它放在一系列的泛型中,这样我就可以把它保存到一个对象然后进入一个特定的位置。之后,程序动态地增加阈值,以便用户拥有大量的联系人。然后他会被告知他的联系人是否已经重复,并且会立即删除。之后他会被问到是否要删除合同,然后全部删除程序统计数据,直到用户完成工作(添加合同)。
我很高兴听到一些意见。
(如果我不能理解,我很抱歉我的英语。)
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main < T >
{
private Object[] myList;
public static int actSize = 0;
public Main(int s)
{
myList = new Object[s];
}
public T Array(int i)
{
@SuppressWarnings("unchecked")
final T t = (T) myList[i];
return t;
}
public void add(T object) //end
{
if (myList.length - actSize <= 5)
{
myList = Arrays.copyOf(myList, myList.length * 2);
System.out.println("\nNew length: " + myList.length);
}
myList[actSize++] = object;
}
public void add(T object, int position) //end
{
myList[position++] = object;
}
public boolean remove(T object) //end
{
if (myList[actSize].equals(object))
{
myList[actSize] = myList[actSize - 1];
return true;
}
else
return false;
}
public void remove(int position) //end
{
myList[position] = myList[position - 1];
}
@SuppressWarnings("unchecked")
public T get(int index) //end
{
if (index < actSize)
{
return (T) myList[index];
}
else
{
throw new ArrayIndexOutOfBoundsException();
}
}
public boolean contains(T object) //end
{
if (myList[actSize].equals(object))
{
return false;
}
else
myList[actSize] = myList[actSize - 1];
return true;
}
public int size() //end
{
return actSize;
}
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
@SuppressWarnings("rawtypes")
Main ob = new Main(actSize);
String str, str1, str2;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
while (true)
{
System.out.print("Do you want to add a contact? press any key to continue or n to exit :");
String str5 = br.readLine();
if ("n".equalsIgnoreCase(str5))
{
System.exit(1);
}
System.out.print("Enter your First Name :");
str = br.readLine();
System.out.print("Enter your Last Name :");
str1 = br.readLine();
System.out.print("Enter your Phone Number :");
str2 = br.readLine();
ob.add(str + str1 + str2);
ob.add(str + str1 + str2, actSize);
if (ob.contains(str + str1 + str2) == true)
{
System.out.println("The contact you entered is duplicated");
ob.remove(ob.contains(str + str1 + str2));
System.out.println("The contact has been removed");
}
System.out.print("Do you want to add contracts? press y to continue :");
String str3 = br.readLine();
System.out.println("You just entered:" + str3);
if ("y".equalsIgnoreCase(str3))
{
System.out.print("Which contract do you wish to be removed?");
String str4 = br.readLine();
if (str4.equals(actSize))
{
ob.remove(actSize);
System.out.println("The contact has been deleted.");
}
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
为什么不使用List<YourGenericType> list = new ArrayList<YourGenericType>();
现在你可以致电list.size(); list.add(); list.set(); list.get();...
。 ArrayList也有动态大小。
我建议这样的事情:删除您的Main并将主要方法更改为:
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
String str, str1, str2;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
System.out
.print("Do you want to add a contact? press any key to continue or n to exit :");
String str5 = br.readLine();
if ("n".equalsIgnoreCase(str5)) {
System.exit(1);
}
System.out.print("Enter your First Name :");
str = br.readLine();
System.out.print("Enter your Last Name :");
str1 = br.readLine();
System.out.print("Enter your Phone Number :");
str2 = br.readLine();
StringBuilder sb = new StringBuilder();
sb.append(str + str1 + str2);
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
if (s.equals(sb.toString())) {
System.out.println("Duplicate");
list.remove(i);
}
}
list.add(sb.toString());
System.out
.print("Do you want to add contracts? press y to continue :");
String str3 = br.readLine();
System.out.println("You just entered:" + str3);
if ("y".equalsIgnoreCase(str3)) {
System.out
.print("Which contract do you wish to be removed?");
String str4 = br.readLine();
int index = Integer.parseInt(str4);
if (index < list.size() && index > 0) {
list.remove(index);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
首先,您可以使用List
存储未知数量的数据。
代码:
myList[actSize++] = object;
抛出异常,因为myList
初始化为0
长度。所以你不能把任何东西放在索引0中。