2

I've been playing around with linked lists and I want to know if it's possible to have an array of strings in a linked list node. I've been trying to get it to work the same way as the string "name", so that I can insert more strings into the array as needed

class Test1 {
    public static void main(String[] args) {
        Node n = new Node("Ace");
    }
}

class Node {
    private Node next;
    private String name;
    private String[] data;

    public Node(String s1) {
        s1 = name;
        next = null;
    }

    public String[] getArray() {
        return array;
    }

    public void setData(String s1, int point) {
        name = s1;
        array
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node nextVal) {
        next = nextVal;
    }
}
6
  • 1
    What's the problem? What did you expect to see/happen and what did you see/happened instead? Commented Nov 9, 2015 at 12:17
  • You may have a design problem here. Do you intend each Node to hold one and only one name? Or, can a given Node correspond to more than one name? Commented Nov 9, 2015 at 12:19
  • I'm trying to add array entries into the node the same way I do with the name entry and Node n= new Node(""); Ideally I'm looking for a solution that lets me enter more strings into the array once the node has been initialized I only need one name for each node as well Commented Nov 9, 2015 at 12:22
  • @Jabba, "Ideally I'm looking for a solution that lets me enter more strings into the array once the node has been initialized I only need one name for each node as well". Does this mean the you want to dynamically add strings to the array contained within the node? Commented Nov 9, 2015 at 13:14
  • 1
    You can't dynamically grow an array. Use a List for that (Vector, ArrayList, etc). Commented Nov 9, 2015 at 14:21

1 Answer 1

1

You can have a String array in your linked list, but you cannot extend an array once it is allocated.

If you want to have something extensible, you should use an ArrayList.

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

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.