[program-java] Re: Java: Redimensioning my existing array.

  • From: Steve Meacham <program-java@xxxxxxxxxxxxxxx>
  • To: program-java@xxxxxxxxxxxxx
  • Date: Fri, 3 Sep 2010 11:42:16 -0500

Please allow me to offer a didactic review of what Java arrays actually
are.  This will be a little bit long, so wait to read it until you have time
to go step-by-step through what would be about a whole page in a textbook.

// This is a single, primitive, double
double d;  // no jokes, guys

// This is a single dimensional array of primitive doubles
double[] ad;

// This is a single dimensional array of single dimensional arrays of
doubles, which can be used to simulate a two dimensional array
double[][] aad;

// This is a single dimensional array of single dimensional arrays of single
dimensional arrays of doubles, which can be used to simulate a three
dimensional array
double[][][] aaad;

Java only supports single-dimensional arrays.  I stopped with three, but
most Java implementations will allow you to go up to 255 levels deep like
this.  I don't recommend it.  Each single dimensional array allows from zero
to Integer.MAX_VALUE elements.

Please note that it isn't until you reserve the memory that the lengths are
specified.  This allows a lot of flexibility, such as not knowing how bit to
make the arrays until run-time.  Now, let's reserve memory for the
two-dimensional array in the most primitive way.

aad = new double[5][]; // This creates an array of five arrays of doubles
aad[0] = new double[10]; // The creates the first one, an array of ten
doubles
aad[1] = new double[2]; // The creates the second one, an array of two
doubles
aad[2] = new double[6]; // The creates the third one, an array of six
doubles
aad[3] = new double[0]; // The creates the fourth one, an array of zero
doubles
aad[4] = new double[5]; // The creates the fifth one, an array of five
doubles

Please also note that each of these sizes could have been done with a
variable and not known until run-time.  Try to do THAT in FORTRAN!  Of
course, Java offers what I call "syntactic sugar" for when all the
dimensions are the same:

// This creates an array of five arrays of doubles.  It also creates five
arrays of ten doubles each.
aad = new double[5][10];

This is good and bad at the same time.  It is good because it is flexible
and powerful.  Bad, because not only is it confusing to just about everybody
(no, you're not alone), but because it slows things down a bit.  In most
languages finding the value stored in aad[2][5] is as simple and fast as
adding two times five time the size of a double to the starting address of
the multi-dimensional array.  In most languages, most or all of this can be
done at compile time, making it into a simple memory de-reference.  In Java,
however, the nested arrays must be traversed at runtime, one at a time.
Finding add[2][5] become "retrieve and de-reference the value at add[2];
then find the value at add[5]."  Arrays of Objects, rather than primitives
makes this even worse.

Because of this, arrays are very unique and special-purposed in Java.  It's
not like in Fortran where, if you can't do it in with array, it isn't worth
doing.  I'm not sure what problem you're trying to solve, Jim, or what
domain you're trying to represent, but in Java, using nested-arrays is
rarely the best solution.  The main places I find them to be appropriate is
in graphics, cryptography, and other very specialized applications.  In
business application programming, not so much.  There, the Collection
Classes are the main containers for multiple instances of related same-type
data, and Classes are the main containers for related but different-type
data.

Steve

On Fri, Sep 3, 2010 at 10:26 AM, Willem <dwillemv@xxxxxxxxx> wrote:

>  Arrays are by design not resizable. You need to copy the data to a bigger
> array.
>
> This is what ArrayList does behind the seens, and linkedList does the same
> for adding nodes.
> On 9/3/2010 4:24 PM, Homme, James wrote:
>
>  Hi Guru,
>
> Maybe you can adapt the code about dynamic arrays on this page. It says
> Java can't really do dynamic arrays, but talks about a way to get around the
> problem. http://math.hws.edu/javanotes/c7/s3.html
>
>
>
> Jim
>
>
>
> Jim Homme,
>
> Usability Services,
>
> Phone: 412-544-1810. Skype: jim.homme
>
> Internal recipients,  Read my accessibility 
> blog<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx>.
> Discuss accessibility 
> here<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx>.
> Accessibility Wiki: Breaking news and accessibility 
> advice<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>
>
>
>
> *From:* program-java-bounce@xxxxxxxxxxxxx [
> mailto:program-java-bounce@xxxxxxxxxxxxx<program-java-bounce@xxxxxxxxxxxxx>]
> *On Behalf Of *Corbett, James
> *Sent:* Friday, September 03, 2010 9:40 AM
> *To:* 'program-java@xxxxxxxxxxxxx'
> *Subject:* [program-java] Java: Redimensioning my existing array.
>
>
>
> Hello all:
>
>
>
> I have an array of type double….
>
>
>
> Double[][] myDoubleArray = new double[2][13];
>
>
>
> …I now need to add an additional field…. Should I redimension the array
> like double[2][13][5] or should I invest in either an arrayList or HashMap?
>
>
>
> Jim
>
>
>
> James M. Corbett
>
>
>
> Programmer / Analyst |
>
> Canada Revenue Agency | Agence du revenue du Canada
>
> 875 Heron Rd.
>
> Ottawa, On.
>
> K1A0L5
>
>
>
> James.Corbett@xxxxxxxxxxxxx
>
> Telephone | Téléphone: (613) 941-1338
>
> Facsimile | Télécopieur: (613) 941-2261
>
>
>
> Government of Canada | Gouvernement du Canada
>
>
>
>
>
> ------------------------------
> This e-mail and any attachments to it are confidential and are intended
> solely for use of the individual or entity to whom they are addressed. If
> you have received this e-mail in error, please notify the sender immediately
> and then delete it. If you are not the intended recipient, you must not
> keep, use, disclose, copy or distribute this e-mail without the author's
> prior permission. The views expressed in this e-mail message do not
> necessarily represent the views of Highmark Inc., its subsidiaries, or
> affiliates.
>
>
>

Other related posts: