Question

So the concept of my question is: Let's say we have nodes. Each node has an array of integers. Now, we must add an integer to end of the array. How do we do that?

Here's what I've done so far:

Created class Node:

public class Node {
private int[] data;

Node next;

public Node(int n, Node nxt) {
    data = new int[n];
    next = nxt;
}

}

Then the dynamic array list class:

public class DynamicArrayOfInts {

private Node head = null; 
private int numOfElementsPerNode = 0;

public DynamicArrayOfInts(int elementsPerNode) {
    numOfElementsPerNode = elementsPerNode;
}

public void add(int e) {

}
}
Was it helpful?

Solution

You should add an attribute in the Node class to know the current index you are for the current Node's array. I would also add an attribute in your DynamicArrayOfInts to keep a reference for the current node.

Then in your add method, check if the array that the current node have is not full (it can be done easily because you know the value of the index and the number of elements per node).

If it's not the case (or if the head is null for the first add call), create a new node and add the element in its array, otherwise just fill the next slot of the array for the current node.

This is how I would implement it.

class DynamicArrayOfInts {
    private Node head, current; 
    private int numOfElementsPerNode;

    public DynamicArrayOfInts(int elementsPerNode) {
        if(elementsPerNode <= 0) 
            throw new IllegalArgumentException("elementsPerNode must be > 0");
        numOfElementsPerNode = elementsPerNode;
    }

    public void add(int e) {
        if(head == null){
            head = new Node(numOfElementsPerNode, null);
            head.data[head.index++] = e;
            current = head;
            return;
        }
        if(current.index == numOfElementsPerNode){
            Node n = new Node(numOfElementsPerNode, null);
            current.next = n;
            current = n;
        }
        current.data[current.index++] = e;
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        Node n = head;
        while(n != null){
            sb.append(Arrays.toString(n.data));
            n = n.next;
        }
        return sb.toString();
    }

    private static class Node {
        private int[] data;
        private int index;
        private Node next;

        public Node(int n, Node nxt) {
            data = new int[n];
            index = 0;
            next = nxt;
        }
    }
}

A small main to show how it behaves:

public static void main(String[] args){
    int[] toAdd = {5,7,10,-1};
    DynamicArrayOfInts d = new DynamicArrayOfInts(2);
    for(int i : toAdd){
        d.add(i);
        System.out.println(d);
    }
} 

Output:

[5, 0]
[5, 7]
[5, 7][10, 0]
[5, 7][10, -1]

OTHER TIPS

Unfortunately you have to create a new array in this case (yes, I feel your pain) which will have one more element than data. You will have to copy the content of data into your new array and set the last element to your int value. This is not too elegant, this is why Mureinik suggested that you should use an ArrayList of Node (ArrayList<Node>) instead. I can even enhance his suggestion to make your solution more general and tell you to use an AbstractList<Node> instead and instantiate it with ArrayList, but this might be too advanced compared to your current level (no offence, we all have been there). As of your exact question, I imagine a method like the following in your Node class to deal with this problem.

public void push(int newValue) {
    int[] newData = new int[data.length + 1];
    for (int i = 0; i < data.length; i++) {
        newData[i] = data[i];
    }
    newData[data.length] = newValue;
    data = newData;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top