0

I am new to Java and writing a method for a generic Linked List that accepts another Linked List as a parameter. As I understand, this new Linked List should be of the same type as the Linked List calling the method.

So, instead of

public void insertList(MyLinkedList<AnyType> otherList, int idx) {

I should be specifying the type of otherList to match the list that calls insertList()?

list.insertList(MyLinkedList<???>, 0);

How would I do this if I don't know the type of list, since it's generic, but know that otherList needs to be the same type? I hope that makes sense. As I mentioned, I am new to Java, so if I am misunderstanding generics, please correct me. Thank you.

6
  • You're not saying you want to add the contents of a List<Integer> to a List<String>, are you? Commented Sep 15, 2017 at 20:17
  • Correct. I want to make sure that can't happen. I figure if I accept a generic MyLinkedList as a parameter, I will run into that issue. Is that true? Commented Sep 15, 2017 at 20:20
  • I'm stuck on what I should be specifying the type of otherList to match the list that calls insertList()? means. Commented Sep 15, 2017 at 20:21
  • Forgive me for my lack of clarity. I am new to Java coming from Python (and a novice programmer at that) so generics and typing to the extent it is used in Java is new to me. I know that otherList needs to match the type of list. When I write the method, I won't know the type of list, since it is generic. How can I ensure the method checks that otherList matches the type of List? Is this even required? Commented Sep 15, 2017 at 20:25
  • So where is your other list defined? Commented Sep 15, 2017 at 20:25

2 Answers 2

1

I am inferring that you are writing your own linked list and that your declaration looks something like this:

class MyLinkedList<T> {
  …

If so, the <T> means that your class has a generic type variable, T, that represents the type of the list's elements.

Given that, the insert method could look something like this:

void insertList(List<? extends T> list, int index) {
  /* Count links to find the insertion point */
  /* Remember the link that used to follow */
  …
  for(T obj : list) {
    /* Create link for obj and link it to previous */
    /* Update previous */
    …
  }
  /* Attach the old link you remembered to previous */
  …
}

Here, ? extends T means that you accept anything that extends the generic type of your list.

There's no need to require the collection of inserted elements to be MyLinkedList, or even List—it could be any type of collection that you could iterate.

Sign up to request clarification or add additional context in comments.

Comments

0

For what I understand you have sue that the object passed to the function will be a LinkedList, even if you did not know you could make use of 'instanceof' to test if the object is an instance of some type, after you can make use of "cast" resources to "transform" an generic object to another type. Se the code bellow and adapt for your use.

    public static void main(String[] args) {


        List<Banana> lista = new LinkedList<Banana>();
        testingAndCastingSomeObject(lista);

    }

    /**
     * This method will receive a generic object and test if it is a LinkedList, if it is it will cast the object "transforming" it in a LinkedList
     * Afte this I can make what I want
     * @param object
     */
    static void testingAndCastingSomeObject(Object object) {
        LinkedList<?> test = null;
        if(object instanceof LinkedList<?>) {
            test = (LinkedList<?>) object;
        }

        //If the List is not empty and the first objetct is a Banana I can cast it too
        if(!test.isEmpty() && test.get(0) instanceof Banana ) {

            Banana banana = (Banana) test.get(0);
            System.out.println("Here is my banana: " +banana);

        }

    }

    public class Banana{
        //Banana members and methods
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.