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?

7
  • 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. Commented Aug 3, 2020 at 17:43
  • You need to loop from zero to the length of the list, and do something like arr[i] = n.getValue();. Commented Aug 3, 2020 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();. Commented Aug 3, 2020 at 17:46
  • But that is quite interesting, are you being taught about more advanced datastructures like linked-lists before basic arrays? Commented Aug 3, 2020 at 17:53
  • @Pshemo Ye of course I do, can you help? I edited my question. Commented Aug 5, 2020 at 23:56

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

4 Comments

...unless n.getValue() returns a int[].
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.
@MCEmperor True, but if variable defined as Node<Integer> n for n.getValue(); would return int[] then OP would have much bigger problem than asked in this question...
@Pshemo Yup, hope it just returns an int, which I assume it does.
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.

Comments

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

2 Comments

This doesn't compile.
You're working with an array of the wrapper Integer class but returning the primitive int array type.

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.