Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala list contains vs array contains

Tags:

scala

Out of interest why does this work in Scala:

val exceptions = List[Char]('+')    
assertTrue(exceptions.contains('+'))

but this not

val exceptions = new Array[Char]('+')    
assertTrue(exceptions.contains('+'))
like image 508
chrisb Avatar asked Aug 07 '12 09:08

chrisb


People also ask

What is difference between Array and list in Scala?

Following are the point of difference between lists and array in Scala: Lists are immutable whereas arrays are mutable in Scala. Lists represents a linked list whereas arrays are flat.

How do you check if a list contains an element in Scala?

contains() function in Scala is used to check if a list contains the specific element sent as a parameter. list. contains() returns true if the list contains that element.

Can list have different data types in Scala?

In a Scala list, each element need not be of the same data type.

How fast is contain?

Contains() ought to be around 50 nanoseconds. So the overall operation takes 50.05 microseconds. A faster Contains might take half the time, the overall operation takes 50.025 microseconds.


1 Answers

Because you wrote new ArrayChar. Doing that, the argument is the size of the array, and the '+' is, rather unfortunately, converted to an int to give the size. And the returned array is full of Char(0).

You should just do Array[Char]('+'), '+' would then be single element in the Array.

like image 176
Didier Dupont Avatar answered Sep 18 '22 02:09

Didier Dupont