Wednesday, February 20, 2019

Lesson 2 - Array, String

Array is a set of values with same data type.
Array of number.
let data = [1, 2,3, 4,5]
Array of string.
let data2 = ["Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]
var array = [String](count: 12, repeatedValue: " ")
var array2 = [Int](count: 12, repeatedValue: 0)
We declare array contain 12 String elements, array2 contain 12 integer elements.
To add more elements, we use append
array.append("Monday");
array2.append(9);
To get array size
let size=data.count
Use for to sum all odd number in data.
for i in 0..<5 {
if(data[i]%2==1){
sum=sum+data[i]
    }
}
Run to see result 9.

Care about array index, first element will be array[0], last elements will be array[size-1]
sum=sum+data[i] mean sum previous value with current value.
You can also write it like this sum+=data[i]
Add to data numbers 35,88.
If we want to know position of number 35 in ar array, use for like this.
int index=0;
for i in0..<data.count {
if(data[i]==35){
index=i+1
    }
}
Value equal 6, plus 1 because i=0 mean first elements.
We want to count how many even number in array ar2.
var count=0
for i in0..<data.count {
if(data[i]%2==0){
count=count+1
    }
}
We want to print max number in data, copy these lines.
var max=data[0]
for i in0..<data.count {
if(data[i]>max){
max=data[i]
    }
}
Result is 88.
Next to two dimension array, we declare like this.
let array3  = [[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]]
Now array has 2 rows, 5 column, elements look like this
1  2  3  4  5 
1  2  3  4  5 
Element at row 2, column 3 will be write array3[1][2], it is number 3.
We also can declare like this.
var array4 = Array(count: 2, repeatedValue:Array(count:3,repeatedValue: 0))
Array will has two row, 3 column.
We add value like this
array4[0][0]=4
array4[0][1]=8
array4[0][2]=6
array4[1][0]=5
array4[1][1]=9
mang4[1][2]=3
Elements now look like this
4 8 6
5 9 3
To sum all elements in array4, we use double for loop.
var sum2=0
for i in0..<2 {
for j in0..<3 {
sum2=sum2+array4[i][j]
}
}
Result is 35.

To understand double for loop, think like this:
First, i=0, run for below with j from 0 to 2
Next, i=1, run for with j from 0 to 2
That mean we run single for with j in i times. Here i mean number of row, i=0 is first row, i=1 is second row.
Repeat single for mean double for loop, you should understand it in practical.
Next to string character.
We need to know about split string.
For example, we have a string like this.
let a="This is example text, we will split by comma";
We create an array by split string like this.
let array5 = a.componentsSeparatedByString(",")
Array5 now has 2 elelents "This is example text”, “ we will split by comma"
To remove blank character in “ we will split by comma"
let remo=array5[1].characters.dropFirst()
let string2=String(remo)
To count number of chacracter, use characters.count

Blank chacracter is counted as one character.
We frequently split String to array when make app.


No comments:

Post a Comment