Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Convert Array to LinkedList

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Convert Array to LinkedList

    I'm looking at how to convert an array of int numbers to a Linked list?

    Here is my take but it doesn't work
    int[] numbers = { 1, 4, 17, 7, 25, 3, 100 };
            Arrays.sort(numbers);
            System.out.println(Arrays.toString(numbers));
     
            System.out.println(numbers.length);
            System.out.println(getAverageValue(numbers));
     
            // Convert array to list
            LinkedList<int[]> ll = new LinkedList(Arrays.asList(numbers));
            System.out.println(ll.size()); // print 1 so something wrong
            for (int i = 0; i < ll.size(); i++) {
                System.out.println(i + " = " + ll.get(i)); // print 0 = [I@3419866c
            }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert Array to LinkedList

    LinkedList<int[]> ll
    says the LinkedList contains arrays of int

    System.out.println(ll.size()); // print 1 so something wrong
    The code has only added 1 array to the List
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Convert Array to LinkedList

    Are you telling me that I perfectly converted my array into a linkedList but your point is that it is an LinkedList of an Array of Int right.

    Is that what you telling me?
    Last edited by siid14; July 18th, 2022 at 04:00 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert Array to LinkedList

    LinkedList of an Array of Int
    Yes, that is what this expression says:
    LinkedList<int[]> ll
    int[] is an array of int
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to fix error java.lang.NullPointerException in array of LinkedList?
    By elf01b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2020, 11:19 AM
  2. Replies: 1
    Last Post: December 13th, 2019, 09:19 PM
  3. convert arraylist to 2D array
    By ishrne in forum Object Oriented Programming
    Replies: 5
    Last Post: April 5th, 2013, 07:57 AM
  4. How to convert String array to Integer array...?'
    By suyog53 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 25th, 2012, 08:50 AM
  5. Replies: 4
    Last Post: November 14th, 2011, 10:00 PM

Tags for this Thread