How to declare Scala array?

0 votes
Can anyone explain how to declare Scala array?
Jul 25 in Apache Spark by Richa
28 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
answered Jul 25 by Gitika
• 25,340 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 in Apache Spark by Gitika
• 25,340 points

edited Jul 29 by Gitika 26 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 in Apache Spark by Gitika
• 25,340 points
28 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 in Apache Spark by Dinesha
28 views
0 votes
1 answer
0 votes
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
• 10,670 points
2,998 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
• 10,670 points
334 views
0 votes
10 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
14,800 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 in Apache Spark by Gitika
• 25,340 points
20 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 in Apache Spark by Gitika
• 25,340 points
23 views