Re: Q about java arrays

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/30/03

  • Next message: Hal Vaughan: "Is It Possible To Access Remote Classes?"
    Date: Sun, 30 Nov 2003 05:56:50 GMT
    
    

    "johnny" <squidish@hotmail.com> wrote in message
    news:oYdyb.73078$Vs1.54284@twister.austin.rr.com...
    > hello.
    > in C++, you can fake a sub-array by passing something
    > like (array + int). If this was passed to something that
    > takes an array as a parameter, the function will deal
    > with the array using the int as the starting value.
    > for example....
    >
    > if we have some function with the declaration
    > void someFunctionOnSomeArray(int[] a)
    >
    > and we have the data array
    > int[] data = { 1,2,3,4,5,6,7,8,9,0 };
    >
    > and then we call it with
    > someFunctionOnSomeArray( data + 5 );
    >
    > the function will have as the array "a" the values
    > { 6,7,8,9,0 }
    >
    > My Question:
    > Is there a way to do the same thing in java?
    >

    In C++ an array [single dimension arrays - what may appear to be
    multi-dimensional arrays are often fudged using pointers], whether stack or
    heap based, is merely a contiguous chunk of memory [even when each element
    is, itself, a fully-fledged object]. This, together with the ability to
    obtain, and use, the address of arbitrary memory locations, allows the
    practice you mention.

    In Java an array is an object, and each element of the array is, itself, a
    separate entity. Even if each element were contiguously located in memory
    [something potentially only useful for primitive-type arrays anyway] there
    is no way to obtain the address of arbitrary memory locations. So the answer
    is: no.

    You would have to rewrite your method [C++ has member data and member
    functions, Java has fields and methods] to include a processing start
    location:

        void someFunctionOnSomeArray(int [] data, int start);

    or perhaps specify a range:

        void someFunctionOnSomeArray(int [] data, int start, int end);

    For better or worse, many of the tricks and shortcuts common in C and C++
    code must be abandoned, and a more Java-oriented mindset adopted. This takes
    time, and determination.

    I hope this helps.

    Anthony Borla


  • Next message: Hal Vaughan: "Is It Possible To Access Remote Classes?"

    Relevant Pages

    • Re: Java Generic programming using subclassing
      ... Java nowhere guarantees that the elements of an array ... Now chances are that any Java ... > reference will be visible through the other. ... > eligible for garbage collection, and will in fact be collected before ...
      (comp.lang.java.programmer)
    • Re: java based supercomputer
      ... checking the correlation beteween an array of data and another array ... java psuedo remote threads will take a considerrably less time. ... Does your algorithm lend itself well to paralellization? ... the only bottleneck i can see is checking the correlation value ...
      (comp.lang.java.programmer)
    • Re: How to store a large amount of 3D data points in Java?
      ... >>None of which would be a problem if you could store ints in a Java ... > With 1.5 autoboxing you can, even if it is implemented with objects. ... ArrayList does array copy for resizing. ...
      (comp.lang.java.programmer)
    • Re: MinMax aus Integer-Array schnell ermitteln
      ... Trick "Loop unrolling" erreicht. ... Java sollte eigentlich an Delphi rankommen, ... Array UP ... private static void fillArray0() ...
      (de.comp.lang.java)
    • Re: MinMax aus Integer-Array schnell ermitteln
      ... Trick "Loop unrolling" erreicht. ... Java sollte eigentlich an Delphi rankommen, ... Array UP ... private static void fillArray0() ...
      (de.comp.lang.java)