Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

OCA, Chapter 3, Question 23 - array or a list

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to this question says that line 4:  "creates a fixed size array". Is this an errata? the line starts with "List<Integer> list =" which is a constructor for a an arraylist, not an array.

This is also confirmed in page 137 with the text: "merging varargs with ArrayList conversion allows you to create an ArrayList in a cool way: "
 
author
Posts: 20
6
IntelliJ IDE Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ioannis,

no, it's correct. Let me tell you why.

Have a look at the implementation of the Arrays.asList method:



"T..." is a shorthand for actually passing in a fixed sized array "T[]". So we "create a fixed size array" and simply pass it to the constructor of an ArrayList. That array is then simply stored within the ArrayList internally.

But because an ArrayList "is-a" List (it implements that interface) it's okay that the asList method returns only a reference to the List interface.

Hope this helps,
Simon

 
Enthuware Software Support
Posts: 4447
41
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing like fixed and non-fixed sized array. All arrays are of fixed size. Size of an array cannot be changed once created.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic