• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

ArrayList Print question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this program, System.out.println(list); statement is printing the elements's values in list. My question is, isn't list a reference variable to ArrayList? Is it not supposed to print reference value of "list"?
 
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would print the reference as defined in Object.toString() unless overridden by ArrayList's own toString() method, which it appears to have done.
 
milind k das
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for response. I have another question.

We have Array which can store object of a class type.

for example: City[] cities = new City[10]; on a City class.

And arraylist is. ArrayList<City> cities = new ArrayList<>(); While arraylist initiates size at 0.

Why is separate ArrayList needed while Arrays themselves can work well? And what is need for conversion of Arrays to ArrayList and ArrayList to Array using Collections class? I don't know nothing about collections or generics yet. Please explain in simple terms.
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList keeps track of two "sizes", one is the number of entries in the List which you can get from the size() method, and the other is the "capacity" (hmm, think I got the name right) which is how much space is internally allocated to store the list elements. The capacity is not something you should care too much about, but size() is used all the time. When you initially create a List its size is zero because you haven't added anything to the list yet.

Please note:
ArrayList<String> list = new ArrayList<>();

Should be written as:
List<String> list = new ArrayList<>();
Because List is a higher level of abstraction than an ArrayList. If you use List then at any time you could substitute a different kind of List (e.g. LinkedList) and nothing should break. This holds true for declaring the parameters passed into a method, use List not ArrayList.
 
Marshal
Posts: 26456
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList has so many useful features which an array doesn't have. An array, you have to decide in advance how many entries it's going to have, and then you can't change the size. And then all of the entries are initialized to null, which can trip you up if you don't fill the array completely with objects. (I wouldn't use "works well" to describe that.)

Whereas an ArrayList, you can add entries to it and it gets bigger, and you can remove entries from it and it gets smaller. It's so much more flexible that when I'm writing programs I only create an array if I'm going to pass it to some method which requires an array. Which very rarely happens.

And like I said, sometimes you have to have an array. But maybe it was way more convenient to build an ArrayList which contains the data. That's why you need a tool to convert it to an array.

As a beginning programmer you're going to be taught to use arrays, because those are basic things that have been in computer languages for decades now. But when you need to write real programs, you aren't going to use arrays much. It's sort of like how they teach you to ride a bicycle before you can learn to drive a car -- well, no, they don't do that. It's more like you have to use a hand-powered saw before you can be trusted with a power saw, let's say.
 
Marshal
Posts: 72406
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is called capacity. No, we don't usually bother about it because the two methods (1 2) for manipulating the capacity aren't in the List interface, and looking for the getCapacity() method is a bit of a waste of time; there isn't one
I wish a few drivers were forced to ride a bicycle. I know they are a minority, but their driving makes me throw lots of exceptions whenever they pass me. What the analogy does show, however, is that there are purposes for different types of journey. There are journeys easiest walked, some cycled most easily, some most readily made by car, some by train and some by car. Not that any of those would work well whenever I go to Holland; I need a ship (or an aeroplane). You decide what is the best, but as you say, most data structures are better to use than plain simple arrays.
I am not sure about the saw analogy; arrays are a bit like using a table knife. It is very good and fast for what it is good and fast for, but as soon as you move out of that realm you find it doesn't work at all well any more. I have been quite happy to use a handsaw for everything, including, most recently, cutting down a Rhododendron which had gone the way of all flesh. It would have taken hours and hours with a table knife, and would have been impossible had the wood not gone brittle with age.
 
Yup, yup, yup. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic