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 11 of 11

Thread: Printing ArrayList

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

    Default Printing ArrayList

    I'm looking for a way to print an Array List. I know I can use a loop but I do not want to use it. I'm looking for others way, I tried with println() and toString() but it does not return me the value of the Array List. See code below

          int[] numbers = { 1, 4, 6, 7, 6, 2 };
            System.out.println(Arrays.toString(numbers)); // print [1, 4, 6, 7, 6, 2] 
     
            List<int[]> numbersList = Arrays.asList(numbers); 
            System.out.println(numbersList); // print [[I@379619aa]
            System.out.println(numbersList.toString()); // print [[I@379619aa]
    Last edited by siid14; July 1st, 2022 at 03:14 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing ArrayList

    way to print an Array List.
    Are you asking about the ArrayList class? Where is the ArrayList variable in the posted code?
    If not, please explain what variable's contents you what to print.

    Note: The numbersList List contains arrays of int, not int.
    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: Printing ArrayList

    I'm trying to print [1, 4, 6, 7, 6, 2] from my ArrayList variable which is numbersList

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing ArrayList

    The List numbersList contain int arrays. To print the contents of an array you need to get it from the List first.

    Note: numbersList is defined as a List, not as an ArrayList.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Printing ArrayList

    So technically, I haven't converted my array to an ArrayList that is what you are saying right? I got this "way" to convert from this: https://www.geeksforgeeks.org/conver...ylist-in-java/ - I'm kinda surprise tho
    Last edited by siid14; July 1st, 2022 at 07:45 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing ArrayList

    I haven't converted my array to an ArrayList
    Technically the asList method returns a List, not an ArrayList.
    What the asList method has done is create a List with one item (an int array)
    Print out the size of numbersList to see how many items it contains.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Printing ArrayList

    You are right so what about this case?

    String[] geeks = { "Rahul", "Utkarsh",
                    "Shubham", "Neelam" };
     
            // Conversion of array to ArrayList
            // using Arrays.asList
            List al = Arrays.asList(geeks); 
     
            System.out.println(al); // print [Rahul, Utkarsh, Shubham, Neelam]

    Took this code from https://www.geeksforgeeks.org/conver...ylist-in-java/

    Heree, size is 4. But for my code with int[]number, size is 1 after using method asList.
    What is the difference between both code?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing ArrayList

    What is the difference
    int is a primitive, String is an object
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: Printing ArrayList

    Converting a set of integers to a list
    import java.util.Arrays;
    import java.util.List;
     
    public class Main{ 
      public static void main(String[] args) {
     
        Integer[] myData = {2,4,6,8,10};
     
        // convert to List ADT
        List<Integer> myList = Arrays.asList(myData);
     
        // display results
        System.out.println(
          String.format("myList contains %s elements\n%s", myList.size(), myList)
        );
      }//main
    } // Main

    Output:
    myList contains 5 elements
    [2, 4, 6, 8, 10]

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

    Default Re: Printing ArrayList

    @AngleWyrm, where could i get more ressource about the format of the code you use on the last line to learn?
    Seems very useful

  11. #11
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: Printing ArrayList

    Java String.format specifiers

Similar Threads

  1. [SOLVED] Printing an arraylist of objects in JoptionPane?
    By VikingCoder in forum Java Theory & Questions
    Replies: 5
    Last Post: November 15th, 2012, 06:01 PM
  2. advice needed on storing string into arraylist and printing it out.
    By jong2009 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 23rd, 2012, 10:16 AM
  3. [SOLVED] Printing arraylist contents in Jtextarea
    By Johnny Bravo in forum AWT / Java Swing
    Replies: 4
    Last Post: August 31st, 2012, 08:56 AM
  4. [SOLVED] Printing an ArrayList of user-defined Objects
    By er1111 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 2nd, 2012, 11:06 AM
  5. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM

Tags for this Thread