SCALA ARRAYS. Following picture represents array mylist. Here, mylist holds ten double values and the indices are from 0 to 9.

Similar documents
Java-Array. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables.

14. Array Basics. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

CS1150 Principles of Computer Science Arrays

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

CS 2340 Objects and Design - Scala

PASCAL - RECORDS. To define a record type, you may use the type declaration statement. The record type is defined as

Computer Programming, I. Laboratory Manual. Final Exam Solution

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

C# MOCK TEST C# MOCK TEST II

Introduction to Programming Using Java (98-388)

C# MOCK TEST C# MOCK TEST I

Chapter 7: Single-Dimensional Arrays. Declaring Array Variables. Creating Arrays. Declaring and Creating in One Step.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

CS205: Scalable Software Systems

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

Chapter 7 Single-Dimensional Arrays

SWIFT - CLOSURES. Global Functions Nested Functions Closure Expressions. Have a name. Capture values from enclosing function

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

The Warhol Language Reference Manual

How to declare an array in C?

Getting started with Java

Overview. Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays. Initialization Searching

CSC231 C Tutorial Fall 2018 Introduction to C

CS205: Scalable Software Systems

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Chapter 2: Using Data

DB2 MOCK TEST DB2 MOCK TEST I

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Chapter 6 Single-dimensional Arrays

JAVA.LANG.INTEGER CLASS

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Python: common syntax

CS1150 Principles of Computer Science Arrays

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Preview from Notesale.co.uk Page 2 of 79

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average.

Scala. Fernando Medeiros Tomás Paim

COBOL - TABLE PROCESSING

Getting started with Java

SECTION II: LANGUAGE BASICS

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

The SCAlable LAnguage

Computational Expression

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Module 7: Arrays (Single Dimensional)

Outline. Collections Arrays vs. Lists Functions using Arrays/Lists Higher order functions using Arrays/Lists

Array. Array Declaration:

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

CS111: PROGRAMMING LANGUAGE II

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Personal SE. Functions & Arrays

FORTRAN - ARRAYS. For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

Arrays. Eng. Mohammed Abdualal

Praktische Softwaretechnologie

CS115 Principles of Computer Science

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Declaring Array Variable

CS2401 QUIZ 5 February 26, questions / 20 points / 20 minutes NAME:..

Scala : an LLVM-targeted Scala compiler

CS 139 Practice Midterm Questions #2

Introduction to Java & Fundamental Data Types

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

AP Computer Science Lists The Array type

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CSC Java Programming, Fall Java Data Types and Control Constructs

FORTRAN - CHARACTERS

Arrays IT 1033: Fundamentals of Programming

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Problems with simple variables

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Information Science 1

Parallel Programming

SWIFT - FUNCTIONS. Function Declaration: It tells the compiler about a function's name, return type, and parameters.

Array. Prepared By - Rifat Shahriyar

VARIABLES AND TYPES CITS1001

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS 61B Data Structures and Programming Methodology. June David Sun

CSc 520. Principles of Programming Languages 25: Types Introduction

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Variable initialization and assignment

Full file at

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Two Dimensional Arrays

Pace University. Fundamental Concepts of CS121 1

Module 6: Array in C

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Exercise: Inventing Language

15. Multidimensional Arrays

Transcription:

http://www.tutorialspoint.com/scala/scala_arrays.htm SCALA ARRAYS Copyright tutorialspoint.com Scala provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1,..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and..., numbers[99] to represent individual variables. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables. The index of the first element of an array is the number zero and the index of the last element is the total number of elements minus one. Declaring Array Variables: To use an array in a program, you must declare a variable to reference the array and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable: var z:array[string] = new Array[String](3) or var z = new Array[String](3) Here, z is declared as an array of Strings that may hold up to three elements. You can assign values to individual elements or get access to individual elements, it can be done by using commands like the following: z(0) = "Zara"; z(1) = "Nuha"; z(4/2) = "Ayan" Here, the last example shows that in general the index can be any expression that yields a whole number. There is one more way of defining an array: var z = Array("Zara", "Nuha", "Ayan") Following picture represents array mylist. Here, mylist holds ten double values and the indices are from 0 to 9. Processing Arrays: When processing array elements, we often use either for loop because all of the elements in an array are of the same type and the size of the array is known. Here is a complete example of showing how to create, initialize and process arrays:

var mylist = Array(1.9, 2.9, 3.4, 3.5) for ( x <- mylist ) { println( x ) // Summing all elements var total = 0.0; for ( i <- 0 to (mylist.length - 1)) { total += mylist(i); println("total is " + total); // Finding the largest element var max = mylist(0); for ( i <- 1 to (mylist.length - 1) ) { if (mylist(i) > max) max = mylist(i); println("max is " + max); scalac Test.scala scala Test 1.9 2.9 3.4 3.5 Total is 11.7 Max is 3.5 Multi-Dimensional Arrays: There are many situations where you would need to define and use multi-dimensional arrays i. e., arrayswhoseelementsarearrays. For example, matrices and tables are examples of structures that can be realized as two-dimensional arrays. Scala does not directly support multi-dimensional arrays and provides various methods to process arrays in any dimension. Following is the example of defining a two-dimensional array: var mymatrix = ofdim[int](3,3) This is an array that has three elements each being an array of integers that has three elements. The code that follows shows how one can process a multi-dimensional array: var mymatrix = ofdim[int](3,3) // build a matrix for (i <- 0 to 2) { for ( j <- 0 to 2) { mymatrix(i)(j) = j; // Print two dimensional array

for (i <- 0 to 2) { for ( j <- 0 to 2) { print(" " + mymatrix(i)(j)); println(); scalac Test.scala scala Test Concatenate Arrays: Following is the example which makes use of concat method to concatenate two arrays. You can pass more than one array as arguments to concat method. var mylist1 = Array(1.9, 2.9, 3.4, 3.5) var mylist2 = Array(8.9, 7.9, 0.4, 1.5) var mylist3 = concat( mylist1, mylist2) for ( x <- mylist3 ) { println( x ) scalac Test.scala scala Test 1.9 2.9 3.4 3.5 8.9 7.9 0.4 1.5 Create Array with Range: Following is the example, which makes use of range method to generate an array containing a sequence of increasing integers in a given range. You can use final argument as step to create the sequence; if you do not use final argument, then step would be assumed as 1.

var mylist1 = range(10, 20, 2) var mylist2 = range(10,20) for ( x <- mylist1 ) { print( " " + x ) println() for ( x <- mylist2 ) { print( " " + x ) scalac Test.scala scala Test 10 12 14 16 18 10 11 12 13 14 15 16 17 18 19 Scala Arrays Methods: Following are the important methods, which you can use while playing with array. As shown above, you would have to package before using any of the mentioned methods. For a complete list of methods available, please check official documentation of Scala. SN 1 Methods with Description def applyx: T, xs: T : Array[T] Creates an array of T objects, where T can be Unit, Double, Float, Long, Int, Char, Short, Byte, Boolean. 2 def concat[t]xss: Array[T] : Array[T] Concatenates all arrays into a single array. 3 def copysrc: AnyRef, srcpos: Int, dest: AnyRef, destpos: Int, length: Int: Unit Copy one array to another. Equivalent to Java's System.arraycopy src, srcpos, dest, destpos, length. 4 def empty[t]: Array[T] Returns an array of length 0 5 def iterate[t]start: T, len: Intf: (T => T ): Array[T] Returns an array containing repeated applications of a function to a start value. 6 def fill[t]n: Intelem:=> T: Array[T] Returns an array that contains the results of some element computation a number of

times. 7 def fill[t]n1: Int, n2: Intelem:=> T: Array[Array[T]] Returns a two-dimensional array that contains the results of some element computation a number of times. 8 def iterate[t]start: T, len: Intf: (T => T ): Array[T] Returns an array containing repeated applications of a function to a start value. 9 def ofdim[t]n1: Int: Array[T] Creates array with given dimensions. 10 def ofdim[t]n1: Int, n2: Int: Array[Array[T]] Creates a 2-dimensional array 11 def ofdim[t]n1: Int, n2: Int, n3: Int: Array[Array[Array[T]]] Creates a 3-dimensional array 12 def rangestart: Int, end: Int, step: Int: Array[Int] Returns an array containing equally spaced values in some integer interval. 13 def rangestart: Int, end: Int: Array[Int] Returns an array containing a sequence of increasing integers in a range. 14 def tabulate[t]n: Intf: (Int=> T): Array[T] Returns an array containing values of a given function over a range of integer values starting from 0. 15 def tabulate[t]n1: Int, n2: Intf: (Int, Int => T): Array[Array[T]] Returns a two-dimensional array containing values of a given function over ranges of integer values starting from 0. Loading [MathJax]/jax/output/HTML-CSS/jax.js