Print ArrayList
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.
dancooper93
Updated on February 18, 2022Comments
-
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 over 12 yearsdo you want the address location in memory?
-
dancooper93 over 12 yearssorry, 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 over 12 yearsI tried to reword the question to make it clearer.
-
Kuldeep Jain over 12 yearsBy Address, he mean House address. I think.
-
A.H. over 12 years@dancooper93: Just add the complete declaration of
houseAddress
including the generics parameter to your question. -
dancooper93 over 12 yearshouseAddress 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 over 12 yearsOkay, 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 theArrayList
? In Java, you should not be able to get your hands on any raw pointers (at least not easily). -
antony.trupe over 12 yearsyou can't point to memory locations in Java.
-
antony.trupe over 12 yearstry including a larger code sample, that may help us infer the question.
-
Louis Wasserman over 12 yearsPlease, just give us the declaration of the
houseAddress
variable, and some sample code for the elements that it contains. -
dancooper93 over 12 yearsok, 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 over 9 yearsThis would only work for an array, not an array list. You can not provide
houseAddress
as an argument forArrays.toString()
because it is not of type Array. -
21stking over 9 years@Mr.Goose i do not get your question quite clear, what do you mean?
-
Tom about 9 yearsThis is not different from OPs current code. The problem remains.
-
geniushkg over 8 yearsin my opinion looping is only possible wayout here , if anyone knows better option please share.
-
Asha almost 8 yearsThe same output is print by System.out.println(list);
-
Ishan Srivastava almost 7 yearsp.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 over 6 yearslist is houseAddress here:
Arrays.toString(houseAddress.toArray())
-
compor over 5 yearsWelcome to Stack Overflow. Please review the How to Answer section and edit your question accordingly.
-
Hanif about 5 yearsAbstractCollection already overrides
toString()
to print the contents of its elements. So, justlist.toString()
prints thetoString()
of all its elements. This is true for all collections. -
Gabriel Pellegrino almost 5 yearscall to .toString() is redundant.
-
Brooklyn99 about 3 yearsI donot think toString is required in this case. ArrayList overrides AbstractCollection class and hence it will take care of printing the values