Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I limit the size of an array in Scala?

Tags:

arrays

scala

size

In Java byte[] st = new byte[4096], implies that size of array st will not exceed 4096 bytes.

The Scala equivalent is st:Array[byte] = Array() where size is unknown. If I am reading a huge file into this array, then how can I limit the size of the array?

Will there be any side effect if I don't care about the size for the above operation?

like image 416
kulkarni Avatar asked Feb 09 '10 02:02

kulkarni


People also ask

Can you change the size of an array in Scala?

The Scala Array is an interesting creature: It's mutable in that its elements can be changed, but it's immutable in that its size cannot be changed.

How do you reduce the size of an array?

Reduce Array Size to The Half in C++ We can choose a set of integers and remove all the occurrences of these integers in the array. We have to find the minimum size of the set so that at least half of the integers of the array are removed. So for example, if arr = [3,3,3,3,5,5,5,2,2,7], then the output will be 2.

Do arrays have a size limit?

The theoretical maximum Java array size is 2,147,483,647 elements. To find the size of a Java array, query an array's length property.

What is the maximum size of tuple in Scala?

Tuple are of type Tuple1 , Tuple2 ,Tuple3 and so on. There is an upper limit of 22 for the element in the tuple in the scala, if you need more elements, then you can use a collection, not a tuple.


1 Answers

var buffer = new Array[Byte](4 * 1024)

Works just fine, and it behaves as expected.

like image 144
Stefan Kendall Avatar answered Oct 16 '22 17:10

Stefan Kendall