<< Chapter < Page Chapter >> Page >

The size issue

Here is some of what the Oracle documentation for the LinkedList class has to say about the size issue for this version of the toArray method.

"If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specifiedarray and the size of this list. If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), theelement in the array immediately following the end of the collection is set to null. This is useful in determining the length of the list only if thecaller knows that the list does not contain any null elements."

So, what did I do?

Knowing all of this in advance, I purposely caused the size of the JComponent array to be larger (by two elements) than the number of elements in the collection. Therefore, the array that I passed to the toArray method was populated and a reference to that populated array was returned.

(Had my array been smaller than the number of elements in the collection, the toArray method would have created and populated a new array of typeJComponent and would have returned a reference to that new array object. In that case, my array would have been used by the toArray method only for the purposeof determining the runtime type of my array.)

More information from Oracle

Here is some additional information about the toArray method provided by the Oracle documentation for the Collection interface:

"If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in thesame order."

Because the iterator for a LinkedList object returns the elements in increasing index order, the toArray method, in this case, copies the element at each index position in the collection into the element at the sameindex position in the array. Thus, reference values are copied from each element in the collection into the first six elements in the array.

The output

The output produced by the code in Listing 5 is shown below:

New array contents B0 B1 L2 B3 B4 L5 null 17

You will note that the first six elements in the array match the six elements in the collection (the initial values placed in the array earlier when the array was instantiated have been overwritten).

You will also note that the value of the seventh element in the array (index value 6) has been overwritten with a null reference.

Demonstrates same array was used

Note finally that the last element in the array was not overwritten. It still contains the value placed there when the array object was instantiated. Thisdemonstrates that the array that I passed to the toArray method was populated with the collection data, and a reference to that array was returnedby the toArray method.

What if the array was too small?

Had my array been too small, it would have been discarded by the toArray method. The toArray method would have created and populated a new array object of the correct size and runtime type, and would have returned a referenceto that new array.

Questions & Answers

what are two classical macroeconomics and what're their theories say about their equations?
what is the formula for calculating elasticity
aza Reply
what is elasticity of demand?
Rita Reply
Causes of economic growth
pierre Reply
What is elasticity of demand
What are the causes of economic growth
economic growth, establishment of industry, encourage of investor's, farm productivities, creation of institutions, construction of good road etc
elasticity of demand can be said to be the responsiveness of demand to a change in prices
impact of collusion in the economy referring to inefficiencies illustrated by means of graph
nondumiso Reply
The Factor price will determine the choice of techniques to produce.Expantiate
what is elasticity of demand?
Etta Reply
state and explain two types of demand
Institution involved in money market
Gande Reply
what is Economics
Kwame Reply
Economic is the study of scarcity
Economics is the study of a lot of things. It is split up into two areas of study, Microeconomics and Macroeconomics. Microeconomics is the study of an individual's choices in the economy and Macroeconomics is the study of the economy as a whole.
Economics is a science that studies human scarcity
What is Equilibrium price?
Equilibrium is the market clearing price. The point at which quantity demanded equals quantity supplied. The point at which the supply and demand curves intersect.
Equilibrium price*
Refers to the study of how producers use limited resources to satisfy human unlimited wants
why is economics important
Derrick Reply
What will you do as a consumer if you are not at equilibrium?
chukwu Reply
am new I will like to know about the graph relationship
Gloria Reply
comment on WTO principle on trading system. trade without discrimination
Omben Reply
optimize z=f(x,y)=6x²-9x-3xy-7y+5y²
Alex Reply
What is an indifference curve?
layla Reply
different levels of utilities of a person in a given set of bundles of goods
identify and quantify five social costs and social benefits of building a school
Mokgobo Reply
identify and quantity five social costs and social benefits of building a hospital
short run vs long run
state the law of diminishing return?
The Law of Diminishing (Marginal) Returns simply states that at some point in time a business/operation/etc.'s increased productivity will begin to decline.
For example, if a small pizza shop currently has 3 workers in the kitchen at any given time,and hiring 1 more worker will increase productivity, at some number of workers hired will the business see a decrease in productivity because the capital resources that the pizza shop has is not infinite.
Five social benefits of building a hospital, in my opinion and depending on where it's built, would be 1) Increased care for neighboring residents, 2) Potential jobs for individuals, 3) May decrease the travel time residents need to endure in order to reach the nearest hospital
4) May create work-study programs for individuals who aspire to be future Doctors, Nurses, Physicians, etc. 5) Assuming there are local pharmaceutical businesses nearby, the hospital may decide to purchase supplies local, increasing the business' sales. Thus, generating more income.
5 costs of building a hospital would be 1) Increased noise and waste pollution from service vehicles and hospital visitors, 2) May require large amounts of space, possibly jeopardizing nearby animal habitats, 3) May see an increase in traffic and possibly car accidents from frantic individuals
racing to see their injured friends, family members, etc. 4) Constructing a hospital and hiring staff is very expensive 5) To use funds, private or public, to finance the construction of a hospital cannot be used to fund any other projects. (The concept of opportunity costs.)
what is meant by inteference with the price mechanism operation?
We use a Supply and Demand graph to illustrate at what price level will the market for a certain good or service be at equilibrium. If the price for a good or service is set too high, consumers will be less inclined to buy that product Thus, creating a surplus.
This surplus will eventually drive the price back down to it's equilibrium point. Similarly, if a price for a good or service is set too low, individuals would be more inclined to buy more of a certain product, creating a shortage. This shortage will cause sellers to drive the price back up to the
equilibrium point.
in a comparison of the stages of meiosis to the stage of mitosis, which stages are unique to meiosis and which stages have the same event in botg meiosis and mitosis
Leah Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?