在Java中的两个arrayList索引之间搜索泛型项
我正在尝试使用我创建的find()方法来搜索泛型类型为T的arrayList元素。搜索时,用户必须输入他们正在搜索的项,以及他们希望在其中搜索的两个索引startPosition和endPosition。每当我运行main()时,它总是打印-1,即使在我的测试代码中,Chevy在数组中明显介于0和3之间。有人能帮我找出为什么它没有打印出雪佛兰所在的正确索引吗?谢谢 列表:在Java中的两个arrayList索引之间搜索泛型项,java,generics,arraylist,Java,Generics,Arraylist,我正在尝试使用我创建的find()方法来搜索泛型类型为T的arrayList元素。搜索时,用户必须输入他们正在搜索的项,以及他们希望在其中搜索的两个索引startPosition和endPosition。每当我运行main()时,它总是打印-1,即使在我的测试代码中,Chevy在数组中明显介于0和3之间。有人能帮我找出为什么它没有打印出雪佛兰所在的正确索引吗?谢谢 列表: import java.util.Scanner; public class AList<T> implemen
import java.util.Scanner;
public class AList<T> implements ListInterface<T>
{
private T[] L;
private T k;
private int count;
public AList(int s)
{
L =(T[]) new Object[s];//Allows the client to decide the length of the list ASK HOW TO MAKE IT A SET SIZE OF 20
count = 0;
}//end of constructor
public void add(T item)throws ListException
{
if(count == L.length)
throw new ListException("Cannot add. List is full.");
if(item == null || item == "")
throw new ListException("Error. Unable to add. Cannot add null entries.");
L[count] = item;
count++;
}//end of add method
public void add(T item, int position)throws ListException
{
if(count == 0)
throw new ListException("Error. Unable to insert. List is empty.");
if(count == L.length)
throw new ListException("Error. Unable to insert. List is full");
if(item == null || item == "")
throw new ListException("Error. Unable to insert. Attempt to insert null object.");
if(position <= 0 || position > count)
throw new ListException("Error. Unable to insert. Bad position.");
for(int k = count-1; k >= position-1; k--)
{
L[k+1] = L[k];
L[2] = L[1];
}
L[position-1] = item;
count++;
}//end of insert method
public T get(int position)throws ListException
{
if(position <= 0 || position > count)
throw new ListException("Error. Unable to get. Bad position.");
if(count == 0)
throw new ListException("Error. Unable to get. List is empty.");
return L[position-1];
}// End of get method
public T set(T item, int position)throws ListException
{
if(item == null || item == "")
throw new ListException("Error. Unable to replace. Replacement cannot be null.");
if(position <= 0 || position > count)
throw new ListException("Error. Unable to replace. Bad position.");
if(count == 0)
throw new ListException("Error. Unable to replace. List is empty.");
T temp = L[position-1];
L[position-1] = item;
temp = item;
return temp;
}// End of set method
public int find(T item, int startPosition, int endPosition)throws ListException
{
if(startPosition < 0 || endPosition > count)
throw new ListException("Error. Unable to find. Start and/or end position bad.");
int found;
if(startPosition > endPosition)
found = -1;
else if(item.equals(L[startPosition]))
found = startPosition;
else
found = find(item, startPosition+1, endPosition);
return found;
}//method for finding
public int size()
{
return count;
}// End of size method
public String toString()
{
int k;
if(count == 0)
return "The list is empty. \n";
String temp = "";
for(k = 0; k < count; k++)
{
temp += L[k] += "\n";
}
return temp;
}//end of method toString
public T remove(int position)throws ListException
{
if(count == 0)
throw new ListException("Error. Unable to remove. List is empty.");
if(position <= 1 || position >= count)
throw new ListException("Error. Unable to remove. Bad position.");
T temp = L[position-1];
int k;
for(k = position-1; k <= count; k++)
{
L[k] = L[k+1];
}
count--;
return temp;
}//end of remove method
public void clear()
{
for(int k = count; k > 0; k--)
{
count--;
}
}// End of clear method
public boolean isEmpty()
{
if(L.length == 0)
return true;
else
return false;
}// End of isEmpty method
import java.util.Scanner;
公共类实现了ListInterface
{
私人T[]L;
私人电讯;
私人整数计数;
公共图书馆(INTS)
{
L=(T[])新对象[s];//允许客户端决定列表的长度,并询问如何将其设置为20
计数=0;
}//构造函数结束
公共void add(T项)抛出ListException
{
如果(计数=L.length)
抛出新ListException(“无法添加。列表已满”);
如果(项==null | |项==“”)
抛出新ListException(“错误。无法添加。无法添加空条目”);
L[计数]=项目;
计数++;
}//添加结束方法
公共void add(T项,int位置)抛出ListException
{
如果(计数=0)
抛出新ListException(“错误。无法插入。列表为空”);
如果(计数=L.length)
抛出新ListException(“错误。无法插入。列表已满”);
如果(项==null | |项==“”)
抛出新ListException(“错误。无法插入。尝试插入空对象。”);
if(职位计数)
抛出新ListException(“错误。无法插入。位置错误”);
对于(int k=count-1;k>=position-1;k--)
{
L[k+1]=L[k];
L[2]=L[1];
}
L[位置-1]=项目;
计数++;
}//插入结束法
公共T get(int位置)抛出ListException
{
if(职位计数)
抛出新ListException(“错误。无法获取。错误位置”);
如果(计数=0)
抛出新ListException(“错误。无法获取。列表为空。”);
返回L[位置-1];
}//获取结束方法
公共T集(T项,int位置)抛出ListException
{
如果(项==null | |项==“”)
抛出新ListException(“错误。无法替换。替换不能为null”);
if(职位计数)
抛出新的ListException(“错误。无法替换。错误位置”);
如果(计数=0)
抛出新ListException(“错误。无法替换。列表为空。”);
温度T=L[位置-1];
L[位置-1]=项目;
温度=项目;
返回温度;
}//集末法
公共int-find(T项、int-startPosition、int-endPosition)抛出ListException
{
if(起始位置<0 | |结束位置>计数)
抛出新的ListException(“错误。无法找到。开始和/或结束位置错误”);
int-found;
如果(开始位置>结束位置)
发现=-1;
else if(项等于(L[起始位置])
发现=起始位置;
其他的
找到=找到(项目,起始位置+1,结束位置);
发现退货;
}//查找方法
公共整数大小()
{
返回计数;
}//尺寸结束法
公共字符串toString()
{
int k;
如果(计数=0)
return“列表为空。\n”;
字符串temp=“”;
对于(k=0;k
}//节目结束
测试代码:
public static void main(String[] args)
{
try
{
AList<String> carList = new AList<String>(20);
carList.add("Ford");
carList.add("Chevy");
carList.add("Toyota");
carList.add("Mercedes");
System.out.println(carList);
System.out.println(carList.find("Chevy", 0, 3));
}
catch(ListException e)
{
System.out.println(e);
}
}
publicstaticvoidmain(字符串[]args)
{
尝试
{
AList carList=新AList(20);
卡利斯特加上(“福特”);
卡利斯特加上(“雪佛兰”);
carList.add(“丰田”);
卡利斯特加上(“梅赛德斯”);
系统输出打印LN(carList);
System.out.println(carList.find(“雪佛兰”,0,3));
}
捕获(ListException e)
{
系统输出打印ln(e);
}
}
列表界面:
public interface ListInterface<T>
{
public void add(T item)throws ListException;
public void add(T item, int position)throws ListException;
public T get(int position)throws ListException;
public T set(T item, int position)throws ListException;
public int find(T item, int startPosition, int endPosition);
public int size();
public T remove(int position) throws ListException;
public void clear();
public boolean isEmpty();
}
公共接口列表接口
{
公共作废添加(T项)抛出ListException;
公共void add(T项,int位置)抛出ListException;
公共T get(int位置)抛出ListException;
公共T集(T项,int位置)抛出ListException;
公共整数查找(T项,整数起始位置,整数结束位置);
公共整数大小();
公共T删除(int位置)抛出ListException;
公共空间清除();
公共布尔值为空();
}
如果开始位置小于结束位置,则有一个条件显式返回-1
。我相信这是一个输入错误,你的意思是在那里有一个
符号而不是,我切换了这个,我仍然得到-1作为输出。我的递归正确吗?为了一个作业,我花了好几个小时试图得到这个,我已经设法得到了除这个以外的所有方法。请展示你的AList
课程的其余部分。你发布的代码(在修正了打字错误后)似乎不是你问题的根源。包括所有的列表和它附带的界面。如果您也需要异常类,请告诉我。我不知所措,什么都试过了,所以我真的很感谢你的帮助。
if (startPosition > endPosition) {
// Changed here --^
return -1;