How to declare Scala array

0 votes
Can anyone explain how to declare Scala array?
Jul 25, 2019 in Apache Spark by Richa
746 views

1 answer to this question.

0 votes

Hi,

We can declare Scala arrays in two ways:

  • Adding Elements Later: We can create an array in Scala with default initial elements according to data type, and then fill in values later.

scala> var a:Array[Int]=new Array[Int](3)

a: Array[Int] = Array(0, 0, 0)

scala> a(4/2)=3

scala> a

res10: Array[Int] = Array(0, 0, 3)
  • Define Scala Array with Values: We can also define a Scala array specifying its values in place.
​scala> var a=Array(1,2,3)

​a: Array[Int] = Array(1, 2, 3

)​​scala> a(4)

java.lang.ArrayIndexOutOfBoundsException: 4

scala> a(0)

res12: Int = 1

If you want to know more about Apache Spark Scala, It's highly recommended to go for the Apache Spark Certification today.

answered Jul 25, 2019 by Gitika
• 65,770 points

Related Questions In Apache Spark

0 votes
1 answer

How to declare a Empty Scala Map?

Hi, You can either declare an empty Scala ...READ MORE

answered Jul 29, 2019 in Apache Spark by Gitika
• 65,770 points

edited Jul 29, 2019 by Gitika 2,423 views
0 votes
1 answer

How to declare sets with value in Scala?

Hey, You can follow this code: scala> var s=Set(1,4,4,3) s: ...READ MORE

answered Jul 30, 2019 in Apache Spark by Gitika
• 65,770 points
767 views
0 votes
1 answer
0 votes
1 answer

Scala: Add user input to array

You can try this:  object printarray { ...READ MORE

answered Jun 19, 2019 in Apache Spark by Dinesha
4,738 views
+1 vote
2 answers
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
11,028 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
2,536 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
108,831 views
0 votes
1 answer

How to process an Array in Scala?

Hi, We can use loop control structures to ...READ MORE

answered Jul 25, 2019 in Apache Spark by Gitika
• 65,770 points
641 views
0 votes
1 answer

How to create Scala Array with Range?

Hey, The range() method will give us integers ...READ MORE

answered Jul 25, 2019 in Apache Spark by Gitika
• 65,770 points
738 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP