Java – lists implementations that do not resolve casts

I created my own array - based list implementation, and it doesn't limit invalid parameters If I use cast to create mylist < string > mylist = new mylist < string > (), it will still accept all other parameters (int, float, double, etc.) If all data types to be accepted are specified, how can I solve this problem; If no data type is specified, I hope it works now

This is my code:

public class MyList <T> implements MyListInterface
{

    private Object[] contents;
    private int size;

    public MyList()
    {
        this(10);
    }

    public MyList(int length)
    {
        contents = new Object[length];
        size = 0;
    }

    private void alterArraySize(int value)
    {
        int len = 0;

        //Value is 1 shrink array; value is 2 then double it
        switch (value)
        {
            case 1:
                len = contents.length / 2;
                break;
            case 2:
                len = contents.length * 2;
                break;
        }

        Object[] copyArr = new Object[len];

        //Copy array
        for (int i = 0; i < size; i++)
        {
            copyArr[i] = contents[i];
        }

        contents = copyArr;
    }

    public <T> boolean insertHead(T newEntry)
    {

        size++;

        if ((size + 1) == contents.length)
            alterArraySize(2);

        //Shift elements up one
        for (int i = size; i >= 0; i--)
            contents[i + 1] = contents[i];

        contents[0] = newEntry;

        return true;
    }


    public <T> boolean insertTail(T newEntry)
    {
        //If the number of items in the list
        if ((size + 1) == contents.length)
            alterArraySize(2);

        //Put the newEntry in the last slot in the array
        contents[size++] = newEntry;

        return true;
    }

    public <T> Object deleteHead()
    {
        //Set temp to first item in the array
        Object temp = contents[0];

        //Delete the first item
        contents[0] = null;

        //Shift all items in the list down one position
        for (int i = 1; i < size; i++)
        {
            contents[i - 1] = contents[i];
        }

        //Update size to accommodate for the item deletion
        size--;
        return temp;
    }


    public <T> Object deleteTail()
    {
        //Set temp to last item in array
        Object temp = contents[--size];

        //Delete the last item
        contents[size] = null;

        //Resize if the number of items in the list is half the length
        if (size <= contents.length / 2)
            alterArraySize(1);

        return temp;
    }


    public void display()
    {
        for (int i = 0; i < size; i++)
            System.out.println(contents[i]);
    }


    public <T> int contains(T anEntry)
    {
        for (int i = 0; i < size; i++)
        {
            if (contents[i].equals(anEntry))
                return ++i;
        }

        return 0;
    }


    public boolean isEmpty()
    {
        return size == 0;
    }


    public boolean isFull()
    {
        //List can't be full
        return size == contents.length;
    }

    public <T> Object get(int givenPosition)
    {
        if ((givenPosition >= 1) && (givenPosition <= size))
            return contents[givenPosition - 1];

        return null;
    }

    public <T> void set(T s,int givenPosition)
    {
        contents[givenPosition - 1] = s;
    }

    public <T> Object remove(int givenPosition)
    {
        Object temp = null;

        //Check if givenPosition is valid and shift elements after remove
        if ((givenPosition >= 1) && (givenPosition <= size))
        {
            temp = contents[givenPosition - 1];
            for (int i = givenPosition; i < size; i++)
            {
                contents[i - 1] = contents[i];
            }

            contents[size--] = null;
        }

        return temp;
    }

    public int size()
    {
        return size;
    }

}

Solution

All of your methods are generic in themselves Therefore, they have their own type parameters, independent of the type parameters declared by your class The following method statements are generic:

public <T> boolean insertHead(T newEntry)

Therefore, regardless of the parameterized instance of the class you create, the method will accept any parameter type, and the type T of this method will be inferred as that type To solve this problem, you should delete a part of < T > each method For example, change the above method to:

public boolean insertHead(T newEntry)

In addition, your method should have the return type of t instead of object, otherwise it will fail on the caller side Change method:

public <T> Object get(int givenPosition)
{
    if ((givenPosition >= 1) && (givenPosition <= size))
        return contents[givenPosition - 1];

    return null;
}

To:

public T get(int givenPosition)
{
    if ((givenPosition >= 1) && (givenPosition <= size))
        return (T) contents[givenPosition - 1];  // Add cast

    return null;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>