【问题标题】:Searching for a generic item between two arrayList indexes in Java在Java中的两个arrayList索引之间搜索通用项
【发布时间】:2017-04-23 07:25:21
【问题描述】:

我正在尝试使用我创建的方法 find() 来搜索泛型类型 T 的 arrayList 元素。搜索时,用户必须输入他们正在搜索的项目,以及他们想要搜索的两个索引在 startPosition 和 endPosition 之间。每当我运行 main() 时,它总是打印 -1,即使在我的测试代码中,Chevy 显然在数组中的 0 和 3 之间。谁能帮我弄清楚为什么它没有打印雪佛兰所在的正确索引?谢谢!

列表:

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

}//程序结束

测试代码:

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);
    }
}

列表接口:

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();
}

【问题讨论】:

  • 请显示您的AList 课程的其余部分。您发布的代码(修正错字后)似乎不是问题的根源。
  • 包含了所有的 AList 和与之配套的接口。如果您还需要异常类,请告诉我。我很茫然,并且已经尝试了所有方法,所以我非常感谢您的帮助。

标签: java generics arraylist


【解决方案1】:

如果开始位置小于结束位置,您有一个条件显式返回-1。我相信这是一个错字,你的意思是在那里有一个&gt; 标志而不是一个&lt; 标志:

if (startPosition > endPosition) {
// Changed here --^
    return -1;

【讨论】:

  • 我切换了这个,我仍然得到 -1 作为输出。我的递归是否正确完成?我已经尝试了几个小时来完成一项任务,并且我设法获得了除此之外的所有方法。
猜你喜欢
  • 1970-01-01
  • 2010-11-02
  • 2015-05-27
  • 2020-03-28
  • 2021-06-09
  • 2021-01-28
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多