3

I am a fairly new Java programmer and I am currently learning about converting lists to arrays. The problem that I am having is that when I try to link the list the output I am getting is not the same as what is in the array. The linked list output is all null values while the array has random values as it should. Here is my code:

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class InsertingElements {
    public static void main(String[] args) {
        Integer[] numbers = new Integer[25];
        List<Integer> linkList = new LinkedList<>(Arrays.asList(numbers));

        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = (int) (Math.random() * 100 + 1);
        }

        System.out.println("Numbers Generated: " + Arrays.toString(numbers));

        numbers = linkList.toArray(new Integer[linkList.size()]);

        System.out.println("Numbers: ");
        for (Integer number : numbers) {
            System.out.println(number);
        }
    }
}

And here is the output:

Numbers Generated: [92, 61, 25, 8, 48, 80, 85, 89, 53, 18, 48, 38, 48, 
41, 93, 94, 24, 73, 83, 21, 18, 52, 3, 14, 10]
Numbers: 
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null

3 Answers 3

16

Move List<Integer> linkList = new LinkedList<> (Arrays.asList(numbers)); to after where you populate the array otherwise your linked list is just a list of default values.

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

2 Comments

This does not work on Java 11. Wee need to use: new LinkedList<>(Arrays.asList(ArrayUtils.toObject(nums)))
@FiReTiTi It works, take a closer look, numbers is Integer[] not an int[], and all what ArrayUtils from Apache Commons does is "boxing" - converting from int[] to Integer[]
0

Try this one

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

    public class InsertingElements {
        public static void main(String[] args) {
            Integer[] numbers = new Integer[25];

            for (int i = 0; i < numbers.length; i++) {
            numbers[i] = (int) (Math.random() * 100 + 1);
           }

           List<Integer> linkList = new LinkedList<>(Arrays.asList(numbers));
           System.out.println("Numbers Generated: " + Arrays.toString(numbers));

          numbers = linkList.toArray(new Integer[linkList.size()]);

          System.out.println("Numbers: ");
          for (Integer number : numbers) {
          System.out.println(number);
        }
      }
    }

Comments

0
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

    public class InsertingElements {
        public static void main(String[] args) {
            Integer[] numbers = new Integer[25];

            for (int i = 0; i < numbers.length; i++) {
            numbers[i] = (int) (Math.random() * 100 + 1);
           }

           List<Integer> linkList = new LinkedList<>(Arrays.asList(numbers));
           System.out.println("Numbers Generated: " + Arrays.toString(numbers));

          numbers = linkList.toArray(new Integer[linkList.size()]);

          System.out.println("Numbers: ");
          for (Integer number : numbers) {
          System.out.println(number);
        }
      }
    }

1 Comment

Just an FYI. When we create a list using a constructor that accepts an array, the list internally iterates the array and adds elements to the list. The list object doesn't hold the reference of the array once the constructor block is executed. That's why @michael 's code is not working.

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.