scala元組 數組_Scala中的數組

scala元組 數組

Scala中的數組 (Arrays in Scala)

An array is a linear data structure with a fixed number of elements. It is a collection that stores a fixed number Arrays in Scalf elements of the same datatype. In Scala, an array is 0 indexed, i.e. the first element has an index of zero. The last element of the array has an index of the last element minus one.

數組是具有固定數量元素的線性數據結構 。 它是一個集合,該集合在相同數據類型的Scalf元素中存儲固定數量的Array。 在Scala中, 數組的索引為0,即第一個元素的索引為零。 數組的最后一個元素的索引為最后一個元素減一。

The syntax of scala array creation is just the same as in Java but is a lot more powerful in term of features and methods backing it. It can also support sequence functions in Scala. In Scala, for defining array there is liberty on the data type. i.e. you can skip assigning of the datatype of the array. Also, it supports all types of elements.

Scala數組創建語法與 Java中的語法相同,但是在支持它的功能和方法方面要強大得多。 它還可以在Scala中支持序列功能。 在Scala中,用于定義數組的數據類型具有自由性。 即,您可以跳過數組數據類型的分配。 此外,它支持所有類型的元素。

An array can extend up to as many dimensions as you want but only 1-D, 2-D, and 3-D arrays are commonly used. Here, we will discuss only a one-dimensional array.

數組可以擴展到任意數量的維,但是通常僅使用1-D2-D3-D數組 。 在這里,我們將只討論一維數組

一維數組 (ONE DIMENSIONAL ARRAY)

A one-dimensional array is one which has only one row that stores data in it. It uses contagious memory allocation for elements at index 0 to total minus one.

一維數組是只有一行存儲數據的數組 。 它使用傳染性內存分配將索引0的元素總計減為1。

Syntax:

句法:

    // its only single dimension...var arrayname = new Array[datatype](size)  

Methods to create an array in Scala

在Scala中創建數組的方法

You can optionally specify the data type of the array in Scala.

您可以選擇在Scala中指定數組的數據類型。

    // It this we have specified that the array will contain string explicitly. 
val name: String = new Array("Ram", "Akash", "Palak", "Geeta", "Sudhir");  
//In this creation method the  will itself make 
//the array of double datatype.
val percentage = new Array(46.4 , 87.4 , 76.2 , 56.9 , 89.87)   

Example:

例:

object MyClass {
def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {
var i=0
var name =  Array("Ram", "Akash", "Palak", "Geeta", "Sudhir")
var percentage =  Array (46.4 , 87.4 , 76.2 , 56.9 , 89.87)
println("Printing student names");
for(i <- 0 to name.length-1){
println("Student " + name(i) + " has scored " + percentage(i) + "%")
}
}
}

Output

輸出量

Printing student names
Student Ram has scored 46.4%
Student Akash has scored 87.4%
Student Palak has scored 76.2%
Student Geeta has scored 56.9%
Student Sudhir has scored 89.87%

翻譯自: https://www.includehelp.com/scala/arrays-in-scala.aspx

scala元組 數組

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540552.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540552.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540552.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

OpenStack —— DevStack一鍵自動化安裝

一、DevStack介紹Devstack目前是支持Ubuntu16.04和CentOS 7&#xff0c;而且Devstack官方建議使用Ubuntu16.04&#xff0c;所以我們使用Ubuntu 16.04進行安裝。默認無論是Devstack和OpenStack&#xff0c;都是采用Master的代碼進行安裝&#xff0c;這樣經常會出現&#xff0c;今…

[轉載] Python學習筆記——運維和Shell

參考鏈接&#xff1a; 在C / C&#xff0c;Python&#xff0c;PHP和Java中交換兩個變量 目錄 什么是運維 運維第一工具-shell編程 shell歷史 執行腳本 基本語法 Shell腳本語法 條件測試&#xff1a;test [ if/then/elif/else/fi case/esac for/do/done …

scala java混合_Scala特性混合

scala java混合Scala | 特性混合 (Scala | Trait Mixins ) In Scala, the number of traits can be extended using a class or an abstract class. This is known as Trait Mixins. For extending, only traits, the blend of traits, class or abstract class are valid. If …

Scala鑄造

Scala中的類型 (Types in Scala) Type also know as data type tells the compiler about the type of data that is used by the programmer. For example, if we initialize a value or variable as an integer the compiler will free up 4 bytes of memory space and it wi…

/ 卡路里_最大卡路里

/ 卡路里Problem statement: 問題陳述&#xff1a; Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day. In his diet plan, he can’t eat on for three consecutive days. But since …

[轉載] Python類中的私有變量和公有變量

參考鏈接&#xff1a; Python中的私有變量 我們這里就直奔主題&#xff0c;不做基礎鋪墊&#xff0c;默認你有一些Python類的基礎&#xff0c;大家在看這篇博客的時候&#xff0c;如果基礎知識忘了&#xff0c;可以去菜鳥教程 從一個簡單的類開始 class A(): #定義一…

OpenCV探索之路(二十五):制作簡易的圖像標注小工具

搞圖像深度學習的童鞋一定碰過圖像數據標注的東西&#xff0c;當我們訓練網絡時需要訓練集數據&#xff0c;但在網上又沒有找到自己想要的數據集&#xff0c;這時候就考慮自己制作自己的數據集了&#xff0c;這時就需要對圖像進行標注。圖像標注是件很枯燥又很費人力物力的一件…

固件的完整形式是什么?

FW&#xff1a;前進 (FW: Forward) FW is an abbreviation of "Forward". FW是“ Forward”的縮寫 。 It is an expression, which is commonly used in Gmail or messaging platform. It is also written as FWD or Fwd or Fw. It shows that the email has been s…

[轉載] python __slots__ 詳解(上篇)

參考鏈接&#xff1a; Python的__name __(特殊變量) python中的new-style class要求繼承Python中的一個內建類型&#xff0c; 一般繼承object&#xff0c;也可以繼承list或者dict等其他的內建類型。 在python新式類中&#xff0c;可以定義一個變量__slots__&#xff0c;它的作…

委托BegionInvoke和窗體BegionInvoke

委托BegionInvoke是指通過委托方法執行多線程任務&#xff0c;例如&#xff1a; //定義委托成員變量 delegate void dg_DeleAirport(); //指定委托函數 dg_DeleAirport dga AirportBLL.DeleteHistoryTransAirport; //通過BeginInvoke以異步線程方式執行委托函數&#xff0c;可…

圖論 弦_混亂的弦

圖論 弦Problem statement: 問題陳述&#xff1a; You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the s…

[轉載] Python列表操作

參考鏈接&#xff1a; Python中的基本運算符 Python列表&#xff1a; 序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置&#xff0c;或索引&#xff0c;第一個索引是0&#xff0c;第二個索引是1&#xff0c;依此類推&#xff1b; Python有6個序列的…

「原創」從馬云、馬化騰、李彥宏的對話,看出三人智慧差在哪里?

在今年中國IT領袖峰會上&#xff0c;馬云、馬化騰、李彥宏第一次單獨合影&#xff0c;同框畫面可以說很難得了。BAT關心的走勢一直是同行們競相捕捉的熱點&#xff0c;所以三位大Boss在這次大會上關于人工智能的見解&#xff0c;也受到廣泛關注與多方解讀。馬云認為機器比人聰明…

python 注釋含注釋_Python注釋

python 注釋含注釋Python注釋 (Python comments) Comments in Python are used to improve the readability of the code. It is useful information given by the programmer in source code for a better understanding of code and logic that they have used to solve the …

C2的完整形式是什么?

C2&#xff1a;核心2 (C2: Core 2) C2 is an abbreviation of "Core 2" or "Intel Core 2". C2是“ Core 2”或“ Intel Core 2”的縮寫 。 It is a family of Intels processor which was launched on the 27th of July, 2006. It comprises a series of…

scala特性_Scala | 特性應用

scala特性特性應用 (Trait App) Scala uses a trait called "App" which is used to convert objects into feasible programs. This conversion is done using the DelayedInit and the objects are inheriting the trait named App will be using this function. T…

[轉載] Python3中的表達式運算符

參考鏈接&#xff1a; Python中的除法運算符 1&#xff1a;Python常用表達式運算符 yield 生成器函數send協議 lambda args:expression 創建匿名函數 x if y else z 三元選擇表達式(當y為真時&#xff0c;x才會被計算) x or y 邏輯或(僅但x為假時y才會被計算) x and …

字符串矩陣轉換成長字符串_字符串矩陣

字符串矩陣轉換成長字符串Description: 描述&#xff1a; In this article, we are going to see how backtracking can be used to solve following problems? 在本文中&#xff0c;我們將看到如何使用回溯來解決以下問題&#xff1f; Problem statement: 問題陳述&#xf…