You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Deep Pradhan <pr...@gmail.com> on 2014/09/06 11:09:39 UTC

How to change the values in Array of Bytes

Hi,
I have an array of bytes and I have filled the array with 0 in all the
postitions.


*var Array = Array.fill[Byte](10)(0)*
Now, if certain conditions are satisfied, I want to change some elements of
the array to 1 instead of 0. If I run,



*if (Array.apply(index)==0) Array.apply(index) = 1*
it returns me an error.

But if I assign *Array.apply(index) *to a variable and do the same thing
then it works. I do not want to assign this to variables because if I do
this, I would be creating a lot of variables.

Can anyone tell me a method to do this?

Thank You

Re: How to change the values in Array of Bytes

Posted by Aaron Davidson <il...@gmail.com>.
More of a Scala question than Spark, but "apply" here can be written with
just parentheses like this:

val array = Array.fill[Byte](10)(0)
if (array(index) == 0) {
  array(index) = 1
}

The second instance of "array(index) = 1" is actually not calling apply,
but "update". It's a scala-ism that's usually invisible. The above is
equivalent to:

if (array.apply(index) == 0) {
  array.update(index, 1)
}



On Sat, Sep 6, 2014 at 2:09 AM, Deep Pradhan <pr...@gmail.com>
wrote:

> Hi,
> I have an array of bytes and I have filled the array with 0 in all the
> postitions.
>
>
> *var Array = Array.fill[Byte](10)(0)*
> Now, if certain conditions are satisfied, I want to change some elements
> of the array to 1 instead of 0. If I run,
>
>
>
> *if (Array.apply(index)==0) Array.apply(index) = 1*
> it returns me an error.
>
> But if I assign *Array.apply(index) *to a variable and do the same thing
> then it works. I do not want to assign this to variables because if I do
> this, I would be creating a lot of variables.
>
> Can anyone tell me a method to do this?
>
> Thank You
>
>
>