我想问一下有没有人知道如何解决这个问题。 我收到此消息:
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);
我想问一下我现在做的事情对吗?我想构建一个使用泛型的程序,其中包含以下方法:添加、删除、获取、包含和大小方法。但是当我构建程序时,我发现泛型无法创建数组,因此我使用了本网站另一篇文章中的内容:[如何:泛型数组创建并使用弱类型(未经检查)。因此,该程序的主题是某人在控制台中输入他的名字、姓氏和电话号码,然后将其放入泛型数组中,以便我可以将其保存到一个对象中,然后保存到特定位置。之后,程序会动态增加阈值,以便用户拥有大量联系人。然后他会被告知他的联系人是否被重复,并将立即删除。之后他会被询问是否要删除契约(Contract),然后程序统计全部结束,直到用户完成他的工作(添加契约(Contract))。
我很高兴听到一些意见。
(如果我听不懂,我很抱歉我的英语。)
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();
}
}
}
最佳答案
为什么不使用 List<YourGenericType> list = new ArrayList<YourGenericType>();
现在您可以调用list.size(); list.add(); list.set(); list.get();...
。 ArrayList 也具有动态大小。
我建议这样做:删除您的 Main 并将您的 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();
}
}
}
关于java - 使用泛型实现数组列表,其中列表的最大大小是动态的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660156/