-5

I am trying to create an array of linked lists The program I am trying to write has a string array of names, however some names are in the same position of the array. For example if the name morgan is in array[0] and john is also in array[0] how can I create a linked list that allows both of the names to be stored in the same index. I am trying to replicate separate chaining collision resolution.

Is it possible to use LinkedList<String> inkedlist = new LinkedList();

If I use that how can I create a new linked list for each array index?

for(int i = 0; i < array.length; i++){
            array[i] = new LinkedList();
        }
Morgan Henry
  • 5
  • 1
  • 4
  • Well, you've got a `LinkedList` class. What's stopping you from making an array of `LinkedList`s? – Kevin Anderson Jun 19 '17 at 02:35
  • Your question is not clear! – lihongxu Jun 19 '17 at 02:57
  • Elaborate, what do I need to be clear about? – Morgan Henry Jun 19 '17 at 03:03
  • From what I understand. You don't need to do much... Just keep adding the names to the list accessing the same array location. Suppose you have a name in arr[0]. Create a new node for the list at arr[0] and just add another name to that list accordingly. – Ugnes Jun 19 '17 at 03:19
  • Similar questions: [Array of Generic List](https://stackoverflow.com/questions/7810074/array-of-generic-list) and [Java 1.6: Creating an array of List](https://stackoverflow.com/questions/5662394/java-1-6-creating-an-array-of-listt). Have a look. – Ole V.V. Jun 19 '17 at 03:56

2 Answers2

1

I don't know if I understand your question. Here is your question. you are trying to create an array of linked lists The program has 'a string array' of names,

: You have String name in the node if you add correctly, you will get the linkedlist.

however some names are in the 'same position' of the array. For example if the name morgan is in array[0] and john is also in array[0] how can I create a linked list that allows both of the names to be stored in the same index.

: How about adding one more Sting in the object, it that what you want?

class Node{
    String firstName;
    String secondName;
    Node next = null;
}

Not sure if I started correctly but this is what I have so far

: You should start with a constructor first and make add, delete, and other functions.

public Class CustomList{

    private CustomList head;
    private int index = 0;


    private Class Node{

        private String firstName;
        private String secondName;
        private Node next;

**constructor**
        public Node(String firstName, String secondName){
            this.firstName =  firstName;
            this.secondName = secondName;
            next = null;
        }

    }

**constructor**
    public CustomList(){
        head = null;
    }
}

The above will contain two String values in the same index of CustomList. If I understood the question wrong or you have any more question feel free to ask.

GentleCoder
  • 96
  • 1
  • 5
  • Thank you, the only problem is, there may not always be just 2 names. 100 names could index to the same position – Morgan Henry Jun 19 '17 at 04:10
  • In that case, make a list instead of two name values so each node contains a list of string which can be any arbitrary numbers of names. declare ArrayList inside of that if you can otherwise String array works as well. – GentleCoder Jun 19 '17 at 04:43
0

It’s not as straightforward as it sounds, but yes, you can use java.util.LinkedList.

It requires that you declare your array an array of LinkedList or just List (not an array of String). This works:

    List<String>[] array = new List[1000];

    for (int i = 0; i < array.length; i++){
        array[i] = new LinkedList<>();
    }

However, where I instantiate the array, I get a warning: Type safety: The expression of type List[] needs unchecked conversion to conform to List<String>[]. One would have expected new List<String>[1000] to work, but it doesn’t. It gives an error: Cannot create a generic array of List<String>. The element type of an array cannot be a generic type. It’s a peculiarity with historic reasons, we’ll just have to live with it.

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117