-1

私はサイトを調べましたが、少なくとも私がしていることに匹敵するものは何も見つかりませんでした (私が 1 時間探していたスタックオーバーフローの応答や質問をリンクしないでください)。この質問をしてください。ジェネリックを使用するリンクリストを作成しようとしています(いかなる種類のライブラリも使用していません)。ここに関連するすべてのコードがあります。

public class LList<T> 
{
MyNode<T> head = null;
MyNode<T> tail = null;
MyNode<T> temp = null;
int count = 0;;
Scanner scan = new Scanner(System.in);

//puts a new value at the edn of the list
public void add(T i)
{
    if(head == null)
    {
        head = new MyNode<T>();
        head.data = i;
        head.next = tail;
        tail = head;
    }
    else
    {
        tail.next = new MyNode<T>();
        tail = tail.next;
        tail.data = i;
    }
    count ++;
}

//inserts a new value at a given index
public void insert(int index, T item)
{
    if(index == size())
    {
        add(item);
    }
    else if(index == 0)
    {
        MyNode<T> temp = new MyNode<T>();
        temp.data = item;
        temp.next = head;
        head.previous = temp;
        head = temp;
        count++;
    }
    temp = head;
    for(int i = 0; i < index-1; i++)
    {
        temp = temp.next;
        MyNode<T> myNode = new MyNode<T>();
        myNode.data = item;
        myNode.next = temp.next;
        temp.next = myNode;
        count++;
    }
}

//returns the number of values in the list
public int size()
{
    return count;
}

//returns the value at a given index
public T get(int index)
{
    if(head == null || index > count -1)
    {
        return null;
    }
    if(index < 0 || index >= count)
    {
        System.out.println("This does not exist");
    }
    MyNode<T> p = head;
    int size = 0;
    while(size < index && p.next != null)
    {
        p = p.next;
        size++;
    }
    if(count != index)
    {
        return null;
    }
    else
    {
        return p.data;
    }
}

//removes the returns the first value in the list
public T remove()
{
    head = head.next;
    head.previous = null;
    count--;
    return (T) head;
}

//removes and returns the value at a given index
public T removeAt(T elem)
{
    temp = head;
    MyNode<T> two = null;
    if(head.data.equals(elem))
    {
        head = head.next;
        head.previous = null;
        count--;
        return elem;
    }
    else if(tail.data.equals(elem))
    {
        tail = tail.previous;
        tail.next = null;
        count--;
        return elem;
    }
    while(temp != null && !temp.data.equals(elem))
    {
        two = temp;
        temp = temp.next;
    }
    if(temp == null)
    {
        return null;
    }
    two.next = temp.next;
    T spare = temp.data;
    temp = null;
    count--;
    return spare;
}

//removes and returns the last value in the list
public T removeLast()
{
    temp = tail;
    tail = tail.previous;
    temp = null;
    count--;
    return (T) tail;
}

//creates a string representation of all the values in the list
public String toString()
{
    String result = null;
    for(int i = 0; i < count; i++)
    {
        result =  i + " : " + get(i).toString(); 
    }
    return result;
}

//removes all the values in the list
public void clear()
{
    for(int i = count -1; i >= 0; i++)
    {
        removeAt(i);
    }
}

//searches for a value in the list and returns the first index of that
//value when found
public int search(T find)
{
    if(head == null)
    {
        return -10;
    }
    MyNode<T> p = head;

    do
    {
        if(find.compareTo(p.data) == 0)
        {
            return 0;
        }
        else
        {
            return -1;
        }
        p = p.next;
    }while(p != null);
}

public void itemChosen(int choice, LLMenu[] menu)
{
    LLMenu m = menu[choice-1];
    switch(m)
    {
    case ADD:
        System.out.println("What value would you like to add?");
        T addThis = scan.nextInt();
        add(addThis);
        break;
    case INSERT:
        System.out.println("What index would you like to replace?");
        T replace = scan.nextInt();
        System.out.println("What number would you like to insert?");
        int val = scan.nextInt();
        insert(val, replace);
        break;
    case SIZE:
        size();
        break;
    case GET:
        System.out.println("What index would you like to look at?");
        int thisOne = scan.nextInt();
        get(thisOne);
        break;
    case REMOVE:
        remove();
        break;
    case REMOVEAT:
        System.out.println("What index would you like to remove?");
        T thisHere = scan.nextInt();
        removeAt(thisHere);
        break;
    case REMOVELAST:
        removeLast();
        break;
    case TOSTRING:
        toString();
        break;
    case CLEAR:
        clear();
        break;
    case SEARCH:
        System.out.println("What value would you like to search for?");
        T searchForMe = scan.nextInt();
        search(searchForMe);
        break;
    }
    }
 }

および MyNode:

public class MyNode<T>
{
T data;
MyNode<T> next;
MyNode<T> previous;
}

私が実際に問題を抱えているのは、ジェネリックに設定されるはずの項目をスキャンしている LList の switch ステートメントであり、明らかにスキャナーを使用してジェネリックを読み取る方法はありません。では、質問 1、これらをジェネリックに読み込んで設定するにはどうすればよいですか?質問 2、LList の clear メソッドで、ジェネリックを期待しているときに removeAt を使用するときに変数 i を送信するにはどうすればよいですか? すべての回答に関連性を持たせてください。お時間をいただきありがとうございます。

編集 do-while 内の検索メソッドにもう 1 つ質問があり、if(find.compareTo(p.data) == 0) という if ステートメントがあります。何を載せようか迷ったので、思いつくままに書いてみました。

4

2 に答える 2

2

#1。汎用コンソール入力メソッドを独自に実装できます。例を次に示します。Java 汎用コンソール入力メソッドの改善点は?

于 2013-01-28T07:59:59.357 に答える
0

あなたのコードはメソッドitemChosenにあるべきだと思います。main

T 型を宣言する必要があることは明らかです。

public void main(String[] args)
{
LList<Integer> myLList = new LList<>(); 

}

次に、必要なケース スイッチ コードを追加します。

于 2013-01-28T08:03:07.000 に答える