当前位置: 首页 > 工具软件 > Scala > 使用案例 >

scala切片_Scala切片功能

有宏邈
2023-12-01

scala切片

Today we will look into Scala slice function.

今天,我们将研究Scala slice功能。

斯卡拉切片 (Scala slice)

Scala slice function is available in the following API classes:

以下API类中提供了Scala slice函数:

We will pick-up each API and discuss them in-depth with suitable examples in next section.

我们将挑选每种API,并在下一部分中通过适当的示例进行深入讨论。

Scala slice函数的用法 (Scala slice function usage)

In Scala API, ‘slice’ function is used to select an interval of elements. It takes two parameters of “Int” type and returns subset or whole or none element(s) of original Collection (or String or Array).

在Scala API中,“切片”功能用于选择元素的间隔。 它使用两个“ Int”类型的参数,并返回原始Collection(或String或Array)的子集或全部或不包含元素。

Real-world slice scenario:-
We can use this slice function in our regular real-world life too as shown below.

现实世界中的场景:
我们也可以在常规的现实生活中使用此slice函数,如下所示。

Here Bread.slice(0,10) means take bread slices from 0 to 9 so total 10 Bread slices.(Just to resemble Scala’s slice function here I’m using zero, but assume this as one.)

这里的Bread.slice(0,10)表示从0到9切面包片,因此总共10个面包片(就像在这里使用Scala的slice函数一样,我使用的是零,但将其假定为一)。

slice function syntax:
In Scala Standard Library (API), this slice function is defined as follows:

切片函数语法:
在Scala标准库(API)中,此分片函数定义如下:

def slice(from-index: Int, until-index: Int): ScalaAPIClass[A]

Here “ScalaAPIClass” means any Scala Collection class (which supports index based access like Seq, List etc), String, Array classes.

这里的“ ScalaAPIClass”表示任何Scala集合类(它支持基于索引的访问,例如Seq,List等),String和Array类。

Example:-
The following function is defined in Scala API’s Seq class.

例:-
以下函数在Scala API的Seq类中定义。

def slice(from-index: Int, until-index: Int): Seq[A]

slice function Parameters:
The “slice” function parameter’s usage are described in the following table:

切片函数参数:
下表描述了“ slice”功能参数的用法:

S.No.Function ParamsUsage
1.First ParameterStarting index (Inclusive). It should be zero or any any positive integer less than the length of the Collection or String or Array.
2.Second ParameterEnding index (Exclusive).
序号 功能参数 用法
1。 第一个参数 起始索引(含)。 它应该为零或小于Collection或String或Array的长度的任何正整数。
2。 第二个参数 结束索引(不包括)。

slice function extract elements starting from ‘first-index’ (Inclusive) to ‘until-index’ (exclusive). Here elements numbers for an Array of Numbers, Characters for a String, an object for a Collection.

slice函数提取从“第一索引”(包含)到“直到索引”(不含)开始的元素。 这里的元素数字代表数字数组,字符代表字符串,对象代表集合。

Scala数组切片 (Scala Array Slice)

In Scala API, Array class defines slice function as follows:

在Scala API中,Array类定义切片函数如下:

def slice(from: Int, until: Int): Array[T]

Here ‘from’ is the starting index (Inclusive) of the Array and ‘until’ is the ending index (Exclusive) of the Array.

这里的“ from”是数组的开始索引(包括),“ until”是数组的结束索引(不包括)。

Array slice Function Examples:

数组切片功能示例:

scala> val marksArray = Array(56,79,60,99,71)
marksArray: Array[Int] = Array(56, 79, 60, 99, 71)

Int of Array with 5 values are created so it’s index start value is 0 and index end value is 4.
It’s length = 5

创建具有5个值的Array的Int,因此其索引起始值为0,索引终止值为4。
长度= 5

Let us play with slice function now.

现在让我们使用切片功能。

scala> marksArray.slice(0,2)
res0: Array[Int] = Array(56, 79)

It starts with 0 index that is first element and retrieves all elements until 2 means index = 1 that’s why we got 0th element and 1st element here.

它从作为第一个元素的0索引开始,然后检索所有元素,直到2表示index = 1,这就是我们在这里得到第0个元素和第一个元素的原因。

scala> marksArray.slice(3,4)
res1: Array[Int] = Array(99)

We can access with any indices range.

我们可以访问任何索引范围。

scala> marksArray.slice(3,3)
res2: Array[Int] = Array()

If we give same values for start and end like above we will get empty array why?

如果我们像上面一样给start和end赋予相同的值,为什么会得到空数组?

Start index = 3
End index = 3 – 1 = 2

起始索引= 3
结束索引= 3 – 1 = 2

It’s not possible to retrieve a set of elements from an array from 3 to 2 indices right.

不可能从3到2个索引的数组中检索一组元素。

scala> marksArray.slice(-1,3)
res3: Array[Int] = Array(56, 79, 60)

If we give -ve values, it just starts with available index as shown above.

如果我们给-ve值,它只是从可用索引开始,如上所示。

scala> marksArray.slice(0,5)

res4: Array[Int] = Array(56, 79, 60, 99, 71)

If we give 2nd parameter value in beyond it’s available index as shown above (Available max index value in marksArray is 4 only as it’s length = 5), it just ignores that value and returns up-to available index only.

如果我们在上面提供的第二个参数值超出其可用索引的位置(marksArray中的可用最大索引值只有4,因为它的长度= 5),它只会忽略该值并仅返回最新的可用索引。

NOTE:- Unlike Java, it does not throw any ArrayIndexOutOfBoundsException.

注意:-与Java不同,它不会引发任何ArrayIndexOutOfBoundsException。

Scala Collection切片 (Scala Collection slice)

In Scala’s Standard API, most of the classes defines this slice function which supports index based elements access. For instance, List class defines this function as shown below:

在Scala的Standard API中,大多数类都定义了此slice函数,该函数支持基于索引的元素访问。 例如,List类定义此功能,如下所示:

def slice(from: Int, until: Int): List[A]

List slice function examples:-

列表切片功能示例:-

Same as Array examples, we will get same results for any Collection API.

与Array示例相同,任何Collection API都会获得相同的结果。

scala> val list = List(56, 79, 60, 99, 71)
list: List[Int] = List(56, 79, 60, 99, 71)

scala> list.slice(0,2)
res5: List[Int] = List(56, 79)

scala> list.slice(3,4)
res6: List[Int] = List(99)

scala> list.slice(3,3)
res7: List[Int] = List()

scala> list.slice(-1,3)
res8: List[Int] = List(56, 79, 60)

scala> list.slice(0,5)
res9: List[Int] = List(56, 79, 60, 99, 71)

If we access an empty list, we will get empty list only as shown below

如果我们访问一个空列表,我们将只获得空列表,如下所示

scala> val list2 = List()
list2: List[Nothing] = List()

scala> list2.slice(0,1)

res10: List[Nothing] = List()

Scala字符串切片 (Scala String slice)

In Scala API, “StringOps” class is defined in scala.collection.immutable package. It defines slice function as shown below:

在Scala API中,“ StringOps”类在scala.collection.immutable包中定义。 它定义了切片功能,如下所示:

def slice(from: Int, until: Int): String

NOTE:- In Scala, we use Java’s String class. But this class does NOT have slice function. When we use slice function on Java’s String objects, Scala Compiler internally converts this String object into StringOps class object to use this slice function. (Not only slice function many more. See Scala API for more information.)

注意:-在Scala中,我们使用Java的String类。 但是此类没有切片功能。 当我们在Java的String对象上使用切片函数时,Scala编译器会在内部将此String对象转换为StringOps类对象,以使用此切片函数。 (不仅切片功能更多。有关更多信息,请参见Scala API。)

That means “StringOps” is an implicit class of String class.

这意味着“ StringOps”是String类的隐式类。

String slice’s function examples:-

字符串切片的函数示例:-

scala> val str = "Hello I'm doing good. How are you?"
str: String = Hello I'm doing good. How are you?

scala> str.slice(6,9)

res8: String = I'm

As we know, String index starts with zero.
Here from-index = 6 means
until-index = 9 (It’s exclusive so we need to consider till index = 8 only)

众所周知,字符串索引从零开始。
此处from-index = 6表示
直到索引= 9(这是排他性的,因此我们仅需考虑直到索引= 8)

String’s substring function works same as it’s slice function as shown below:

字符串的子字符串功能与其切片功能相同,如下所示:

scala> str.substring(6,9)
res12: String = I'm

Here both str.slice(6,9) and str.substring(6,9) are returning same value.

在这里,str.slice(6,9)和str.substring(6,9)都返回相同的值。

slice Vs substring

切片Vs子串

Difference between slice and substring functions of String class

String类的slice和substring函数之间的区别

  • Functionality wise and syntax wise there is no difference

    在功能上和语法上没有区别
  • Performance is almost similar and ignorable.

    性能几乎相似且可忽略。

NOTE:- In Scala, We can access String characters just like an Array elements as shown below:

注意:-在Scala中,我们可以像访问数组元素一样访问字符串字符,如下所示:

scala> str(0)
res0: Char = H

Here it returns a Char, but not a String

在这里它返回一个Char,但不是一个String

scala> str(-1)
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  at java.lang.String.charAt(String.java:658)
  at scala.collection.immutable.StringOps$.apply$extension(StringOps.scala:38)
  ... 33 elided

scala> str.length
res2: Int = 34

scala> str(34)
java.lang.StringIndexOutOfBoundsException: String index out of range: 34
  at java.lang.String.charAt(String.java:658)
  at scala.collection.immutable.StringOps$.apply$extension(StringOps.scala:38)

  ... 33 elided

NOTE:- If we try to access String characters in out of range, we get StringIndexOutOfBoundsException as shown above.

注意:-如果我们尝试访问超出范围的字符串字符,则会得到StringIndexOutOfBoundsException,如上所示。

String’s character access returns Char where as substring & slice functions returns String as shown below.

字符串的字符访问返回Char,其中子字符串和切片函数返回String,如下所示。

scala> str(0)
res4: Char = H

scala> str.substring(0,1)
res5: String = H

scala> str.slice(0,1)
res6: String = H

That’s it all about “Scala’s slice function” usage. We will discuss some more Scala concepts in my coming posts.

这就是“ Scala的切片功能”用法的全部内容。 我们将在我的后续文章中讨论更多Scala概念。

Reference: Scala API Doc

参考: Scala API文档

翻译自: https://www.journaldev.com/11113/scala-slice-function

scala切片

 类似资料: