arraylist question

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

arraylist question

Post by OCAJO1 »

I was looking around for ArrayList manipulation examples and came across this code. I get all of it, except for the fact that why the last w2 assignment stays the same and not pickup the new ws1 String object. Some hint(s) please? Thanks

Code: Select all

        ArrayList <String> mylArray = new ArrayList<>();
        myArray.add("coke");
        myArray.add("pepsi");
        myArray.add("mountain dew");
        
        System.out.println("Total ArrayList: "+myArray);
        
        String[] ws1 = new String[myArray.size()];
        String[] ws2 = myArray.toArray(ws1);
        
        System.out.println("ws1 = ws2: "+(ws1 == ws2)); //true
        System.out.println("ws1: "+Arrays.toString(ws1));
        System.out.println("ws2: "+Arrays.toString(ws2));
        
        ws1 = new String[1];
        ws1[0] = "filler";
        ws2 = myArray.toArray(ws1);
        
        System.out.println("ws1 = ws2: "+(ws1 == ws2)); //false
        System.out.println("ws1: "+Arrays.toString(ws1));
        System.out.println("ws2: "+Arrays.toString(ws2));

admin
Site Admin
Posts: 10064
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 352, Sec. 12.4.1 - arraylist-and-collections]

Post by admin »

I am sorry, I couldn't understand your question.
Also, please create a new topic for a new question if it is not about a topic in the book.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: arraylist question

Post by OCAJO1 »

I was asking how come the last System.out.println("ws2: "+Arrays.toString(ws2)); does not print filler?

admin
Site Admin
Posts: 10064
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: arraylist question

Post by admin »

Where did you get this code from? What is their explanation? Did you try reading the JavaDoc description of toArray method?
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: arraylist question

Post by OCAJO1 »

I had googled for ArrayList use examples in Java, to see what other examples are out there in case likes of them show up on the exam. An entry from some blog caught my eye asking how to convert ArrayList to array without writing my own code. This was the example left on the blog as an answer. Anyway when I ran it, I did not expect the last line to print what it did.

As for java docs, I read the following in the quotes, but not sure if variation of the underlined part is at work here. Variation being, since the new ws1 is smaller, the toArray instead of allocating a new array at run time, it went back and allocated an array with original size of ws1 - or is it that size of the original ws1 has nothing to do with it, and just happens to be the same size of the ArrayList?
toArray
<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)

admin
Site Admin
Posts: 10064
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: arraylist question

Post by admin »

Please read the API description carefully. The values contained in the array that is passed as an argument to toArray have no role to play here. It doesn't matter what ws1 contains. The array returned by myArray.toArray(ws1) will contain the values present in myArray. Not in ws1!
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: arraylist question

Post by OCAJO1 »

So, toArray acts on the contents of the ArrayList, and ws1 just happen to be a string that was populated by the values of that ArrayList. Nothing more.

Post Reply

Who is online

Users browsing this forum: No registered users and 94 guests