0

I'm trying to give a function a list and make an array which contains the value of the nodes of the list (Not complicated). There it is:

public class MainClass {

    public static int[] makeListIntoArray(Node<Integer> n) {
        int[] arr1 = new int [countSizeOfList(n)];
        for(int i = 0; i < countSizeOfList(n); i++) {
            arr1[i] = n.getValue();
            n = n.getNext();
        }
        return arr1;
    }//EndOfFunction

    public static int[] finalFunction2(Node<Integer> n) {
        if(n == null) return ifListIsEmpty(n);
        return makeListIntoArray(n);
    }

    public static int[] ifListIsEmpty(Node<Integer> n) {
        Node<Integer> n2 = new Node<Integer>(999);
        int[] arr1 = new int [countSizeOfList(n2)];
        int i = 0;
        arr1[i] = n2.getValue();
        return arr1;
    }
    public static void main(String[] args) {

        Node<Integer> n1 = new Node<Integer>(5);
        Node<Integer> n2 = new Node<Integer>(4);
        Node<Integer> n3 = new Node<Integer>(3);
        Node<Integer> n4 = new Node<Integer>(5);
        Node<Integer> n5 = new Node<Integer>(1);
    


        n1.setNext(n2);
        n2.setNext(n3);
        n3.setNext(n4);
        n4.setNext(n5);
        
        System.out.println(finalFunction2(n1));

    }//Main

}//Class

Thing is that it prints "[I@7960847b" beside of the actual array... fixes?

Any fixes?

  • 1
    You need to insert values into the array at a specific index. The first index is always 0, so `arr1[0] = n.getValue()` and so on. Create a loop with in incrementing index. – f1sh Aug 03 '20 at 17:43
  • You need to loop from zero to the length of the list, and do something like ``arr[i] = n.getValue();``. – NomadMaker Aug 03 '20 at 17:45
  • You can create variable like `int i = 0;` which will represent position/index in array on which element should be placed. In each loop iteration it could be used like `arr[i] = n.getValue(); i = i + 1;` or even `arr[i++] = n.getValue();`. – Pshemo Aug 03 '20 at 17:46
  • But that is quite interesting, are you being taught about more advanced datastructures like linked-lists before basic arrays? – Pshemo Aug 03 '20 at 17:53
  • @Pshemo Ye of course I do, can you help? I edited my question. – Dan Volovsky Aug 05 '20 at 23:56
  • Regarding "it prints "[I@7960847b"": [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/q/29140402), [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784). – Pshemo Aug 06 '20 at 05:41
  • But aside from that, you shouldn't be using `i < countSizeOfList(n)` as loop condition because (1) it makes loop inefficient as you in each iteration need to again traverse all remaining elements which instead of O(N) makes it O(N^2) complexity (2) it will count only *remaining* nodes and since you change `n` in each iteration via `n = n.getNext();` its "size" (remaining elements) will also change and prevent you from iterating pass middle element. Instead either use `length` of array which is fixed or use something like `for(int i=0; n!=null; i++, n=n.getNext()){arr[i]=n.getValue();}` – Pshemo Aug 06 '20 at 05:41

3 Answers3

1

If you want to insert a value into array then there should be an index especially a static one. You cannot simply assign it to arr1 like your do for primitive types.

For example, arr1[0] = n.getValue() is valid but not arr1 = n.getValue();

public static int[] makeListIntoArray(Node<Integer> n) {
    int[] arr1 = new int [countSizeOfList(n)];
    int idx=0;
    while(n != null) {
        arr1[idx++] = n.getValue();
        n = n.getNext();
    }
    return arr1;
}//EndOfFunction
rootkonda
  • 1,700
  • 1
  • 6
  • 11
  • ...unless `n.getValue()` returns a `int[]`. – MC Emperor Aug 03 '20 at 17:47
  • Yes agreed. It is valid for single value int. But n.getValue() looks like it returns single value and also it is inside a loop so assuming element wise copy is being made here. – rootkonda Aug 03 '20 at 17:47
  • 1
    @MCEmperor True, but if variable defined as `Node n` for `n.getValue();` would return `int[]` then OP would have much bigger problem than asked in this question... – Pshemo Aug 03 '20 at 17:49
  • @Pshemo Yup, hope it just returns an `int`, which I assume it does. – MC Emperor Aug 03 '20 at 21:15
1

If you're using Java's built-in LinkedList data structure you can simply use the following to convert from LinkedList to array:

Integer[] array = list.toArray(new Integer[list.size()]);

So for the situation you're describing all you would need for the function is:

import java.util.LinkedList;

public class MainClass {
   public static int[] makeListIntoArray(LinkedList<Integer> list) {
       Integer[] arr = list.toArray(new Integer[list.size()]);
       int[] intArr = Arrays.stream(array).mapToInt(Integer::intValue).toArray();
       // Above line converts the wrapper Integer[] to int[] if you need that
       return intArr;
    }
}

You can find more info about LinkedList's toArray() method here.

Kel Varnsen
  • 314
  • 2
  • 8
0

If you want to keep the linked list but also want to access the elements in O(1), you can create an array of Nodes.

public class MainClass {

    public static Node<Integer>[] makeListIntoArray(Node<Integer> n) {
        Node<Integer>[] arr1 = new Node<Integer> [countSizeOfList(n)];
        int i=0;
        while(n != null) {
            arr1[i] = n;
            n = n.getNext();
            ++i;
        }
        return arr1;
    }//EndOfFunction

}//Class
underhood31
  • 115
  • 7