Q about java arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnny

    Q about java arrays

    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 someFunctionOnS omeArray(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
    someFunctionOnS omeArray( 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?

    thanks
    ~johnny

  • Anthony Borla

    #2
    Re: Q about java arrays


    "johnny" <squidish@hotma il.com> wrote in message
    news:oYdyb.7307 8$Vs1.54284@twi ster.austin.rr. com...[color=blue]
    > 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 someFunctionOnS omeArray(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
    > someFunctionOnS omeArray( 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?
    >[/color]

    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 someFunctionOnS omeArray(int [] data, int start);

    or perhaps specify a range:

    void someFunctionOnS omeArray(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


    Comment

    • Jéjé

      #3
      Re: Q about java arrays

      Dans l'article <oYdyb.73078$Vs 1.54284@twister .austin.rr.com> , "johnny"
      <squidish@hotma il.com> a tapoté :[color=blue]
      > in C++, you can fake a sub-array by passing something like (array +
      > int)[/color]
      ....[color=blue]
      > and we have the data array
      > int[] data = { 1,2,3,4,5,6,7,8 ,9,0 };[/color]
      ....[color=blue]
      > the function will have as the array "a" the values { 6,7,8,9,0 }[/color]
      ....[color=blue]
      > My Question:
      > Is there a way to do the same thing in java?[/color]

      AFAIK, there's no way to pick a slide from an array, for instance like
      that: int[] data = {1,2,3,4,5,6,7} ; getting data[5..data.length-1] is
      impossible.

      Maybe someone will explain this java drawback.

      Anyway, I think it's never a good idea to use primitive types when using
      Objects is possible. I've never been confortable about this primitive-object
      mix of java. When you read the api, it sometimes look like Sun guys think
      everybody have an infinite amount of RAM, but hey, they still use 'int'
      as constants instead of static instances. Why ? Saving a few kBytes ?


      --
      --
      A day without sunshine is like a day without orange juice.
      ----------------------------
      jerome.eteve_at _it-omics.com

      Comment

      • SMC

        #4
        Re: Q about java arrays

        On Tue, 02 Dec 2003 20:10:27 +1100, Jéjé wrote:
        [color=blue]
        > Dans l'article <oYdyb.73078$Vs 1.54284@twister .austin.rr.com> , "johnny"
        > <squidish@hotma il.com> a tapoté :[color=green]
        >> in C++, you can fake a sub-array by passing something like (array +
        >> int)[/color]
        > ...[color=green]
        >> and we have the data array
        >> int[] data = { 1,2,3,4,5,6,7,8 ,9,0 };[/color]
        > ...[color=green]
        >> the function will have as the array "a" the values { 6,7,8,9,0 }[/color]
        > ...[color=green]
        >> My Question:
        >> Is there a way to do the same thing in java?[/color]
        >
        > AFAIK, there's no way to pick a slide from an array, for instance like
        > that: int[] data = {1,2,3,4,5,6,7} ; getting data[5..data.length-1] is
        > impossible.
        >[/color]

        An array splice (of sorts):

        int[] original = { 1, 2, 3, 4, 5 }
        int[] splice = new int[3];

        System.arraycop y(original, 0, splice, 0, 3);

        Would give splice = { 1, 2, 3 }

        --
        Sean

        "There are 10 types of people in this world, those who can count in binary,
        and those who can't."

        Comment

        • Raymond DeCampo

          #5
          Re: Q about java arrays

          johnny wrote:[color=blue]
          > 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 someFunctionOnS omeArray(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
          > someFunctionOnS omeArray( 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?
          >[/color]

          As many have already indicated, you can't do this with arrays. However,
          the java.util.List. subList() method provides a similar functionality.
          So, if you are willing to work with Lists, you can use this methodology.
          The drawback is that you cannot put primitives in Lists.

          Ray

          Comment

          • Jéjé

            #6
            Re: Q about java arrays

            > int[] original = { 1, 2, 3, 4, 5 }[color=blue]
            > int[] splice = new int[3];
            >
            > System.arraycop y(original, 0, splice, 0, 3);
            >
            > Would give splice = { 1, 2, 3 }[/color]

            Good old C style :)

            --
            --
            Agree with them now, it will save so much time.
            ----------------------------
            jerome.eteve_at _it-omics.com

            Comment

            Working...