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"?
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; 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.
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.
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.
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.
Yes, it is called capacity. No, we don't usually bother about it because the two methods (12) 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