Print ArrayList

799,638

Solution 1

list.toString() is good enough.

The interface List does not define a contract for toString(), but the AbstractCollection base class provides a useful implementation that ArrayList inherits.

Solution 2

Add toString() method to your address class then do

System.out.println(Arrays.toString(houseAddress));

Solution 3

From what I understand you are trying to print an ArrayList of arrays and one way to display that would be

System.out.println(Arrays.deepToString(list.toArray()));

Solution 4

since you haven't provide a custom implementation for toString() method it calls the default on which is going to print the address in memory for that object

solution in your Address class override the toString() method like this

public class Address {

int addressNo ; 
....
....
...

protected String toString(){
    return Integer.toString(addressNo);
}

now when you call

houseAddress.get(i)  in the `System.out.print()` method like this

System.out.print( houseAddress.get(i) ) the toString() of the Address object will be called

Solution 5

You can simply give it as:

System.out.println("Address:" +houseAddress);

Your output will look like [address1, address2, address3]

This is because the class ArrayList or its superclass would have a toString() function overridden.

Hope this helps.

Share:
799,638
dancooper93
Author by

dancooper93

Updated on February 18, 2022

Comments

  • dancooper93
    dancooper93 over 2 years

    I have an ArrayList that contains Address objects.

    How do I print the values of this ArrayList, meaning I am printing out the contents of the Array, in this case numbers.

    I can only get it to print out the actual memory address of the array with this code:

    for(int i = 0; i < houseAddress.size(); i++) {   
        System.out.print(houseAddress.get(i));
    }  
    
    • Paul Nikonowicz
      Paul Nikonowicz over 12 years
      do you want the address location in memory?
    • dancooper93
      dancooper93 over 12 years
      sorry, i don't understand what you mean. the arraylist is storing the address pointing to the array, and i want to print the contents of the array, but I don't know how to go about it.
    • antony.trupe
      antony.trupe over 12 years
      I tried to reword the question to make it clearer.
    • Kuldeep Jain
      Kuldeep Jain over 12 years
      By Address, he mean House address. I think.
    • A.H.
      A.H. over 12 years
      @dancooper93: Just add the complete declaration of houseAddress including the generics parameter to your question.
    • dancooper93
      dancooper93 over 12 years
      houseAddress is just the name of my ArrayList, it contains an address pointing to an Array which contains the actual numbers, I want to be able to print these numbers through the ArrayList.
    • Tamás
      Tamás over 12 years
      Okay, I'm completely confused now. You say you have an ArrayList, which contains an address pointing to an Array. How did you obtain the address of the Array that you put in the ArrayList? In Java, you should not be able to get your hands on any raw pointers (at least not easily).
    • antony.trupe
      antony.trupe over 12 years
      you can't point to memory locations in Java.
    • antony.trupe
      antony.trupe over 12 years
      try including a larger code sample, that may help us infer the question.
    • Louis Wasserman
      Louis Wasserman over 12 years
      Please, just give us the declaration of the houseAddress variable, and some sample code for the elements that it contains.
    • dancooper93
      dancooper93 over 12 years
      ok, in my contructor the line to declare my ArrayList is houseAddress = new ArrayList<Numbers>(); with Numbers being the name of the Array in my other class
  • beckah
    beckah over 9 years
    This would only work for an array, not an array list. You can not provide houseAddress as an argument for Arrays.toString() because it is not of type Array.
  • 21stking
    21stking over 9 years
    @Mr.Goose i do not get your question quite clear, what do you mean?
  • Tom
    Tom about 9 years
    This is not different from OPs current code. The problem remains.
  • geniushkg
    geniushkg over 8 years
    in my opinion looping is only possible wayout here , if anyone knows better option please share.
  • Asha
    Asha almost 8 years
    The same output is print by System.out.println(list);
  • Ishan Srivastava
    Ishan Srivastava almost 7 years
    p.s. it is least controvercial because you are not doing any unnecesary conversions over here like converting ArrayList to Array and then converting it to string to print it
  • Beginner
    Beginner over 6 years
    list is houseAddress here: Arrays.toString(houseAddress.toArray())
  • compor
    compor over 5 years
    Welcome to Stack Overflow. Please review the How to Answer section and edit your question accordingly.
  • Hanif
    Hanif about 5 years
    AbstractCollection already overrides toString() to print the contents of its elements. So, just list.toString() prints the toString() of all its elements. This is true for all collections.
  • Gabriel Pellegrino
    Gabriel Pellegrino almost 5 years
    call to .toString() is redundant.
  • Brooklyn99
    Brooklyn99 about 3 years
    I donot think toString is required in this case. ArrayList overrides AbstractCollection class and hence it will take care of printing the values