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/04 10:04:32 UTC

Iterate over ArrayBuffer

Hi,
I have the following ArrayBuffer
*ArrayBuffer(5,3,1,4)*
Now, I want to iterate over the ArrayBuffer.
What is the way to do it?

Thank You

Re: Iterate over ArrayBuffer

Posted by Ngoc Dao <ng...@gmail.com>.
> I want to iterate over the ArrayBuffer.

You should get yourself familiar with methods related to the Scala
collection library:
https://twitter.github.io/scala_school/collections.html

Almost all of the methods take a function as their parameter. This is
a very convenient feature of Scala (unlike in Java, where you have to
get an iterator, the use the iterator.hasNext and .next).

For an example, you can write like:
ArrayBuffer(5,3,1,4).foreach(println)

It's because "foreach" expects a one-parameter function as its
parameter, and "println" is such a function.

On Thu, Sep 4, 2014 at 5:57 PM, Madabhattula Rajesh Kumar
<mr...@gmail.com> wrote:
> Hi Deep,
>
> If you are requirement is to read the values from ArrayBuffer use below code
>
> scala> import scala.collection.mutable.ArrayBuffer
> import scala.collection.mutable.ArrayBuffer
>
> scala> var a = ArrayBuffer(5,3,1,4)
> a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5, 3, 1, 4)
>
> scala> for(b <- a)
>      | println(b)
> 5
> 3
> 1
> 4
>
> scala>
>
>
> Regards,
> Rajesh
>
>
> On Thu, Sep 4, 2014 at 1:34 PM, Deep Pradhan <pr...@gmail.com>
> wrote:
>>
>> Hi,
>> I have the following ArrayBuffer
>> ArrayBuffer(5,3,1,4)
>> Now, I want to iterate over the ArrayBuffer.
>> What is the way to do it?
>>
>> Thank You
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@spark.apache.org
For additional commands, e-mail: user-help@spark.apache.org


Re: Iterate over ArrayBuffer

Posted by Madabhattula Rajesh Kumar <mr...@gmail.com>.
Hi Deep,

If you are requirement is to read the values from ArrayBuffer use below code

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> var a = ArrayBuffer(5,3,1,4)
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5, 3, 1, 4)

scala> for(b <- a)
     | println(b)
5
3
1
4

scala>


Regards,
Rajesh


On Thu, Sep 4, 2014 at 1:34 PM, Deep Pradhan <pr...@gmail.com>
wrote:

> Hi,
> I have the following ArrayBuffer
> *ArrayBuffer(5,3,1,4)*
> Now, I want to iterate over the ArrayBuffer.
> What is the way to do it?
>
> Thank You
>