Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Automatic expansion of arrays?

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must have missed this in K&B. the following code is run with the command: java ArrayTest 0 1 2


What I see here is an array of 3 elements being pushed into an array that was defined with 2 elements. Do you agree? This code compiles, runs and prints 012012.
Am I reading this correctly? If so does this mean that an array will "auto size" itself to accespt all of the elements of another array being assigned to it?
Thanks
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you missed this: s=args;

Mind you, I had to blink twice before I saw it.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short:
s is just a reference-variable of type "String-Array" and as such it can point to String-Arrays of any size. First you let it reference an Array of size 2 and after that to an array of size 3. Ther is nothing like "auto-size" as s denotes just a reference-variable and not an array!

In long:
https://coderanch.com/t/263017/java-programmer-SCJP/certification/Arrays
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just in case the OP was under the impression that the following...

s=args;

does an element by element assignment, it does not. To do a copy of the elements of the array into another array, you should use the System.arraycopy() method.

Henry
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
Just in case the OP was under the impression that the following...

s=args;

does an element by element assignment, it does not. To do a copy of the elements of the array into another array, you should use the System.arraycopy() method.

Henry



Or to put it differently again:

defines an object reference which is able to point at String arrays, and at time of creation it happens to point at an String array object with a dimension of 2. Then the object to which s is pointing is changed by this line:

Now args is a String array as well, and since s was defined to point at String arrays, the assignment is correct (the original String array is likely to be gc-fodder), and the program runs happily.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The Arrays are not like Collections wherein they are dynamically varying (able to grow/shrink) nature. (Though, the code internally does it explicitly).

As others told, what you are doing here is just making the String reference variable s to point to a * different * array and now its of args, which was previously to the array created through String[2].

Just reassigning the same variable to point to the * different object *.

HtH.
[ June 11, 2007: Message edited by: Raghavan Muthu ]
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your great input. I completely missinterpreted the statement " s=args", in that this was a reasignment of s.

This is such a great forum...
thangs again
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic